import {detailByUserPage, deviceLogs, devicePage, genDeviceQR, printDevLabel} from "@/api/dev/deviceInfoApi";
import {deviceChangeStatus, deviceChangeType, deviceStatus} from "@/utils/enum";
import {downloadBase64Image} from "@/utils";
import {stockQRCodeTextPDFFromTemplateFile} from "@/api/itextpdf/itextpdf";

let query = {
  page: 1,
  limit: 10,
  productName: null,
  deviceCode: null,
  status: deviceStatus.NORMAL.key,
}

let detailQuery = {
  page: 1,
  limit: 10,
  deviceCode: null,
  deptCode: null,
  productId: null,
  productName: null,
  status: deviceStatus.NORMAL.key,
  checkLock: 0
}

export default {
  name: "deviceInfo",
  computed: {
    deviceChangeType() {
      return deviceChangeType
    },
    deviceStatus() {
      return deviceStatus
    },
    deviceChangeStatus() {
      return deviceChangeStatus
    }
  },
  props: {isChoose: {required: false, default: false, type: Boolean}, chooseFunc: {required: false, type: Function}},
  data() {
    return {
      maxFiles: 3, // 允许同时上传的最大文件数
      fileSizeLimit: 5, // 允许上传的文件大小限制（MB）
      uploadedFileNames: '', // 存储已上传文件名，逗号隔开

      locDeptCode: null,
      clickRow: null,
      showSearch: true,
      loading: false,
      total: .0,
      list: [],
      query: {...query},
      //设备详情=============================================
      detailLoading: false,
      detailQuery: {...detailQuery},
      detailList: [],
      detailTotal: 0,
      showLog: false,
      productRow: null,
      //===================================================
      logList: [],
      radioCheck : null,
      currentRow : null,
      multipleSelection: null,
      item:null,
    }
  },
  created() {
    this.locDeptCode = this.$store.getters.locDeptCode
    this.detailQuery.deptCode = this.locDeptCode
    this.getDetailList()
  },
  methods: {
    /**
     * 父组件处理子组件发送的数据更改
     * @param params
     */
    component1DataChange(params) {
      this.uploadedFileNames = params;
    },
    downloadBase64Image,
    tableRowClassName({row}) {
      if (row.checked) return "warning-row";
      return "";
    },
    push(){
      if (this.item == null){
        this.$message.error("请选择一个设备！")
        return
      }
      this.item.livePath = this.uploadedFileNames
      this.chooseFunc(this.item)
    },
    handleChange(val) {
      this.radioCheck = val.deviceCode;
      this.currentRow = val;
      this.item = val;
      this.multipleSelection = val
      this.uploadedFileNames = val.livePath
    },
    genQR(row) {
      genDeviceQR(row.deviceCode).then(res => {
        if (res.code != 20000) {
          this.$message.error(res.message)
          return
        }
        this.$set(row, 'QR', res.data)
      })
    },
    getDeviceLogs(row) {
      this.productRow = row
      deviceLogs(row.deviceCode).then(res => {
        if (res.code != 20000) {
          this.$message.error(res.message)
          return
        }
        this.logList = res.data || []
        this.showLog = true
      }).catch(e => {
      })
    },
    rowClick(row) {
      if (this.clickRow && row.deptCode == this.clickRow.deptCode && row.productId == this.clickRow.productId && row.status == this.clickRow.status && row.checkLock == this.clickRow.checkLock) {
        return false
      }
      this.clickRow = row
      this.detailQuery = {
        ...detailQuery,
        deptCode: row.deptCode,
        productId: row.productId,
        status: row.status,
        checkLock: row.checkLock
      }
      this.getDetailList()
    },
    getDetailList() {
      this.detailLoading = true
      detailByUserPage(this.detailQuery).then(res => {
        this.detailLoading = false
        if (res.code != 20000) {
          this.$message.error(res.message)
          return
        }
        this.detailList = res.data.list || []
        this.detailTotal = res.data.total || 0
      }).catch(e => {
        this.detailLoading = false
      })
    },
    search() {
      this.detailQuery.page = 1
      this.detailQuery.deptCode = this.locDeptCode
      this.getDetailList()
    },
    onReset() {
      this.detailQuery = {...detailQuery}
      this.detailQuery.deptCode = this.locDeptCode
      this.getDetailList()
    },
    getList() {
      this.loading = true
      devicePage(this.query).then(res => {
        this.loading = false
        if (res.code != 20000) {
          this.$message.error(res.message)
          return
        }
        this.list = res.data.list || []
        this.total = res.data.total || 0

      }).catch(e => {
        this.loading = false
      })
    },
    printDevLabel(row) {
      let tQuery = {
        labelId: 5,
        deviceCode: row.deviceCode
      };

      printDevLabel(tQuery).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。
      }).catch(() => {
        this.loading = false;
      });
    }

  }
}
