import {detailByUserPage, deviceLogs, devicePage} from "@/api/dev/deviceInfoApi";
import {deviceChangeStatus, deviceChangeType, deviceStatus} from "@/utils/enum";
import {
  deviceCheckPage,
  deviceCheckPageByDept,
  deviceCheckPrint,
  deviceCheckPrintByDeviceCode
} from "@/api/dev/deviceCheckApi";
import {deviceCheckDetailPage, deviceCheckDetailRepair} from "@/api/dev/deviceCheckDetailApi";
import {deviceCheckDetailItemFinish, deviceCheckDetailItemPage} from "@/api/dev/deviceCheckDetailItemApi";
import {copyProperties, getLoading} from "@/utils";

let query = {
  page: 1,
  limit: 10,
  chargeDeptCode: null,
  finishFlag: null,
}

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

let itemQuery = {
  page: 1,
  limit: 10,
  taskId: null,
  deviceCode: null,
}
let repairData = {
  taskId: null,
  deviceCode: null,
  description: null,
  diagnosisInfo: null,
  innerFlag: true,
  repairUserName: null,
  repairUserPhone: null,
}

export default {
  name: "deptDeviceCheck",
  computed: {},
  data() {
    return {
      clickRow: null,
      showSearch: true,
      loading: false,
      total: .0,
      list: [],
      query: {...query},
      //设备详情=============================================
      detailLoading: false,
      detailQuery: {...detailQuery},
      detailList: [],
      detailTotal: 0,
      showLog: false,
      //===================================================
      itemDialogFlag: false,
      itemQuery: {...itemQuery},
      itemList: [],
      itemTotal: 0,
      itemLoading: false,
      checkPrintData: null,
      itemRow: null,
      itemRules: {
        normalFlag: [{required: true, message: "请选择异常情况", trigger: ["change", "blur"]}],
        suggestion: [{required: true, message: "巡检意见不能为空", trigger: ["change", "blur"]}],
      },
      repairRow: null,
      repairRules: {
        innerFlag: [{
          required: true,
          message: "请选择维修方式",
          trigger: ["change", "blur"]
        }],
        repairUserName: [{
          required: true,
          message: "维修人姓名不能为空",
          trigger: ["change", "blur"]
        }],
        repairUserPhone: [{
          required: true,
          message: "维修人电话不能为空",
          trigger: ["change", "blur"]
        }],
        description: [{required: true, message: "问题描述不能为空", trigger: ["change", "blur"]}],
        diagnosisInfo: [{required: true, message: "诊断信息不能为空", trigger: ["change", "blur"]}],
      },
      repairId:null,
    }
  },
  created() {
    this.getList()
  },
  methods: {
    commitRepair(){
      this.$refs.repairForm.validate(b => {
        if (!b) {
          return
        }
        let load = getLoading(this)
        deviceCheckDetailRepair(this.repairRow).then(res => {
          load.close()
          if (res.code != 20000) {
            this.$message.error(res.message)
            return
          }
          this.$message.success(res.message)
          this.getDetailList()
          this.getList()
          this.repairRow = null
        }).catch(() => {
          load.close()
        })
      })
    },
    createRepair(row) {
      let data = copyProperties(row, {...repairData});
      this.repairRow = _.extend({}, row, data)
    },
    commitItemCheck() {
      this.$refs.itemForm.validate(b => {
        if (!b) {
          return
        }
        let load = getLoading(this)
        deviceCheckDetailItemFinish(this.itemRow).then(res => {
          load.close()
          if (res.code != 20000) {
            this.$message.error(res.message)
            return
          }
          this.$message.success(res.message)
          this.getDetailItemList()
          this.getDetailList()
          this.getList()
          this.itemRow = null
        }).catch(() => {
          load.close()
        })
      })
    },
    openItem(row) {
      this.itemQuery = {
        ...itemQuery,
        taskId: row.taskId,
        deviceCode: row.deviceCode,
        title: `[${row.deviceCode}]--${row.productName}(${row.nameCode})`
      }
      this.itemDialogFlag = true
      this.getDetailItemList()
    },
    getDetailItemList() {
      this.itemLoading = true
      deviceCheckDetailItemPage(this.itemQuery).then(res => {
        this.itemLoading = false
        if (res.code != 20000) {
          this.$message.error(res.message)
          return
        }
        this.itemList = res.data.list || []
        this.itemTotal = res.data.total || 0
      }).catch(e => {
        this.itemLoading = false
        this.itemList = []
        this.itemTotal = 0
      })
    },
    rowClick(row) {
      if (this.clickRow && row.taskId == this.clickRow.taskId) {
        return false
      }
      this.clickRow = row
      this.detailQuery = {...detailQuery, taskId: row.taskId}
      this.getDetailList()
    },
    getDetailList() {
      this.detailLoading = true
      deviceCheckDetailPage(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.getList()
    },
    getList() {
      this.loading = true
      deviceCheckPageByDept(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.list = []
        this.total = 0
        this.loading = false
      })
    }
  }
}
