import {getLoading, copyProperties} from "@/utils"
import {
  deviceRepairApplyHallPage,
  deviceRepairApplyConfirmByUserPage,
  confirmDeviceRepairApply
} from "@/api/dev/deviceRepairApplyApi";
import {deviceRepairApplyDetailStatus, deviceRepairApplyStatus} from "@/utils/enum";
import {deviceRepairApplyDetailDiagnosis, deviceRepairApplyDetailPage} from "@/api/dev/deviceRepairApplyDetailApi";
import {deviceRepairInfo} from "@/api/dev/deviceRepairApi";
import {filterListByDeptCode} from "@/api/system/deptUser";

let query = {
  page: 1,
  limit: 10,
  applyId: null,
  isFinish: 0,
  status: null,
  applyDeptCode: null,
}

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

let diagnosisData = {
  applyId: null,
  deviceCode: null,
  repairFlag: true,
  innerFlag: true,
  repairDeptCode: null,
  repairUserId: '',
  repairUserName: '',
  repairUserPhone: '',
  diagnosisInfo: '',
  diagnosisLivePath: '',
}

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

      tabActive: 1,
      showSearch: true,
      loading: false,
      total: 0,
      list: [],
      userlist: [],
      query: {...query},
      user: null,
      chooseDeviceFlag: false,
      //========detail===============================
      clickRow: null,
      detailQuery: _.cloneDeep(detailQuery),
      detailLoading: false,
      detailList: [],
      detailTotal: 0,
      //========detail--end===============================
      repairId: null,
      //========detailDialog===============================
      detailFlag: false,
      detailData: null,
      diagnosisData: _.cloneDeep(diagnosisData),
      detailRules: {
        repairFlag: [{required: true, message: "请选择是否维修", trigger: ["change", "blur"]}],
        innerFlag: [{
          required: true,
          validator: this.innerFlagValid,
          message: "请选择维修方式",
          trigger: ["change", "blur"]
        }],
        repairDeptCode: [{
          required: true,
          validator: this.repairDeptCodeValid,
          message: "维修部门不能为空",
          trigger: ["change", "blur"]
        }],
        repairUserId: [{
          required: true,
          validator: this.repairUserNameValid,
          message: "维修人姓名不能为空",
          trigger: ["change", "blur"]
        }],
        repairUserPhone: [{
          required: true,
          validator: this.repairUserPhoneValid,
          message: "维修人电话不能为空",
          trigger: ["change", "blur"]
        }],
        diagnosisInfo: [{required: true, message: "诊断信息不能为空", trigger: ["change", "blur"]}],
      },
      submitLoading: false
      //========detailDialog--end===============================
    }
  }
  ,
  watch: {
    'diagnosisData.repairDeptCode': {
      handler (newV, oldV) {
        this.getUerList()
      },
      // deep: true,
      // immediate: true,
    }
  },
  created() {
    this.user = this.$store.getters.user
    this.getList()
  },
  methods: {
    /**
     * 父组件处理子组件发送的数据更改
     * @param params
     */
    component1DataChange(params) {
      this.uploadedFileNames = params;
    },
    repairInfo(row) {
      this.repairId = row.repairId
    },
    handleEmployeeSelect(value){
        this.diagnosisData.repairUserName = this.userlist.find(user => user.userId == value).employeeName;
    },
    commitFunc() {
      this.$refs.saveForm.validate(b => {
        if (!b) {
          return
        }
        this.diagnosisData.diagnosisLivePath = this.uploadedFileNames
        this.submitLoading = true
        deviceRepairApplyDetailDiagnosis(this.diagnosisData).then(res => {
          this.submitLoading = false
          if (res.code != 20000) {
            this.$message.error(res.message)
            return
          }
          this.$message.success(res.message)
          this.detailFlag = false
          this.getList()
        })
      })
    },
    repairUserPhoneValid(rule, value, callback) {
      if (this.diagnosisData?.repairFlag) {
        if (!this.diagnosisData.repairUserPhone) {
          callback(new Error("维修人电话不能为空"))
        }
      }
      callback()
    },
    repairUserNameValid(rule, value, callback) {
      if (this.diagnosisData?.repairFlag) {
        if (!this.diagnosisData.repairUserId) {
          callback(new Error("维修人姓名不能为空"))
        }
      }
      callback()
    },
    repairDeptCodeValid(rule, value, callback) {
      if (this.diagnosisData?.repairFlag) {
        if (!this.diagnosisData.repairDeptCode) {
          callback(new Error("维修部门不能为空"))
        }
      }
      callback()
    },
    innerFlagValid(rule, value, callback) {
      if (this.diagnosisData?.repairFlag) {
        if (this.diagnosisData.innerFlag == null) {
          callback(new Error("请选择维修方式"))
        }
      }
      callback()
    },
    openDiagnosisDialog(row) {
      this.detailFlag = true
      this.detailData = _.cloneDeep(row)
      this.uploadedFileNames = row.diagnosisLivePath
      this.diagnosisData = _.cloneDeep({...diagnosisData, applyId: row.applyId, deviceCode: row.deviceCode})
    },
    confirm(row) {
      this.$prompt("请输入受理人电话", "提示", {
        closeOnClickModal: false,
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputType: String,
        inputPlaceholder: '请输入受理人电话',
      }).then(({value}) => {
        let data = {
          applyId: row.id,
          confirmUserPhone: value
        }
        confirmDeviceRepairApply(data).then(res => {
          if (res.code != 20000) {
            this.$message.error(res.message)
            return
          }
          this.$message.success(res.message)
          this.getList()
        })
      })
    },
    tabClick(v, ov) {
      if (v == ov) {
        return
      }
      this.tabActive = v
      this.query = _.cloneDeep(query)
      this.getList()
    },
    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
      })
    },
    search() {
      this.query.page = 1
      this.getList()
    },
    onReset() {
      this.query = {...query}
      this.getList()
    },
    getList() {
      this.loading = true
      this.clickRow = null
      this.detailList = []
      this.detailTotal = 0
      if (this.tabActive == 1) {
        deviceRepairApplyHallPage(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
          this.list = res.data.list || []
          this.total = res.data.total || 0
        })
      } else {
        deviceRepairApplyConfirmByUserPage(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
          this.list = res.data.list || []
          this.total = res.data.total || 0
        })
      }
    },
    getUerList() {
      if (this.diagnosisData.repairDeptCode != null){
        let userQuery = {
          deptCode: this.diagnosisData.repairDeptCode,
        }
        filterListByDeptCode(userQuery).then((response) => {
          // this.loading = false;
          this.userlist = response.data.list || [];
          // this.userTotal = response.data.total;
        })
          .catch(() => {
            // this.loading = false;
            this.userlist = [];
            // this.userTotal = 0;
          });
      }
    }

  }
}
