import DeviceCheckItemDict from '@/views/dev/deviceCheckItemDict'
import product from '@/views/basic/product/product'
import DevicePlanProject from '@/views/dev/devicePlanProject'
import {
  devicePlanDetailDel,
  devicePlanDetailDelByDeviceCode,
  devicePlanDetailPage
} from '@/api/dev/devicePlanDetailApi'
import { getLoading } from '@/utils'
import { deviceCheckGenByPlanId } from '@/api/dev/deviceUpkeepApi'
import { deviceUpkeepPlanDel, devicePlanInfo, deviceUpkeepPlanPage, devicePlanSave } from '@/api/dev/deviceUpkeepPlanApi'
import {
  devicePlanDetailItemAdd,
  devicePlanDetailItemByDeviceCodeDel,
  devicePlanDetailItemDel,
  devicePlanDetailItemPage
} from '@/api/dev/devicePlanDetailItemApi'
import devicePlanEdit from "@/views/dev/devicePlanEdit";

let query = {
  page: 1,
  limit: 10,
  name: null,
  chargeDeptCode: null,
  type:2,
}

let detailQuery = {
  page: 1,
  limit: 10,
  planId: null,
}
let rowDeltailQuery = {
  page: 1,
  limit: 10,
  planId: null,
  productId: null,
}

let itemQuery = {
  page: 1,
  limit: 10,
  planId: null,
  productId: null,
}

let planData = {
  name: null,
  chargeDeptCode: null,
  startDate: null,
  endDate: null,
  frequency: 1,
  dateInterval: null,
  remark: null,
  type:2,
}

export default {
  name: "deviceUpkeepPlan",
  components: { product, devicePlanEdit, DevicePlanProject},
  computed: {},
  data() {
    return {
      clickRow: null,
      showSearch: true,
      loading: false,
      total: .0,
      list: [],
      query: {...query},
      //计划设备明细=============================================
      detailLoading: false,
      detailQuery: {...detailQuery},
      detailList: [],
      detailTotal: 0,
      //巡检项目=============================================
      itemQuery: {...itemQuery},
      itemList: [],
      itemTotal: 0,
      itemLoading: false,
      chooseDetailItemFlag: false,
      chooseDeviceList: [],
      chooseItemList: [],
      //新增/编辑计划===================================================
      planEditTitle: "新增保养计划",
      createActive: 0,
      createFlag: false,
      chooseDeviceFlag: false,
      expandRowKeys: [],
      dialogExpandRowKeys: [],
      planData: {...planData},
      formRule: {
        name: [{required: true, message: "计划名称不能为空", trigger: ["change", "blur"]}],
        chargeDeptCode: [{required: true, message: "负责部门不能为空", trigger: ["change", "blur"]}],
        dateInterval: [{required: true, message: "计划执行区间不能为空", trigger: ["change", "blur"]}],
        frequency: [{
          required: true,
          message: "计划执行频率不能为空",
          trigger: ["change", "blur"]
        }],
      },
      dateInterval: null,
      pickerOptions: {
        disabledDate: time => {
          let nowDate = new Date().getTime();
          let pickDate = new Date(time).getTime();
          return (nowDate >= pickDate);
        },
        shortcuts: [{
          text: '一个月(30天)',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() + 3600 * 1000 * 24);
            end.setTime(start.getTime() + 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '三个月(90天)',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() + 3600 * 1000 * 24);
            end.setTime(start.getTime() + 3600 * 1000 * 24 * 30 * 3);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '半年(180天)',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() + 3600 * 1000 * 24);
            end.setTime(start.getTime() + 3600 * 1000 * 24 * 30 * 6);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '一年(365天)',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() + 3600 * 1000 * 24);
            end.setTime(start.getTime() + 3600 * 1000 * 24 * 365);
            picker.$emit('pick', [start, end]);
          }
        }]
      },

      projectVisible: false,
      projectItem: null,
    }
  },
  watch: {
    planData(v) {
      if (v.planId != null) {
        this.$set(v, "dateInterval", [v.startDate, v.endDate])
      }
    },
    "planData.dateInterval"(v) {
      if (v != null && v.length > 0) {
        this.planData.startDate = v[0]
        this.planData.endDate = v[1]
      }
    },
    "$refs.dialogTable"(v) {

      this.$refs.dialogTable?.doLayout()
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleExpendRow(row, expandedRows) {
      if (expandedRows.length == 0) {
        return
      }
      this.$set(row, "detailData", {
        loading: false,
        showSearch: true,
        list: [],
        total: 0,
        query: {...rowDeltailQuery, planId: row.planId, productId: row.productId}
      })
      this.getRowDetailList(row.detailData)
    },
    getRowDetailList(data) {
      data.loading = true
      devicePlanDetailPage(data.query).then(res => {
        data.loading = false
        if (res.code != 20000) {
          this.$message.error(res.message)
          return
        }
        data.list = res.data.list || []
        data.total = res.data.total || 0

      }).catch(() => {
        data.loading = false
        data.list = []
        data.total = 0
      })
    },
    getDetailList() {
      this.detailLoading = true
      this.expandRowKeys = []
      this.dialogExpandRowKeys = []
      devicePlanDetailPage(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.detailList = []
        this.detailTotal = 0
        this.detailLoading = false
      })
    },
    deviceCheckGenFunc(row) {
      this.$confirm("此操作将会创建一个任务单，是否继续", "提示", {
        confirmButtonText: "继续",
        cancelButtonText: "取消"
      }).then(() => {
        let load = getLoading(this)
        deviceCheckGenByPlanId(row.planId).then(res => {
          load.close()
          if (res.code != 20000) {
            this.$message.error(res.message)
            return
          }
          this.$message.success(res.message)
          this.getList()

        }).catch(() => {
          load.close()
        })
      })
    },
    delplan(row) {
      this.$confirm("此操作将会永久删除该计划，现有任务不受影响，是否继续", "提示", {
        confirmButtonText: "继续",
        cancelButtonText: "取消"
      }).then(() => {
        let load = getLoading(this)
        deviceUpkeepPlanDel(row.planId).then(res => {
          load.close()
          if (res.code != 20000) {
            this.$message.error(res.message)
            return
          }
          this.$message.success(res.message)
          this.query.page = 1
          this.getList()
          if (this.clickRow && this.clickRow.planId == row.planId) {
            this.clickRow = null
            this.detailTotal = 0
            this.detailList = []
          }
        }).catch(() => {
          load.close()
        })
      })
    },
    // openItem(row) {
    //   this.itemQuery = {
    //     ...itemQuery,
    //     planId: row.planId,
    //     productId: row.productId,
    //     deviceCode: row.deviceCode,
    //     title: `${row.deviceCode ? row.deviceCode + '--' : ''}${row.productName}(${row.nameCode})`
    //   }
    //   this.getDetailItemList()
    // },
    // getDetailItemList() {
    //   this.itemLoading = true
    //   devicePlanDetailItemPage(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.itemList = []
    //     this.itemTotal = 0
    //     this.itemLoading = false
    //   })
    // },
    // // delDetailItem(row) {
    //   this.$confirm("此操作将会永久删除该条明细，且将在下一次任务生效，现有任务不受影响，是否继续", "提示", {
    //     confirmButtonText: "继续",
    //     cancelButtonText: "取消"
    //   }).then(() => {
    //     let load = getLoading(this)
    //     if (!row.deviceCode) {
    //       devicePlanDetailItemDel(row.planId, row.productId, row.itemCode).then(res => {
    //         load.close()
    //         if (res.code != 20000) {
    //           this.$message.error(res.message)
    //           return
    //         }
    //         this.$message.success(res.message)
    //         this.itemQuery.page = 1
    //         this.getDetailItemList()
    //       }).catch(() => {
    //         load.close()
    //       })
    //     } else {
    //       devicePlanDetailItemByDeviceCodeDel(row.planId, row.deviceCode, row.itemCode).then(res => {
    //         load.close()
    //         if (res.code != 20000) {
    //           this.$message.error(res.message)
    //           return
    //         }
    //         this.$message.success(res.message)
    //         this.itemQuery.page = 1
    //         this.getDetailItemList()
    //       }).catch(() => {
    //         load.close()
    //       })
    //     }
    //   })
    // },
    // delDetail(row) {
    //   this.$confirm("此操作将会永久删除该条明细，且将在下一次任务生效，现有任务不受影响，是否继续", "提示", {
    //     confirmButtonText: "继续",
    //     cancelButtonText: "取消"
    //   }).then(() => {
    //     let load = getLoading(this)
    //     if (row.deviceCode) {
    //       devicePlanDetailDelByDeviceCode(row.planId, row.deviceCode).then(res => {
    //         load.close()
    //         if (res.code != 20000) {
    //           this.$message.error(res.message)
    //           return
    //         }
    //         this.$message.success(res.message)
    //         this.detailQuery.page = 1
    //         this.getDetailList()
    //       }).catch(() => {
    //         load.close()
    //       })
    //     } else {
    //       devicePlanDetailDel(row.planId, row.productId).then(res => {
    //         load.close()
    //         if (res.code != 20000) {
    //           this.$message.error(res.message)
    //           return
    //         }
    //         this.$message.success(res.message)
    //         this.detailQuery.page = 1
    //         this.getDetailList()
    //       }).catch(() => {
    //         load.close()
    //       })
    //     }
    //
    //   })
    // },
    rowClick(row) {
      this.clickRow = row
      this.detailQuery = {...detailQuery, planId: row.planId}
      this.getDetailList()
    },
    // getDetailList() {
    //   this.detailLoading = true
    //   this.expandRowKeys = []
    //   this.dialogExpandRowKeys = []
    //   devicePlanDetailPage(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.detailList = []
    //     this.detailTotal = 0
    //     this.detailLoading = false
    //   })
    // },
    // changeActive(isNext, row) {
    //   let load = getLoading(this)
    //   if (isNext) {
    //     if (this.createActive == 0) {
    //       this.$refs.saveForm.validate(b => {
    //         if (!b) {
    //           load.close()
    //           return
    //         }
    //         devicePlanSave(this.planData).then(res => {
    //           load.close()
    //           if (res.code != 20000) {
    //             this.$message.error(res.message)
    //             return
    //           }
    //           this.planData.planId = res.data
    //           this.detailQuery = {...detailQuery, planId: this.planData.planId}
    //           this.getDetailList()
    //           this.createActive += 1
    //         }).catch(e => {
    //           load.close()
    //         })
    //       })
    //     } else if (this.createActive == 1) {
    //       this.openItem(row)
    //       load.close()
    //       this.createActive += 1
    //     }
    //
    //   } else {
    //     if (this.createActive == 2) {
    //       this.planData = {...this.planData}
    //       this.detailQuery = {...detailQuery, planId: this.itemQuery.planId}
    //       this.getDetailList()
    //       load.close()
    //       this.createActive -= 1
    //     } else if (this.createActive == 1) {
    //       devicePlanInfo(this.planData.planId).then(res => {
    //         load.close()
    //         if (res.code != 20000) {
    //           this.$message.error(res.message)
    //           return
    //         }
    //         this.planData = {...res.data}
    //         this.createActive -= 1
    //       }).catch(e => {
    //         load.close()
    //       })
    //     }
    //   }
    //   load.close()
    // },
    // itemSelectChangFunc(list) {
    //   this.chooseItemList = list.map(i => i.code)
    // },
    // chooseDetailItem() {
    //   let loading = getLoading(this)
    //   let param = {
    //     planId: this.itemQuery.planId,
    //     productId: this.itemQuery.productId,
    //     deviceCode: this.itemQuery.deviceCode,
    //     itemCodes: this.chooseItemList
    //   }
    //   devicePlanDetailItemAdd(param).then(res => {
    //     loading.close()
    //     if (res.code != 20000) {
    //       this.$message.error(res.message)
    //       return
    //     }
    //     this.chooseItemList = []
    //     this.$message.success(res.message)
    //     this.chooseDetailItemFlag = false
    //     this.itemQuery.page = 1
    //     this.getDetailItemList()
    //
    //   }).catch(e => {
    //     loading.close()
    //   })
    // },

    closePlanEdit() {
      this.createFlag = false;
      this.getList()
    },
  //
  //
  //   deviceSelectChangFunc(list) {
  //     this.chooseDeviceList = list.map(i => i.deviceCode)
  //   },
  //
    //新增编辑计划
    openCreate(planId, row) {
      if (planId != null) {
        this.planEditTitle = `编辑保养计划信息${row.planId ? '--(' + row.name + ')' : ''}`;
        this.planData = row;
        this.planData.type = 2
      } else {
        this.planEditTitle = "新增保养计划";
        this.planData = {
          type:2,
        };
      }
      this.createFlag = true;
    },
    search() {
      this.query.page = 1
      this.getList()
    },
    onReset() {
      this.query = {...query}
      this.getList()
    },
    getList() {
      this.loading = true
      deviceUpkeepPlanPage(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
      })
    },
    setProject(row) {
      this.projectVisible = true
      this.projectItem = row
    },
  }
}
