import {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";

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:true,
          deviceInfoFlag: false,
            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: [],
        }
    },
    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
          }
          detailByDeptCodePage(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
            })
        }
    },
  components:{
    deptDeviceDetail
  }
}
