<template>
  <div>

    <el-tabs type="border-card" style="margin: 15px">
      <el-tab-pane label="基本信息">
          <deviceModifyDialog   v-if="viewCheckVisible "
            :rowData.sync="this.rowData"
            :editSaveDev="editSaveDev"
            :maintainsList = "this.maintainsList"
          >
          </deviceModifyDialog>
      </el-tab-pane>
      <el-tab-pane label="财务信息">
        <deviceFinanceModifyDialog v-if="viewCheckVisible "
          :rowData="this.rowData"
          :editSaveDev="editSaveDev"
          :maintainsList = "this.maintainsList"
        >
        </deviceFinanceModifyDialog>
      </el-tab-pane>
      <el-tab-pane label="附属信息">
        <deviceSubModifyDialog v-if="viewCheckVisible "
          :rowData="this.rowData"
          :editSaveDev="editSaveDev"
          :maintainsList = "this.maintainsList"
        >
        </deviceSubModifyDialog>
      </el-tab-pane>
      <el-tab-pane label="设备资质">
        <checkResultPanel v-if="viewCheckVisible "
            :idQuery="filterQuery"
            :rowData="this.rowData"
        ></checkResultPanel>
      </el-tab-pane>
      <el-tab-pane label="技术资料" >
        <technical v-if="viewCheckVisible "
            :idQuery="filterQuery"
            :rowData="this.rowData"
        ></technical>

      </el-tab-pane>
      <el-tab-pane label="相关人员" >
        <invDeviceUserEditDialog v-if="viewCheckVisible "
          :idQuery="filterQuery"
          :rowData="this.rowData">
        </invDeviceUserEditDialog>
      </el-tab-pane>

      <el-tab-pane label="维修记录" v-if="this.visible " >
        <invDeviceRepairEditDialog v-if="viewCheckVisible "
          :idQuery="filterQuery"
          :rowData="this.rowData">
        </invDeviceRepairEditDialog>
      </el-tab-pane>
      <el-tab-pane label="巡检记录" v-if="this.visible" >
        <invDeviceCheckEditDialog v-if="viewCheckVisible "
          :idQuery="filterQuery"
          :rowData="this.rowData">
        </invDeviceCheckEditDialog>
      </el-tab-pane>
      <el-tab-pane label="保养记录" v-if="this.visible" >
        <invDeviceUpkeepEditDialog v-if="viewCheckVisible "
          :idQuery="filterQuery"
          :rowData="this.rowData">
        </invDeviceUpkeepEditDialog>
      </el-tab-pane>

<!--      <el-tab-pane label="采购合同" v-if="this.visibleFull">-->
<!--        <invDevicePurContractDialog v-if="viewCheckVisible "-->
<!--                                  :idQuery="filterQuery"-->
<!--                                  :rowData="this.rowData"-->
<!--        >-->
<!--        </invDevicePurContractDialog>-->
<!--      </el-tab-pane>-->
      <el-tab-pane label="采购订单" v-if="this.visibleFull">
        <invDevicePurOrderDialog v-if="viewCheckVisible "
                                  :idQuery="filterQuery"
                                  :rowData="this.rowData"
        >
        </invDevicePurOrderDialog>
      </el-tab-pane>

<!--      <el-tab-pane label="申购记录" v-if="this.visibleFull">-->
<!--        <invDevicePurApplyDialog v-if="viewCheckVisible "-->
<!--                                  :idQuery="filterQuery"-->
<!--                                  :rowData="this.rowData"-->
<!--        >-->
<!--        </invDevicePurApplyDialog>-->
<!--      </el-tab-pane>-->

    </el-tabs>
    <el-dialog
        :title="formMap[formName]"
        :visible.sync="deviceDialogVisible"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        width="60%"
        v-if="deviceDialogVisible"
    >
      <deptDeviceModifyDialog
          :formData="formData"
          :closeDialog="closeDialog"
      ></deptDeviceModifyDialog>
    </el-dialog>

    <el-dialog
        title="设备巡检设置"
        :visible.sync="inspectSetVisible"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        width="60%"
        v-if="inspectSetVisible"
    >
      <inspectSetModifyDialog
          :code="deviceCode"
          :closeDialog="closeInspectSetDialog"
      ></inspectSetModifyDialog>
    </el-dialog>

    <el-dialog
        title="设备编辑"
        :visible.sync="isInformation"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        width="90%"
        v-if="isInformation"
    >
      <DeviceModelDetail
          :rowData="this.rowData"
          :closeDialog="closeInspectSetDialog"
      ></DeviceModelDetail>
    </el-dialog>

  </div>
</template>

<script>
import deviceModifyDialog from "@/views/dev/devInfoEdit.vue";
import deviceFinanceModifyDialog from "@/views/dev/deviceFinanceModifyDialog.vue";
import deviceSubModifyDialog from "@/views/dev/deviceSubModifyDialog.vue";
import {getInvListByUser} from "@/api/system/invWarehouse";
import {getDeptListByUser} from "@/api/auth/authDept";
import {getDeptDeviceList, deleteDeptDevice, printOrder, verifyTemplateFile} from "@/api/dev/deptDevice";
import deptDeviceModifyDialog from "@/views/dev/deptDeviceModifyDialog.vue";
import DeviceModelDetail from "@/views/dev/DeviceModelDetail.vue";
import inspectSetModifyDialog from "@/views/dev/InspectSetModifyDialog.vue";
import checkResultPanel from "@/views/dev/invDeviceEditDialog.vue";
import technical from "@/views/dev/invTechniclDialog.vue";
import invDeviceUserEditDialog from "@/views/dev/invDeviceUserEditDialog.vue";
import invDeviceUsageEditDialog from "@/views/dev/invDeviceUsageEditDialog.vue";
import invDevicePurContractDialog from "@/views/dev/invDevicePurContractDialog.vue";
import invDevicePurOrderDialog from "@/views/dev/invDevicePurOrderDialog.vue";
import invDevicePurApplyDialog from "@/views/dev/invDevicePurApplyDialog.vue";
import invDeviceInspectEditDialog from "@/views/dev/invDeviceInspectEditDialog.vue";
import invDeviceCheckEditDialog from "@/views/dev/invDeviceCheckEditDialog.vue";
import invDeviceRepairEditDialog from "@/views/dev/invDeviceRepairEditDialog.vue";
import invDeviceUpkeepEditDialog from "@/views/dev/invDeviceUpkeepEditDialog.vue";
import {getBasicUnitMaintains, getCorpList} from "@/api/basic/basicUnitMaintain";

export default {
  props: {
    rowData: {
      type: Object,
      required: true
    },
    editSaveDev: {
      type: Function,
      required: true
    },
    visible: {
      type: Boolean,
      required: true
    },
    visibleFull: {
      type: Boolean,
      required: true
    }
  },
  name: "DeptDeviceDetail",
  data() {
    return {
      selectedRowData: {},
      viewCheckVisible: false,
      filterQuery: {
        deptCode: this.$store.getters.locDeptCode,
        deviceName: null,
        devCodeFk: null,
        code: null,
        ggxh: null,
        batchNo: null,
        status: 1,
        addType: 1,
        page: 1,
        limit: 10,
      },
      maintainsList: [],
      list: [],
      total: 0,
      deptList: [],
      invList: [],
      isInformation: false,
      loading: false,
      fromOptions: [],
      formData: {
        id: null,
        deptCode: null,
        invCode: null,
        originCode: null,
        code: null,
        deviceName: null,
        ggxh: null,
        batchNo: null,
        serialNo: null,
        productionDate: null,
        expireDate: null,
        manufactory: null,
        measname: null,
        zczbhhzbapzbh: null,
        addType: null,
        supName: null,
        manager: null,
        remark: null
      },
      deviceDialogVisible: false,
      showSearch: true,
      statusMap: [
        {
          label: '正常',
          value: 1
        },
        {
          label: '停用',
          value: 2
        },
        {
          label: '报废',
          value: 3
        },
      ],
      status: {
        1: "正常",
        2: "停用",
        3: "报废",
      },
      addTypeMap: [
        {
          label: "领用添加",
          value: 1
        },
        {
          label: "手动添加",
          value: 2
        }
      ],
      formMap: {
        add: "添加设备",
        edit: "编辑设备信息"
      },
      formName: "add",
      inspectSetVisible: false,
      deviceCode: null
    };
  },
  beforeDestroy() {
    // 移除监听器，防止内存泄露
    this.$parent.$off('refresh', this.refreshComponentData);
  },
  methods: {
    refreshComponentData() {
      this.rowData = this.rowData
    },
    getBasicUnitMaintainsList() {
      getCorpList()
        .then((response) => {
          this.maintainsList = response.data || [];
        })
        .catch(() => {
          this.maintainsList = [];
        });

    },
    listClick(row) {
      this.selectedRowData = row // 将选中的行数据保存到 selectedRow 变量中
    },
    keyup_submit(event) {
      this.filterQuery.page = 1;
      this.getList();
      event.target.select();
    },

    hideSearch() {
      this.showSearch = !this.showSearch;
    },
    onReset() {
      this.$router.push({
        path: "",
      });
      this.filterQuery = {
        deptCode: this.$store.getters.locDeptCode,
        deviceName: null,
        devCodeFk: null,
        code: null,
        ggxh: null,
        batchNo: null,
        status: 1,
        addType: 1,
        page: 1,
        limit: 10,
      };
      this.selectedRowData = null;
      this.getDeptList();
    },
    onSubmit() {
      this.filterQuery.page = 1;
      this.getList();
    },
    getList() {
      // this.loading = true;
      // getDeptDeviceList(this.filterQuery).then((res) => {
      //   this.loading = false;
      //   if (res.code === 20000) {
      //     this.list = res.data.list || [];
      //     this.total = res.data.total || 0;
      //   } else {
      //     this.$message.error(res.message);
      //     this.list = [];
      //     this.total = 0;
      //   }
      // }).catch((error) => {
      //   this.loading = false;
      //   this.$message.error(error.message);
      //   this.list = [];
      //   this.total = 0;
      // })
    },
    deptChange() {
      this.filterQuery.invCode = null;
      this.invList = [];
      this.getInvList();
    },
    getDeptList() {
      getDeptListByUser().then((res) => {
        this.deptList = res.data || [];
        this.getInvList();
        this.getList();
      });
    },
    invChange() {
      this.getList();
    },
    getInvList() {
      let params = {deptCode: this.filterQuery.deptCode};
      getInvListByUser(params)
          .then((response) => {
            this.invList = response.data || [];
          })
          .catch(() => {
          });
    },
    editOrder(row) {
      this.selectedRowData = row //
      this.isInformation = true
    },
    addDevice() {
      this.formName = 'add';
      this.deviceDialogVisible = true;
      this.formData = {
        id: null,
        deptCode: this.$store.getters.locDeptCode,
        invCode: null,
        originCode: null,
        code: null,
        deviceName: null,
        ggxh: null,
        batchNo: null,
        serialNo: null,
        productionDate: null,
        expireDate: null,
        manufactory: null,
        measname: null,
        zczbhhzbapzbh: null,
        addType: 2,
        supName: null,
        manager: null,
        remark: null
      };
    },
    deleteDevice(id) {
      this.$confirm('此操作将从部门设备列表中删除此设备, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let params = {
          id: id
        };
        deleteDeptDevice(params).then((res) => {
          if (res.code === 20000) {
            this.$message.success("删除成功");
            this.getList();
          } else {
            this.$message.error(res.message);
          }
        })
      }).catch((error) => {
        this.$message.error(error.message);
      });
    },
    editDialog(row) {
      this.formName = 'edit';
      this.deviceDialogVisible = true;
      this.formData = {
        id: row.id,
        deptCode: row.deptCode,
        invCode: row.invCode,
        originCode: row.originCode,
        code: row.code,
        deviceName: row.deviceName,
        ggxh: row.ggxh,
        batchNo: row.batchNo,
        serialNo: row.serialNo,
        productionDate: row.productionDate,
        expireDate: row.expireDate,
        manufactory: row.manufactory,
        measname: row.measname,
        zczbhhzbapzbh: row.zczbhhzbapzbh,
        addType: row.addType,
        supName: row.supName,
        manager: row.manager,
        remark: row.remark
      }
    },
    closeDialog() {
      this.deviceDialogVisible = false;
      this.getList();
    },
    inspectSet(code) {
      this.inspectSetVisible = true;
      this.deviceCode = code;
    },
    closeInspectSetDialog() {
      this.inspectSetVisible = false;
    },
    printOrder(row) {
      let query = {id: 5};
      this.loading = true;
      verifyTemplateFile(query).then((res) => {
        if (res.code === 20000) {
          let printParams = {
            templateId: res.data,
            id: row.id
          };
          printOrder(printParams).then((response) => {
            //将pdf文件转换为url。
            const binaryData = [];
            binaryData.push(response);
            //获取blob链接。
            let url = window.URL.createObjectURL(
                new Blob(binaryData, {type: "application/pdf"})
            );
            this.loading = false;
            window.open(url);//打开新标签页，预览pdf。
          })
        } else {
          this.loading = false;
          this.$message.error(res.message);
        }
      }).catch((error) => {
        this.loading = false;
        this.$message.error(error.message);
      })
    },
  },
  mounted() {
    document.body.ondrop = function (event) {
      event.preventDefault();
      event.stopPropagation();
    };
  },
  components: {
    checkResultPanel,
    technical,
    deptDeviceModifyDialog,
    inspectSetModifyDialog,
    DeviceModelDetail,
    deviceModifyDialog,
    deviceFinanceModifyDialog,
    invDeviceUserEditDialog,
    invDeviceUsageEditDialog,
    invDeviceInspectEditDialog,
    invDeviceCheckEditDialog,
    deviceSubModifyDialog,
    invDevicePurContractDialog,
    invDevicePurOrderDialog,
    invDevicePurApplyDialog,
    invDeviceRepairEditDialog,
    invDeviceUpkeepEditDialog
  },
  created() {
    this.filterQuery = this.rowData
    this.filterQuery.devCodeFk = this.rowData.deviceCode
    this.getDeptList();
    this.getBasicUnitMaintainsList();
    this.viewCheckVisible = true
  },
  watch: {
    rowData: {
      handler: function(newVal) {
        this.rowData = newVal;
        this.viewCheckVisible = false
        this.$nextTick(() => {
          //重新加载组件
          this.viewCheckVisible = true
        });
      },
      deep: true
    }
  }
};
</script>

<style type="text/scss" lang="scss">
</style>

