<template>
  <div class="app-container">
    <!-- 列表 -->
    <AutoResizer :given-height="230">
      <template #default="{ height }">
        <!-- <el-table
          v-if="columnsList.length > 0"
          v-loading="loading"
          :data="list"
          border
          :height="height"
          show-summary
          :summary-method="getSummaries"
        >
          <el-table-column
            label="erp单号"
            align="center"
            prop="erpWriteNo"
            width="140"
            fixed="left"
          />
          <el-table-column
            v-if="columnsList[6].visible"
            :key="Math.random()"
            label="入库单号"
            align="center"
            prop="warehousingOrderNo"
            width="160"
            fixed="left"
            show-overflow-tooltip
          >
            <template #default="{ row }">
              <el-button type="text" @click="findDetail(row)">{{
                row.warehousingOrderNo
              }}</el-button>
            </template></el-table-column
          >
          <el-table-column
            v-if="columnsList[7].visible"
            :key="Math.random()"
            label="物料编码"
            align="center"
            prop="materialNo"
            width="120"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[8].visible"
            :key="Math.random()"
            label="物料名称"
            align="center"
            prop="materialName"
            width="100"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[9].visible"
            :key="Math.random()"
            label="计划数量"
            align="center"
            prop="planAmount"
          />
          <el-table-column
            v-if="columnsList[10].visible"
            :key="Math.random()"
            label="BOM版本"
            align="center"
            prop="mesBomVersion"
            width="100"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[11].visible"
            :key="Math.random()"
            label="生产订单"
            align="center"
            prop="workOrderNo"
            width="100"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[12].visible"
            :key="Math.random()"
            label="箱号"
            align="center"
            prop="caseNo"
            width="170"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[13].visible"
            :key="Math.random()"
            label="拆箱状态"
            align="center"
            prop="splitFlag"
            width="120"
          >
            <template slot-scope="scope">
              <dict-tag
                :type="DICT_TYPE.WMS_QUERY_UNPACKING_STATUS"
                :value="scope.row.splitFlag"
              />
            </template>
          </el-table-column>
          <el-table-column
            v-if="columnsList[14].visible"
            :key="Math.random()"
            label="装箱数量"
            align="center"
            prop="inQty"
          />
          <el-table-column
            v-if="columnsList[15].visible"
            :key="Math.random()"
            label="仓库"
            align="center"
            prop="wmsStockId"
            width="100"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[16].visible"
            :key="Math.random()"
            label="区域"
            align="center"
            prop="wmsStockAreaId"
            width="100"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[17].visible"
            :key="Math.random()"
            label="货位"
            align="center"
            prop="wmsStockLocationId"
            width="100"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[18].visible"
            :key="Math.random()"
            label="操作人"
            align="center"
            prop="creator"
          />
          <el-table-column
            v-if="columnsList[19].visible"
            :key="Math.random()"
            label="入库时间"
            align="center"
            prop="createTime"
            width="160"
          >
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="80">
            <template slot-scope="scope">
              <el-button
                v-hasPermi="['wms:incoming-produce-detail:delete']"
                size="mini"
                type="text"
                icon="el-icon-delete"
                class="text-danger"
                @click="handleDelete(scope.row)"
                >删除</el-button
              >
            </template></el-table-column>
        </el-table> -->

        <el-table
          ref="table"
          v-if="columnsList.length > 0"
          v-loading="loading"
          :data="list"
          :height="height"
          border
          :summary-method="getSummaries"
          show-summary
        >
          <!-- <el-table-column
            type="selection"
            width="55"
            fixed="left"
            align="center"
          /> -->
          <el-table-column
            prop="inOrderNo"
            label="入库单号"
            width="140"
            align="center"
          >
            <template #default="{ row }">
              <el-button type="text" @click="findDetail(row)">{{
                row.inOrderNo
              }}</el-button>
            </template>
          </el-table-column>
          <el-table-column
            prop="productCode"
            label="产品编码"
            min-width="90"
            align="center"
          />
          <el-table-column
            prop="productName"
            label="产品名称"
            min-width="120"
            align="center"
          />
          <el-table-column
            prop="erpWorkOrderNo"
            label="生产订单编号"
            width="120"
            align="center"
          />
          <el-table-column
            prop="inQty"
            label="入库箱数"
            width="80"
            align="center"
          />
          <el-table-column
            prop="applyer"
            label="申请人员"
            width="80"
            align="center"
          />
          <el-table-column
            prop="applyTime"
            label="申请入库时间"
            align="center"
            min-width="160"
          >
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.applyTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="inStorager"
            min-width="80"
            label="入库人员"
            align="center"
          />
          <el-table-column
            prop="realTime"
            min-width="160"
            label="实际入库时间"
            align="center"
          >
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.realTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="sumDetailQty"
            min-width="100"
            label="箱内产品总数"
            align="center"
          />
          <el-table-column
            :key="Math.random()"
            prop="storageStatus"
            label="状态"
            min-width="80"
            align="center"
          >
            <template slot-scope="scope">
              <dict-tag
                :type="DICT_TYPE.MES_IMPLEMENT_STORAGE_STATE"
                :value="scope.row.storageStatus"
              />
            </template>
          </el-table-column>
          <el-table-column
            prop="countOrderNo"
            label="关联订单数"
            min-width="90"
            align="center"
          />
        </el-table>
      </template>
    </AutoResizer>

    <!-- 分页组件 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <ProductWareHousingPlanAlready
      ref="ProductWareHousingPlanAlready"
      @complete="getList"
      storageStatus="1"
    />
  </div>
</template>

<script>
import {
  getProduceStockedPage,
  deleteIncomingProduceDetail,
} from "@/api/wms/inquirePurchase/complexDocuments";
import { getFormColumnsList } from "@/api/system/saveColumns/index";
import ProductWareHousingPlanAlready from "./ProductWareHousingPlanAlready.vue";
import { mapGetters } from "vuex";
import { isArray } from "min-dash";
import { countProductInStoragePage } from "@/api/wms/incoming/production";

export default {
  name: "AlreadyProduct",
  components: { ProductWareHousingPlanAlready },
  inject: ["paramsQuery"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // WMS补料申请单主列表
      list: [],
      // 高级查询参数
      advParams: {},
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        eBillNo: null,
        billTppe: null,
        wmsProductionWorkOrderBomId: null,
        createTime: [],
        wmsResupplyOrderMasterId: undefined,
      },
      // 表单参数
      form: {},
      columns: [
        { key: 0, label: "生产入库单号", visible: true },
        { key: 1, label: "申请入库时间", visible: true },
        { key: 2, label: "申请人", visible: true },
        { key: 3, label: "入库箱数", visible: true },
        { key: 4, label: "检验结果", visible: true },
        { key: 5, label: "检验单号", visible: true },
        { key: 6, label: "入库单号", visible: true },
        { key: 7, label: "物料编码", visible: true },
        { key: 8, label: "物料名称", visible: true },
        { key: 9, label: "计划数量", visible: true },
        { key: 10, label: "BOM版本", visible: true },
        { key: 11, label: "生产订单", visible: true },
        { key: 12, label: "箱号", visible: true },
        { key: 13, label: "拆箱状态", visible: true },
        { key: 14, label: "装箱数量", visible: true },
        { key: 15, label: "仓库", visible: true },
        { key: 16, label: "区域", visible: true },
        { key: 17, label: "货位", visible: true },
        { key: 18, label: "操作人", visible: true },
        { key: 19, label: "入库时间", visible: true },
      ],
      // 显示隐藏持久化参数
      columnsList: [],
    };
  },
  // 获取当前登录用户id
  computed: {
    ...mapGetters(["userId"]),
  },
  created() {
    this.getColumnsList();
    this.getList();
  },
  methods: {
    findDetail(data) {
      const { inOrderNo, productCode } = data;
      this.$refs.ProductWareHousingPlanAlready.queryParams.warehousingOrderNo =
        inOrderNo;
      this.$refs.ProductWareHousingPlanAlready.queryParams.materialNo = productCode;
      this.$refs.ProductWareHousingPlanAlready.open = true;
    },
    /** 查询列表 */
    getList() {
      // this.loading = true;
      // // 执行查询
      // const params = {
      //   ...this.paramsQuery,
      //   ...this.queryParams,
      //   ...this.advParams,
      // };
      // getProduceStockedPage(params)
      //   .then((response) => {
      //     this.list = response.data.list;
      //     this.total = response.data.total;
      //   })
      //   .finally(() => {
      //     this.loading = false;
      //   });
      this.loading = true;
      // 执行查询
      const params = {
        ...this.paramsQuery,
        ...this.queryParams,
        ...this.advParams,
        storageStatus: 1,
      };
      countProductInStoragePage(params)
        .then((response) => {
          this.list = response.data.list;
          this.total = response.data.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const { id } = row;
      this.$modal
        .confirm('是否确认删除生产入库已入库单号为"WMS-' + id + '"的数据项?')
        .then(function () {
          return deleteIncomingProduceDetail(id);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功,请在erp系统同步删除！");
        })
        .catch(() => {});
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 总计
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "总计";
          return;
        }

        const values = data.map((item) => Number(item[column.property]));
        if (column.property === "inQty" || column.property === "sumDetailQty") {
          sums[index] = values.reduce((acc, cur) => {
            const value = Number(cur);
            if (!isNaN(value)) {
              return acc + cur;
            } else {
              return acc;
            }
          }, 0);
          sums[index];
        }
      });
      return sums;
    },
    // 获取显示隐藏列
    getColumnsList() {
      const { componentName } = this.$route.meta || null;
      const params = {
        userId: this.userId,
        vueForm: componentName,
      };
      getFormColumnsList(params)
        .then((res) => {
          if (isArray(res.data)) {
            this.columnsList = res.data.length === 0 ? this.columns : res.data;
            // 对比默认数据和接口数据，将缺失的列添加到接口数据中
            this.columnsList = this.columns.reduce((acc, defaultColumn) => {
              const found = res.data.find(
                (apiColumn) => apiColumn.key === defaultColumn.key
              );
              if (!found) {
                acc.push({
                  ...defaultColumn,
                  vueForm: componentName,
                  userId: this.userId,
                  id: null,
                });
              }
              return acc;
            }, res.data);
          } else {
            this.columnsList = this.columns;
          }
        })
        .catch((err) => {
          if (err) {
            this.columnsList = this.columns;
          }
        })
        .finally(() => {
          this.$emit("columns", this.columnsList);
        });
    },
  },
};
</script>
