<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 85%; 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.deviceCode" placeholder="请输入编码" show-word-limit maxlength="11"
                      style="width: 90%" clearable disabled></el-input>
<!--            <el-button class="ml5" type="text" @click="genCode()">生成-->
<!--            </el-button>-->
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item class="query-form-item" prop="code" label="物资编码:">
            <el-input v-model="formData.productId" 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.productName" 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-date-picker
                style="width: 90%"
                v-model="formData.productionDate"
                type="date"
                placeholder="请输入生产日期"f
                format="yyyy 年 MM 月 dd 日"
                value-format="yyyy-MM-dd">
              </el-date-picker>
<!--            </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-date-picker
              style="width: 90%"
              v-model="formData.expireDate"
              type="date"
              placeholder="请输入失效日期"f
              format="yyyy 年 MM 月 dd 日"
              value-format="yyyy-MM-dd">
            </el-date-picker>
          </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="供应商:" prop="supId" style="width: 92%">
<!--            <el-input v-model="formData.supName" placeholder="请输入供应商" style="width: 90%" clearable></el-input>-->
            <el-select
              v-model="formData.supId"
              filterable
              remote
              clearable="true"
              reserve-keyword
              placeholder="请选择供应商"
              :loading="loading"
              style="width: 100%;"
              @change="supChange"
            >
            <el-option
              v-for="item in maintainsList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
              <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 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="19">
          <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";
import {genDeviceCode} from "@/api/dev/deviceInfoApi";

export default {
  props: {
    rowData: {
      type: Object,
      required: true
    },
    editSaveDev: {
      type: Function,
      required: true
    },
    maintainsList: {
      type: Array,
      required: true
    }
  },
  name: "deptDeviceModifyDialog",
  data() {
    return {
      deptList: [],
      invList: [],
      userList: [],
      formData: {},
      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: {
    supChange(val) {
      this.formData.supName = this.maintainsList.find(item => item.id == val).name
      this.formData.supId = val
    },
    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 {
          // this.rowData = this.formData;
          this.rowData.batchNo = this.formData.batchNo;
          this.rowData.deviceCode = this.formData.deviceCode;
          this.rowData.productId = this.formData.productId;
          this.rowData.productName = this.formData.productName;
          this.rowData.ggxh = this.formData.ggxh;
          this.rowData.serialNo = this.formData.serialNo;
          this.rowData.productionDate = this.formData.productionDate;
          this.rowData.expireDate = this.formData.expireDate;
          this.rowData.manufactory = this.formData.manufactory;
          this.rowData.measname = this.formData.measname;
          this.rowData.zczbhhzbapzbh = this.formData.zczbhhzbapzbh;
          this.rowData.supName = this.formData.supName;
          this.rowData.supId = this.formData.supId;
          this.rowData.remark = this.formData.remark;
          this.editSaveDev();
        }
      })
    },
    genCode(row) {
      let loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });
      genDeviceCode().then(res => {
        loading.close()
        if (res.code != 20000) {
          this.$message.error(res.message)
          return
        }
        this.formData.deviceCode = res.data
      }).catch(e => {
        loading.close()
      })
    },
  },
  created() {
    console.log(this.rowData);
    console.log(this.maintainsList.length);
    this.formData = JSON.parse(JSON.stringify(this.rowData));
    console.log(this.formData);
  },
}
</script>

<style scoped>

</style>
