import {getLoading, copyProperties} from "@/utils"
import {deviceRepairApplyPage} from "@/api/dev/deviceRepairApplyApi"
import {deviceRepairApplyAdd} from "../../../api/dev/deviceRepairApplyApi";
import {deviceRepairApplyDetailStatus, deviceRepairApplyStatus} from "../../../utils/enum";
import {deviceRepairApplyDetailPage} from "../../../api/dev/deviceRepairApplyDetailApi";

let query = {
  page: 1,
  limit: 10,
}

let saveData = {
  applyUserPhone: null,
  details: []
}

let detailData = {
  deviceCode: null,
  description: null,
}

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

export default {
  name: "deviceRepairApply",
  computed: {
    deviceRepairApplyDetailStatus() {
      return deviceRepairApplyDetailStatus
    },
    deviceRepairApplyStatus() {
      return deviceRepairApplyStatus
    }
  },
  data() {
    return {
      maxFiles: 3, // 允许同时上传的最大文件数
      fileSizeLimit: 5, // 允许上传的文件大小限制（MB）
      uploadedFileNames: '', // 存储已上传文件名，逗号隔开

      showSearch: true,
      loading: false,
      total: .0,
      list: [],
      query: {...query},
      createFlag: false,
      saveData: _.cloneDeep(detailQuery),
      user: null,
      formRule: {
        applyDeptCode: [{required: true, message: "报修部门不能为空", trigger: ["change", "blur"]}],
        applyUserPhone: [{required: true, message: "报修人联系方式不能为空", trigger: ["change", "blur"]}],
        details: [{
          required: true,
          validator: this.validToDetails,
          message: "报修设备不能为空",
          trigger: ["change", "blur"]
        }],
      },
      detailRules: {
        description: [{required: true, message: "问题描述不能为空", trigger: ["change", "blur"]}],
      },
      chooseDeviceFlag: false,
      //========detail===============================
      clickRow: null,
      detailQuery: _.cloneDeep(detailQuery),
      detailLoading: false,
      detailList: [],
      detailTotal: 0,
      repairId: null,
      submitLoading: false,
      //========detail--end===============================
    }
  },
  created() {
    this.user = this.$store.getters.user
    this.getList()
  },
  methods: {
    rowClick(row) {
      this.clickRow = row
      this.detailQuery = _.cloneDeep({...detailQuery, applyId: row.id})
      this.getDetailList()
    },
    getDetailList() {
      this.detailLoading = true
      deviceRepairApplyDetailPage(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
      })
    },
    removeDetail(i) {
      this.saveData.details.splice(i, 1)
    },
    chooseDevice(row) {
      // let data = {...row, description: ""}
      let i = this.saveData.details.findIndex(i => i.deviceCode == row.deviceCode)
      if (i != -1) {
        this.$message.error("该设备已被选入,无法再次选择")
        return
      }

      this.saveData.details.push(row)
      this.chooseDeviceFlag = false
    },
    validToDetails(rule, value, callback) {
      if (!this.saveData.details || this.saveData.details.length == 0) {
        callback(new Error("报修设备不能为空"))
      }
      callback()
    },
    saveFunc: function () {
      let that = this
      this.$refs.saveForm.validate(b => {
        if (!b) {
          return false
        }
        let success = true
        this.saveData.details.forEach(i => {
          that.$refs[`${i.deviceCode}`][0].validate(s => {
            if (!s) {
              success = false
              return false
            }
          })
        })
        if (!success) {
          return false
        }
        let loading = getLoading(that);
        this.submitLoading = true
        deviceRepairApplyAdd(this.saveData).then(res => {
          this.submitLoading = false
          loading.close()
          if (res.code != 20000) {
            this.$message.error(res.message)
            return
          }
          this.$message.success(res.message)
          this.createFlag = false
          this.query.page = 1
          this.getList()
        }).catch(() => {
          this.submitLoading = false
          loading.close
        })
      })
    },
    openCreate() {
      this.saveData = _.cloneDeep(saveData)
      this.createFlag = true
    },
    search() {
      this.query.page = 1
      this.getList()
    },
    onReset() {
      this.query = {...query}
      this.getList()
    },
    getList() {
      this.loading = true
      deviceRepairApplyPage(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
      })
    }
  }
}
