<template>
  <el-card style="margin: 5px;margin-top: -20px">
    <el-form :model="formData" ref="dataForm" :rules="rules" label-width="100px" style="margin-bottom: -15px">
      <el-button-group style="display: flex;margin: 0px 0 10px 80%; height: 35px">
        <el-button
          type="primary"
          @click.native="submit()"
        >保存
        </el-button>
      </el-button-group>

      <el-row>
        <el-col :span="10">
          <el-form-item class="query-form-item" prop="deptCode" label="部门:">
            <el-select v-model="formData.deptCode"
                       placeholder="请选择部门"
                       clearable="true"
                       @change="deptChange"
                       style="width: 90%"
            >
              <el-option
                v-for="item in deptList"
                :key="item.name"
                :label="item.name"
                :value="item.code">
                <span style="float: left">{{ item.name }}</span>
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="10">
          <el-form-item class="query-form-item" prop="invCode" label="仓库:">
            <el-select v-model="formData.invCode"
                       placeholder="请选择仓库"
                       clearable="true"
                       style="width: 90%"
            >
              <el-option
                v-for="item in invList"
                :key="item.name"
                :label="item.name"
                :value="item.code">
                <span style="float: left">{{ item.name }}</span>
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="10">
          <el-form-item class="query-form-item" prop="originCode" label="原始编码:">
            <el-input v-model="formData.originCode" placeholder="请输入编码" style="width: 90%" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item class="query-form-item" prop="code" label="物资编码:">
            <el-input v-model="formData.code" placeholder="请输入编码" style="width: 90%" clearable disabled></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="10">
          <el-form-item class="query-form-item" prop="deviceName" label="设备名称:">
            <el-input v-model="formData.deviceName" placeholder="请输入设备名称" style="width: 90%"
                      clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item class="query-form-item" prop="ggxh" label="规格型号:">
            <el-input v-model="formData.ggxh" placeholder="请输入规格型号" style="width: 90%" clearable></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="10">
          <el-form-item label="批次号:">
            <el-input v-model="formData.batchNo" placeholder="请输入批次号" style="width: 90%" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="序列号:">
            <el-input v-model="formData.serialNo" placeholder="请输入序列号" style="width: 90%" clearable></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="10">
          <el-form-item label="生产日期:">
            <el-input v-model="formData.productionDate" placeholder="请输入生产日期" style="width: 90%"
                      clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="失效日期:">
            <el-input v-model="formData.expireDate" placeholder="请输入失效日期" style="width: 90%"
                      clearable></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="10">
          <el-form-item label="生产厂家:">
            <el-input v-model="formData.manufactory" placeholder="请输入生产厂家" style="width: 90%"
                      clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="计量单位:">
            <el-input v-model="formData.measname" placeholder="请输入计量单位" style="width: 90%" clearable></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="10">
          <el-form-item label="注册证号:">
            <el-input v-model="formData.zczbhhzbapzbh" placeholder="请输入注册备案凭证号" style="width: 90%"
                      clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="供应商:">
            <el-input v-model="formData.supName" placeholder="请输入供应商" style="width: 90%" clearable></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="10">
          <el-form-item label="负责人:">
            <el-select v-model="formData.manager"
                       placeholder="请选择负责人"
                       clearable="true"
                       style="width: 90%"
            >
              <el-option v-for="item in userList"
                         :key="item.employeeName"
                         :label="item.employeeName"
                         :value="item.userId">
                <span>{{ item.employeeName }}</span>
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="18">
          <el-form-item label="备注:">
            <el-input v-model="formData.remark" type="textarea" style="width: 100%" clearable></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </el-card>

</template>

<script>
import {addDeptDevice, saveDeptDevice, updateDeptDevice} from "@/api/dev/deptDevice";
import {isBlank} from "@/utils/strUtil";
import {getDeptListByUser} from "@/api/auth/authDept";
import {getInvListByUser} from "@/api/system/invWarehouse";
import {listDeptUser} from "@/api/system/deptUser";


export default {
  props: {
    formData: {
      type: Object,
      required: true
    },
    closeDialog: {
      type: Function,
      required: true
    }
  },
  name: "deptDeviceModifyDialog",
  data() {
    return {
      deptList: [],
      invList: [],
      userList: [],
      rules: {
        deptCode: [
          {required: true, message: '请选择部门', trigger: 'change'}
        ],
        invCode: [
          {required: true, message: '请选择仓库', trigger: 'change'}
        ],
        deviceName: [
          {required: true, message: '请输入设备名称', trigger: 'blur'}
        ],
        originCode: [
          {required: true, message: '请输入原始编码', trigger: 'blur'}
        ]
      }
    };
  },
  methods: {
    deptChange() {
      this.formData.invCode = null;
      this.formData.manager = null;
      this.invList = [];
      this.getInvList();
      this.getDeptUserList();
    },
    getDeptList() {
      getDeptListByUser().then((res) => {
        this.deptList = res.data || [];
        this.getInvList();
        this.getDeptUserList();
      });
    },
    getInvList() {
      let params = {deptCode: this.formData.deptCode};
      getInvListByUser(params)
        .then((response) => {
          this.invList = response.data || [];
          this.getList();
        })
        .catch(() => {
        });
    },
    getDeptUserList() {
      this.userList = [];
      let params = {deptId: null};
      this.deptList.forEach((item) => {
        if (item.code === this.formData.deptCode) {
          params.deptId = item.id;
        }
      });
      listDeptUser(params).then((res) => {
        this.userList = res.data.list || [];
      })
    },
    submit() {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          this.$message.error("请完善必填信息");
        } else {
          let formName = isBlank(this.formData.id) ? "add" : "edit";
          saveDeptDevice(this.formData, formName).then((res) => {
            if (res.code === 20000) {
              this.$message.success("添加成功");
              this.closeDialog();
            } else {
              this.$message.error(res.message);
            }
          }).catch((error) => {
            this.$message.error(error.message);
          });
        }
      })
    },
  },
  created() {
    this.getDeptList();
  },
}
</script>

<style scoped>

</style>
