<template>
  <div>
    <!-- <el-col :span="24" class="text-right">
      <el-button
        v-hasPermi="['wms:incoming-produce-detail:query']"
        type="primary"
        plain
        icon="el-icon-upload"
        size="mini"
        class="mb10"
        @click="handleCase"
      >入库方案</el-button>
    </el-col> -->
    <!-- 列表 -->
    <AutoResizer :given-height="220">
      <template #default="{ height }">
        <el-table
          v-if="columnsList.length > 0"
          v-loading="loading"
          :data="list"
          :height="height"
          border
        >
          <el-table-column
            v-if="columnsList[0].visible"
            :key="Math.random()"
            label="退料单号"
            align="center"
            prop="rejectNo"
            width="150"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[1].visible"
            :key="Math.random()"
            label="退料原因"
            align="center"
            prop="remark"
            width="130"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[2].visible"
            :key="Math.random()"
            label="退料发起时间"
            align="center"
            prop="applyTime"
          >
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.applyTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            v-if="columnsList[3].visible"
            :key="Math.random()"
            label="退料状态"
            align="center"
            prop="wmsStatus"
          >
            <template slot-scope="scope">
              <dict-tag
                :type="DICT_TYPE.WMS_MATERIAL_RETURN_STATUS"
                :value="scope.row.wmsStatus"
              />
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
            width="160"
            fixed="right"
          >
            <template slot-scope="scope">
              <el-button
                v-hasPermi="['wms:incoming-reject-detail:query']"
                size="mini"
                type="text"
                icon="el-icon-view"
                @click="handleView(scope.row)"
                >查看</el-button
              >
              <el-button
                type="text"
                icon="el-icon-upload"
                size="mini"
                @click="handleCase(scope.row)"
                @complete="getComplete"
                >入库方案</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </template>
    </AutoResizer>
    <!-- 分页组件 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 入库方案 -->
    <RejectWareHousingPlan
      ref="rejectWareHousingPlan"
      :warehousing-list="warehousingList"
    />
    <!-- 查看明细 -->
    <AwaitRejectDetail ref="awaitRejectDetail" />
  </div>
</template>

<script>
import {
  getIncomingRejectPage,
  getToStockPageFromOut,
} from "@/api/wms/incoming/reject";
import RejectWareHousingPlan from "./RejectWareHousingPlan.vue";
import AwaitRejectDetail from "./AwaitRejectDetail.vue";
import { getFormColumnsList } from "@/api/system/saveColumns/index";
import { mapGetters } from "vuex";
import { isArray } from "min-dash";

export default {
  name: "AwaitRejectStock",
  components: {
    RejectWareHousingPlan,
    AwaitRejectDetail,
  },
  props: {
    activeName: {
      type: String,
      default: "",
    },
    queryParamspro: {
      type: Object,
      default: () => {},
    },
  },
  inject: ["paramsQuery"],
  data() {
    return {
      // 遮罩层
      loading: false,
      // 总条数
      total: 0,
      // 领料单列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //   是否显示查看弹出层
      viewOpen: false,
      // 高级查询参数
      advParams: {},
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        receiveOrderNo: null,
      },
      warehousingList: [],
      currentRows: [],
      //   表单参数
      form: {},
      // 表单校验
      rules: {},
      // 显示隐藏列参数
      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: "供应商", 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 },
      ],
      // 显示隐藏持久化参数
      columnsList: [],
    };
  },
  // 获取当前登录用户id
  computed: {
    ...mapGetters(["userId"]),
  },
  created() {
    this.getColumnsList();
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      // if (this.$route.query.rejectId) {
      //   this.queryParams.id = this.$route.query.rejectId
      // }
      const params = {
        ...this.paramsQuery,
        ...this.queryParams,
        ...this.advParams,
      };
      if (this.$route.query.receiveOrderNo) {
        this.queryParams.receiveOrderNo = this.$route.query.receiveOrderNo;
        const params = {
          ...this.paramsQuery,
          ...this.queryParams,
          ...this.advParams,
        };
        getToStockPageFromOut(params)
          .then((res) => {
            this.list = res.data.list;
            this.total = res.data.total;
          })
          .finally(() => {
            this.loading = false;
          });
      } else {
        getIncomingRejectPage(params)
          .then((response) => {
            this.list = response.data.list;
            this.total = response.data.total;
          })
          .finally(() => {
            this.loading = false;
          });
      }
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        wmsIncomingReceiptId: undefined,
        orderType: undefined,
        erpStockId: undefined,
        wmsStoreAreaId: undefined,
        wmsStoreId: undefined,
        inQty: undefined,
        funitId: undefined,
        remark: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    // 入库方案
    handleCase(row) {
      const { rejectNo } = row;
      this.$refs.rejectWareHousingPlan.queryParams.rejectNo = rejectNo;
      this.$refs.rejectWareHousingPlan.open = true;
      this.$refs.rejectWareHousingPlan.title = "入库方案";
      this.$refs.rejectWareHousingPlan.getList();
    },
    // 查看明细
    handleView(row) {
      const { rejectNo } = row;
      this.$refs.awaitRejectDetail.queryParams.rejectNo = rejectNo;
      this.$refs.awaitRejectDetail.open = true;
    },
    getComplete() {
      this.getList();
      this.$emit("rejectComplete");
    },
    // 获取显示隐藏列
    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>
<style></style>
