import {
  compareExportExcel,
  dlThrCpProduct,
  dlThrProduct,
  selectInvProductDetail, stockCompareAddProduct,
  stockCompareDelete, stockCompareDetailDel, stockCompareDetailPage,
  stockComparePage, stockCompareResultPage,
  stockCompareSave, uploadCompare
} from "@/api/inventory/stockCompareApi";
import {mainActionMap, stockCompareStatusEnum} from "@/utils/data";
import data from "@/views/system/dict/data.vue";
import fa from "element-ui/src/locale/lang/fa";
import {postExcelInoutImport} from "@/api/inout/orderDetailResult";

let filterQuery = {
  page: 1,
  limit: 10,
  id: null,
  thrSysId: null,
  invCode: null,
  mainAction: null,
  action: null,
  remark: null,
  status: null,
}
let param = {
  thrSysId: null,
  invCode: null,
  action: null,
  mainAction: '',
  compareDate: [],
  compareStartDate: '',
  compareEndDate: '',
  remark: null,
}
let selectProductQuery = {
  page: 1, limit: 10, compareId: null
}


export default {
  computed: {
    stockCompareStatusEnum() {
      return stockCompareStatusEnum
    },
    data() {
      return data
    },
    mainActionMap() {
      return mainActionMap
    }
  },
  data() {
    return {
      showSearch: true,
      compareId: null,
      editRow: null,
      saveLoading: false,
      showDialog: false,
      loading: false,
      stockCompareDetailLoading: false,
      stockCompareDetailList: [],
      stockCompareDetailTotal: 0,
      stockCompareDetailQuery: {
        page: 1,
        limit: 10
      },
      // --------选入产品--------------
      showAddProductDialog: false,
      productLoading: false,
      productList: [],
      checkProductList: [],
      productTotal: 0,
      // ---------------------------
      filterQuery: {...filterQuery},
      list: [],
      total: 0,
      param: {...param},
      selectProductQuery: {...selectProductQuery},
      paramRules: {
        thrSysId: [{required: true, message: '第三方系统不能为空', trigger: 'change'}],
        invCode: [{required: true, message: '所属仓库不能为空', trigger: 'change'}],
        compareDate: [{required: true, message: '对比时间不能为空', trigger: 'change'}],
        remark: [{required: true, message: '备注描述不能为空', trigger: 'blur'}],

      },
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            let now = new Date();
            const end = new Date(now - 24 * 60 * 60 * 1000);
            const start = new Date(now - 24 * 60 * 60 * 1000);
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            let now = new Date();
            const end = new Date(now - 24 * 60 * 60 * 1000);
            const start = new Date(now - 24 * 60 * 60 * 1000);
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            let now = new Date();
            const end = new Date(now - 24 * 60 * 60 * 1000);
            const start = new Date(now - 24 * 60 * 60 * 1000);
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }],
        disabledDate(time) {
          const now = new Date();
          const oneDayAgo = new Date(now - 24 * 60 * 60 * 1000);
          return time.getTime() >= oneDayAgo;
        },
        firstDayOfWeek: 1
      },

      currentRow: null,
      uploadCompareLoading: false,
      compareFuncLoading: false,
    }
  },
  created() {
    this.getList()
  },
  methods: {
    checkProduct(isAll) {
      let param = {
        compareId: this.compareId,
        isAll: isAll,
        productIds: this.checkProductList
      }
      stockCompareAddProduct(param).then(res => {
        if (res.code != 20000) {
          this.$message.error(res.message)
          return
        }
        this.$message.success(res.message)
        this.showAddProductDialog = false
        let query = {compareId: this.compareId,};
        this.getResultList(query)
      })
    },
    addProductSelectionChange(row) {
      this.checkProductList = row.map(i => {
        return {productId: i.productId}
      })
    },
    //触发对比
    compareFunc() {
      const loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });
      let query = {
        compareId: this.compareId
      }
      this.compareFuncLoading = true
      dlThrCpProduct(query).then(res => {
        this.compareFuncLoading = false
        loading.close();
        if (res.code != 20000) {
          this.$message.error(res.message)
          return
        }
        this.getResultList(query)
      }).catch(e => {
        this.compareFuncLoading = false
        loading.close();
      })
    },

    uploadCompare() {
      this.uploadCompareLoading = true
      uploadCompare(this.compareId).then(res => {
        this.uploadCompareLoading = false
        if (res.code != 20000) {
          this.$message.error(res.message)
          return
        } else {
          this.$message.success("提交成功！");
          this.showDialog = false;
          this.getList()
        }
      })
    },


    showAddProduct() {
      this.selectProductQuery = {...selectProductQuery, compareId: this.compareId}
      this.productList = []
      this.checkProductList = []
      this.productTotal = 0
      this.showAddProductDialog = true
      this.selectInvProductDetailList()
    },
    getStockCompareDetailList() {
      this.stockCompareDetailQuery.compareId = this.compareId
      // this.stockCompareDetailLoading = true
      // stockCompareDetailPage(this.stockCompareDetailQuery).then(res => {
      //   this.stockCompareDetailLoading = false
      //   if (res.code != 20000) {
      //     this.$message.error(res.message)
      //     return
      //   }
      //   this.stockCompareDetailList = res.data.list || []
      //   this.stockCompareDetailTotal = res.data.total || 0
      // }).catch(e => {
      //   this.stockCompareDetailLoading = false
      // })
      this.getResultList(this.stockCompareDetailQuery)
    },
    selectInvProductDetailList() {
      this.productLoading = true
      selectInvProductDetail(this.selectProductQuery).then(res => {
        this.productLoading = false
        if (res.code != 20000) {
          this.$message.error(res.message)
          return
        }
        this.productList = res.data.list || []
        this.productTotal = res.data.total || 0
      }).catch(e => {
        this.productLoading = false
      })
    },
    saveStockCompare() {
      this.saveLoading = true
      this.$refs.stockCompare.validate(valid => {
        if (valid) {
          this.param.compareStartDate = this.param.compareDate[0]
          this.param.compareEndDate = this.param.compareDate[1]
          stockCompareSave(this.param).then(res => {
            this.saveLoading = false
            if (res.code != 20000) {
              this.$message.error(res.message)
              return
            }
            this.stockCompareDetailList = []
            this.stockCompareDetailTotal = 0
            this.compareId = res.data
            this.editRow = {status: stockCompareStatusEnum.DRAFT.key}
          }).catch(e => {
            this.saveLoading = false
          })
        }
        this.saveLoading = false
      })
    },
    removeDetail(row) {
      this.$confirm("此操作将永久移除该条数据，是否继续", "提示", {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(res => {
        let param = {
          compareId: row.compareId,
          productId: row.productId,
          mainAction: row.mainAction,
          batchNo: row.batchNo
        }
        stockCompareDetailDel(param).then(res => {
          if (res.code != 20000) {
            this.$message.error(res.message)
            return
          }
          this.$message.success(res.message)
          this.stockCompareDetailQuery.page = 1
          this.getStockCompareDetailList()
        })
      })
    },
    del(row) {
      this.$confirm("此操作将永久删除该条数据，是否继续", "提示", {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(res => {
        stockCompareDelete(row.id).then(res => {
          if (res.code != 20000) {
            this.$message.error(res.message)
            return
          }
          this.filterQuery.page = 1
          this.getList()
        })
      })
    },
    edit(row) {
      this.compareId = row.id
      this.editRow = row
      this.saveLoading = false
      this.param = {
        thrSysId: row.thrSysId,
        invCode: row.invCode,
        action: row.action,
        mainAction: row.mainAction,
        compareDate: [row.compareStartDate, row.compareEndDate],
        compareStartDate: row.compareStartDate,
        compareEndDate: row.compareEndDate,
        remark: row.remark
      }
      this.showDialog = true
      this.stockCompareDetailQuery.page = 1
      this.stockCompareDetailQuery.limit = 10
      this.stockCompareDetailQuery.compareId = this.compareId
      this.getStockCompareDetailList()
    },
    add() {
      this.param = {...param}
      this.compareId = null
      this.editRow = null
      this.showDialog = true
      this.saveLoading = false
    },
    hideSearch() {
      this.showSearch = !this.showSearch
    },
    onReset() {
      this.showSearch = true
      this.filterQuery = {...filterQuery}
    },
    onSubmit() {
      this.filterQuery.page = 1
      this.getList()
    },
    getList() {
      this.loading = true
      stockComparePage(this.filterQuery).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(res => {
        this.loading = false
      })
    },
    handleSelectionChange(val) {
      this.currentRow = val;
      let query = {
        compareId: this.currentRow.id
      }
      this.getResultList(query);
    },

    getResultList(query) {
      stockCompareResultPage(query).then(res => {
        this.loading = false
        if (res.code != 20000) {
          this.$message.error(res.message)
          return
        }
        this.stockCompareDetailList = res.data || []
        this.total = res.data.total || 0
      }).catch(res => {
        this.loading = false
      })
    },

    exportExcel(row) {
      let printParams = {
        compareId: row.id
      };
      compareExportExcel(printParams).then((response) => {
        let blob = new Blob([response], {type: "application/vnd.ms-excel"});
        let url = window.URL.createObjectURL(blob); // 创建一个临时的url指向blob对象
        let a = document.createElement("a");
        a.href = url;
        a.click();
        this.$message("生成文件成功！")
      })
    }


  }
}
