<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 20px 30px 85%; height: 35px;">
        <el-button
          type="primary"
          @click.native="submit()"
        >保存
        </el-button>
      </el-button-group>

      <!--      <el-row>-->
      <!--        <el-col :span="12">-->
      <!--          <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="12">-->
      <!--          <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="12">
          <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="12">
          <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="12">
          <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="12">
          <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="12">
          <el-form-item  class="query-form-item"  label="批次号:">
            <el-input v-model="formData.batchNo" placeholder="请输入批次号" style="width: 90%" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item  class="query-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="12">
          <el-form-item  class="query-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="12">
          <el-form-item  class="query-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="12">
          <el-form-item  class="query-form-item"  label="生产厂家:">
            <el-input v-model="formData.manufactory" placeholder="请输入生产厂家" style="width: 90%"
                      clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item  class="query-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="12">
          <el-form-item  class="query-form-item"  label="注册证号:">
            <el-input v-model="formData.zczbhhzbapzbh" placeholder="请输入注册备案凭证号" style="width: 90%"
                      clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item  class="query-form-item"  label="供应商:" prop="supId" style="width: 91%">
<!--            <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="12" >
          <el-form-item class="query-form-item"  label="医保编码:" prop="ybbm">
            <el-input style="width: 90%" size="small" placeholder="请输入医保编码" v-model.trim="formData.ybbm" v-debounce-input:500ms="getYbHcData" ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12" >
          <el-form-item class="query-form-item"  label="一级分类名称:" prop="catalogname1">
            <el-input style="width: 90%" size="small" placeholder="请输入一级分类名称" v-model.trim="formData.catalogname1" disabled></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12" >
          <el-form-item class="query-form-item"  label="二级分类名称:" prop="catalogname2">
            <el-input style="width: 90%" size="small" placeholder="请输入二级分类名称" v-model.trim="formData.catalogname2" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12" >
          <el-form-item class="query-form-item"  label="三级分类名称:" prop="catalogname3">
            <el-input style="width: 90%" size="small" placeholder="请输入三级分类名称" v-model.trim="formData.catalogname3" disabled></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <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";
import {getYbHcflDetail,disableUdi, removeDiRl, removeRl, thirdSysDetail, updatetSingleUdiInfo} from "@/api/basic/product/udiRelevance";

export default {
  props: {
    rowData: {
      type: Object,
      required: true
    },
    editSaveDev: {
      type: Function,
      required: true
    },
    maintainsList: {
      type: Array,
      required: true
    }
  },
  name: "deptDeviceModifyDialog",
  dicts: ['device_source_type'],
  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: {
    getYbHcData(){
      let query = {
        specificationCode: this.formData.ybbm
      };
      getYbHcflDetail(query).then((response) => {
        if (response.code == 20000) {
          this.ybHcflDetail = response.data
          this.formData.catalogname1 = this.ybHcflDetail.catalogname1
          this.formData.catalogname2 = this.ybHcflDetail.catalogname2
          this.formData.catalogname3 = this.ybHcflDetail.catalogname3
          this.$forceUpdate();
        }else {
          this.formData.catalogname1 = ""
          this.formData.catalogname2 = ""
          this.formData.catalogname3 = ""
          this.$forceUpdate();
        }
      }).catch(() => {
      })

    },
    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.rowData.ybbm = this.formData.ybbm;
          this.rowData.catalogname1 = this.formData.catalogname1;
          this.rowData.catalogname2 = this.formData.catalogname2;
          this.rowData.catalogname3 = this.formData.catalogname3;
          this.rowData.catalogCode = this.formData.catalogCode;
          this.rowData.catalogCode1 = this.formData.catalogCode1;
          this.rowData.catalogCode2 = this.formData.catalogCode2;
          this.rowData.catalogCode3 = this.formData.catalogCode3;
          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()
      })
    },
  },
  directives: {
    'debounce-input': {
      inserted(el, binding) {
        let timeout
        el.addEventListener('input', () => {
          clearTimeout(timeout)
          timeout = setTimeout(() => {
            binding.value()
          }, parseInt(binding.arg) || 300)
        })
      }
    }
  },
  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>
