<template>
  <div>
    <div class="title_box rx-bc mt6">
      <div class="name">内包装 </div>

      <div class="rx-bc">
        <el-button size="mini" @click="handCancelPacking">重置打包</el-button>
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    components: {},
    props: {
      objData: {
        type: Object,
        default: () => {}
      },

      taskId: {
        type: String,
        default: ''
      },

      workOrderId: {
        type: String,
        default: ''
      }
    },

    data() {
      return {
        formedNumLast: 0,

        quantity: '',
        unit: '',

        unitList: [],

        splitList: [],

        packUnit: null,
        temporaryNum: 0,
        temporaryCount: 0,
        temporaryList: [],
        packTwoList: [],

        show: false,
        formData: {
          wPackNum: null
        }
      };
    },

    computed: {
      clientEnvironmentId() {
        return this.$store.state.user.info.clientEnvironmentId;
      }
    },

    methods: {
      byCode() {
        getByCode('packing_unit').then((res) => {
          this.unitList = [];

          res.forEach((obj, index) => {
            for (let key in obj) {
              if (obj.hasOwnProperty(key)) {
                // 确保key是对象自身的属性

                this.unitList.push({
                  Key: `${key}`,
                  Value: ` ${obj[key]}`
                });
              }
            }
          });
        });
      },

      getPackingDetails() {
        let param = {
          workOrderId: this.workOrderId,
          taskId: this.taskId,
          tier: 1
        };

        getPackingReport(param).then((res) => {
          this.splitList = res.detailList;
          this.formedNumLast = res.totalQuantity;
          this.quantity = res.quantity;
          this.unit = res.unit;
        });
      },

      handCancelPacking() {
        uni.showModal({
          title: `是否清空包装？`,
          content: '',
          confirmText: '确认',
          success: () => {
            let param = {
              workOrderId: this.workOrderId,
              taskId: this.taskId
            };
            cancelPacking(param).then((res) => {
              this.getPackingDetails();
              this.getPackingDetailsTwo();
            });
          }
        });
      },

      handleSplit() {
        if (this.splitList.length) {
          uni.showToast({
            title: '产品已分包',
            icon: 'none'
          });
          return false;
        }

        if (!this.formedNumLast) {
          uni.showToast({
            title: '包装总数数量不能为空',
            icon: 'none'
          });
          return false;
        }

        if (!this.quantity) {
          uni.showToast({
            title: '内包装单元数量不能为空',
            icon: 'none'
          });
          return false;
        }

        if (!this.unit) {
          uni.showToast({
            title: '请选择包装单位',
            icon: 'none'
          });
          return false;
        }

        let param = {
          totalQuantity: this.formedNumLast,
          quantity: this.quantity,
          unit: this.unit,
          workOrderId: this.workOrderId,
          taskId: this.taskId,
          tier: 1
        };
        packingReport(param).then((res) => {
          this.splitList = res.map((m) => {
            return {
              check: false,
              ...m
            };
          });

          this.getPackingDetails();
        });
      },

      handleCheck(idx, it) {
        if (it.quantity <= 0) {
          uni.showToast({
            icon: 'none',
            title: '数量为空不能勾选'
          });

          return false;
        }

        this.$set(this.splitList[idx], 'check', it.check ? 0 : 1);

        this.temporaryList = [];

        this.temporaryList = this.splitList.filter((e) => {
          return e.check == 1;
        });
        this.temporaryNum = 0;
        this.temporaryCount = 0;
        if (this.temporaryList.length > 0) {
          this.temporaryList.forEach((e) => {
            this.temporaryNum = this.temporaryNum + 1;
            this.temporaryCount = this.temporaryCount + Number(e.quantity);
          });
        }
      },

      handclose(it) {
        this.splitList.forEach((f) => {
          if (f.computeSize == it.computeSize) {
            f.check = 0;
          }
        });

        this.temporaryList = [];

        this.temporaryList = this.splitList.filter((e) => {
          return e.check == 1;
        });
      },

      getPackingDetailsTwo() {
        let param = {
          totalQuantity: this.formedNumLast,
          quantity: this.quantity,
          unit: this.unit,
          workOrderId: this.workOrderId,
          taskId: this.taskId,
          tier: 2
        };
        getPackingReportRepeat(param).then((res) => {
          this.packTwoList = res.detailList;
        });
      },

      handlePack() {
        let _arr = [];
        _arr = this.temporaryList.map((m) => {
          return {
            ...m,
            isChecked: 1
          };
        });

        let param = {
          detailList: _arr,
          quantity: this.temporaryCount,
          totalQuantity: this.formedNumLast,
          unit: this.packUnit,
          taskId: this.taskId,
          workOrderId: this.workOrderId,
          tier: 2
        };

        packingReportRepeat(param).then((res) => {
          this.temporaryList = [];
          this.getPackingDetails();
          this.getPackingDetailsTwo();
        });
      },

      scrolltolower() {},

      getData() {
        let _packingReportMarginList = [];

        _packingReportMarginList = this.splitList.filter((e) => {
          return !e.parentId;
        });

        let packInfo = {
          packingReportList: this.packTwoList,
          packingReportMarginList: _packingReportMarginList,
          formedNumLast: this.formedNumLast
        };
        return packInfo;
      },

      calculation() {
        this.show = true;
      },

      calculationSave() {
        let count = this.splitList.filter((item) => item.isChecked != 1).length;

        if (this.formData.wPackNum <= count && count != 0) {
          for (let i = 0; i < this.formData.wPackNum; i++) {
            this.splitList[i].check = 1;
          }
          this.handleCheck(0, 0);
          this.calculationClose();
        } else {
          if (count != 0) {
            for (let i = 0; i < count; i++) {
              this.splitList[i].check = 1;
            }
            this.handleCheck(0, 0);
            this.calculationClose();
          }
          uni.showToast({
            icon: 'none',
            title: '外包装数超出内包装条数'
          });
        }
      },

      calculationClose() {
        this.show = false;
      },

      calculationReset() {
        this.formData.wPackNum = null;
        this.splitList.forEach((f) => {
          f.check = null;
        });
        this.temporaryList = [];
        this.show = false;
      }
    },

    created() {
      this.formedNumLast = this.objData.formedNumLast;

      this.byCode(), this.getPackingDetails();
      this.getPackingDetailsTwo();
    }
  };
</script>
