import {deviceDetailInfo,deviceAllPage, deviceLogs, detailPage, genDeviceQR,detailByDeptCodePage} from "@/api/dev/deviceInfoApi";
import {deviceChangeStatus, deviceChangeType, deviceStatus} from "@/utils/enum";
import {downloadBase64Image} from "@/utils";
import deptDeviceDetail from "@/views/dev/DeptDeviceDetail.vue";
import {updateOrderItem } from "@/api/dev/deviceInfoApi";//保存到info
import {
  verifyTemplateFile,verifyTemplateFileLabel,
  printDev,
  excelInoutImport, postExcelInoutImport
} from '@/api/inout/orderDetailResult'

let query = {
    page: 1,
    limit: 10,
    productName: null,
    deviceCode: null,
    deptCode: null,
    status: null,
}

let detailQuery = {
    page: 1,
    limit: 10,
    deviceCode:null,
    deptCode: null,
    productId: null,
}

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 {
          disabled:false,
          deviceInfoFlag: false,
            locDeptCode: null,
            clickRow: null,
            showSearch: true,
            loading: false,
            total: .0,
            list: [],
            orderSelection: [],
            query: {...query},
            //设备详情=============================================
            detailLoading: false,
            detailQuery: {...detailQuery},
            detailList: [],
            detailTotal: 0,
            showLog: false,
            productRow: null,
            //===================================================
            logList: [],
        }
    },
    created() {
        this.locDeptCode = this.$store.getters.locDeptCode
        this.query.deptCode = this.$store.getters.locDeptCode
        this.getList()
    },
    methods: {

      //设备编辑保存
      editSaveDev(row) {
        // this.curDevRow = row;
        // console.log(this.curDevRow)
        updateOrderItem(this.curDevRow).then(res => {
          if (res.code != 20000) {
            this.$message.error(res.message)
            return
          }
          this.getList()
          this.$message.success(res.message)
        })
        // this.deviceInfoFlag = false;

      },
        downloadBase64Image,
        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()
        },
      rowClickDetail(row){
        this.deviceInfoFlag = false
        this.curDevRow = row;
        this.$emit('refresh');
        this.deviceInfoFlag = true
      },
        getDetailList() {
            this.detailLoading = true
            detailPage(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.query.page = 1
            this.getList()
        },
        onReset() {
            this.query = {...query}
          this.query.deptCode = this.$store.getters.locDeptCode
            this.getList()
        },
        getList() {
            this.loading = true
          if (this.query.deptCode == null){
            this.$message.warning("请选择所属科室！")
            this.loading = false
            return
          }
          deviceDetailInfo(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
            })
        },
      handleCheckedChange(val) {
        this.orderSelection = val;
      },
      optinPrint() {
        this.$confirm('是否确定打印选中科室设备, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            if (this.orderSelection == null || this.orderSelection.length <= 0) {
              return this.$message.error('请选择打印的明细！')
            }
            let query = { id: 5 }

            verifyTemplateFileLabel(query).then((res) => {
              if (res.code === 20000) {
                let printParams = {
                  deptCode: this.query.deptCode,
                  deviceCode: this.query.deviceCode,
                  productName: this.query.productName,
                  status: this.query.status,
                  list: this.orderSelection,
                  templateId: res.data
                }
                printDev(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 {
                return this.$message.error('摸版错误！')
              }
            })
          }).catch((error) => {
          this.loading = false
          // this.$message.error(error.message);
        })

      },
      selectPrint() {
        this.$confirm('是否确定打印所有查询结果数据', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            let query = { id: 5 }
            verifyTemplateFileLabel(query).then((res) => {
              if (res.code === 20000) {
                let printParams = {
                  deptCode: this.query.deptCode,
                  deviceCode: this.query.deviceCode,
                  productName: this.query.productName,
                  status: this.query.status,
                  templateId: res.data
                }
                printDev(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 {
                return this.$message.error('摸版错误！')
              }
            })
          })

        return

      },
      selectExport() {
        this.$confirm("是否确定导出选中出入库明细, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            if (this.orderSelection == null || this.orderSelection.length <= 0) {
              return this.$message.error("请选择需要导出的明细！");
            }
            let printParams = {
              list: this.orderSelection
            };
            postExcelInoutImport(printParams).then((response) => {
              let blob = new Blob([response], {type: "application/vnd.ms-excel"});
              let url = window.URL.createObjectURL(blob); // 创建一个临时的url指向blob对象
              let a = document.createElement("a");
              a.href = url;
              a.click();
              this.$message("成功")
            })
          });


      },
      searchExport() {
        this.$confirm("是否确定导出所有查询结果数据", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {

            let printParams = {
              orderIdFk: this.filterQuery.orderIdFk,
              nameCode: this.filterQuery.nameCode,
              coName: this.filterQuery.coName,
              spec: this.filterQuery.spec,
              batchNo: this.filterQuery.batchNo,
              zczbhhzbapzbh: this.filterQuery.zczbhhzbapzbh,
              manufacturer: this.filterQuery.manufacturer,
              actionType: this.filterQuery.actionType,
              keyWords: this.filterQuery.keyWords,
              mainAction: this.filterQuery.mainAction,
              action: this.filterQuery.action
            };

            if (this.filterQuery.actionType == 'preInDetail') {
              printParams.invCode = '1000001'
            } else if (this.filterQuery.actionType == 'preDetail') {
              printParams.invCode = '1000002'
            }

            if (this.actDateRange !== null) {
              printParams.startAduditTime = this.actDateRange[0];
              printParams.endAduditTime = this.actDateRange[1];
            } else {
              printParams.startAduditTime = null;
              printParams.endAduditTime = null;
            }
            excelInoutImport(printParams).then((response) => {
              let blob = new Blob([response], {type: "application/vnd.ms-excel"});
              let url = window.URL.createObjectURL(blob); // 创建一个临时的url指向blob对象
              let a = document.createElement("a");
              a.href = url;
              a.click();
              this.$message("成功")
            })

          });
      },
    },
  components:{
    deptDeviceDetail
  }
}
