<!-- 上架确认  -->
<template>
  <div class="onConfirm">
    <p-card title="上架确认">
      <template slot="extra">
        <p-button type="primary" icon="check" @click="finishOk">上架确认</p-button>
        <!-- <p-button type="primary" icon="redo" @click="hanlderRest">重置</p-button> -->
      </template>
      <div class="plg-inner-form-mb">
        <p-grid-form ref="search" :items="items" @submit="hanlderSubmit" />
      </div>
      <!-- @checkRow="checkRow" -->
      <p-table-sheet
        ref="tableSheet"
        :columns="columns"
        :data-source="data"
        rowKey="detailId"
        dragle
        auto-width
        :scroll="{ x: true }"
        hasRowSelectCheck
        :selectedRowKeys.sync="selectedRowKeys"
        @change="changePagination"
        :pagination="pagination"
        :loading="loading"
        bordered
      >
      </p-table-sheet>
      <div style="float: right; padding: 10px">
        <a-pagination
          v-model="pageNum"
          :page-size-options="pageSizeOptions"
          :page-size="pageSize"
          @change="change"
          show-size-changer
          :show-total="(total) => `共 ${total} 条`"
          @showSizeChange="onShowSizeChange"
          :total="total"
          show-less-items
        />
      </div>
    </p-card>
  </div>
</template>

<script>
// 设置默认仓库
import { setwarehouse } from "@/views/kzyWMS/SearchModal/mixins_warehouse.js";
import { items, columns } from "./config/onConfirm.js";
import { replenish_list, replenish_confirm, WmsLoctype } from "./config/api.js";
import { getCargoOwner } from "../../utils/index";

export default {
  mixins: [setwarehouse],
  data() {
    return {
      pageSizeOptions: ["10", "20", "30", "40", "50"],
      pageNum: 1,
      pageSize: 15,
      total: 0,
      items,
      columns: columns(this),
      data: [],
      WmsLoctypeOptions:[],
      loading: false,
      selectedRowKeys: [],
      selectedRows: [],
      pagination: {
        total: 0,
        pageSize: 10, //每页中显示10条数据
        current: 1,
        showSizeChanger: true,
        pageSizeOptions: ["10", "15", "20"], //每页中显示的数据
        showTotal: (total) => `共有 ${total} 条数据`, //分页中显示总的数据
      },
    };
  },
  methods: {
    async getWmsLoctype() {
      let res = await WmsLoctype();
      console.log(res);
      this.WmsLoctypeOptions = res.data.map((i) => {
        return {
          dictCode: i.dictCode,
          dictName: i.dictName,
        };
      });
      console.log(this.WmsLoctypeOptions);
    },
    change(page, pageSize) {
      console.log(page, pageSize);
      this.pageNum = page;
      this.pageSize = pageSize;
      //   this.getinfo()
      this.$refs.search.form.validateFields((err, values) => {
        if (!err) {
          console.log("验证通过: ", values);
          this.getData(values);
        }
      });
    },
    onShowSizeChange(current, pageSize) {
      this.pageSize = pageSize;
      this.$refs.search.form.validateFields((err, values) => {
        if (!err) {
          console.log("验证通过: ", values);
          this.getData(values);
        }
      });
    },
    async getAuthBycargoOwner() {
      await getCargoOwner().then((res) => {
        if (res.success) {
          this.$nextTick(() => {
            this.$refs.search.form.setFieldsValue({
              cargoOwnerId: res.data.list[0].companyId,
              cargoOwnerName: res.data.list[0].companyName,
              cargoOwnerCode: res.data.list[0].companyCode,
            });
          });
        }
        if (!res.success) {
          this.$exception(res);
          return;
        }
      });
    },
    getData(params) {
      (params = {
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        ...params,
      }),
        (this.loading = true);
      replenish_list(params)
        .then((res) => {
          if (res.success) {
            if (res.data) {
              // this.data = res.data.filter((item,index)=>item.status != 'IN');
              this.data = res.data.list;
              this.total = Number(res.data.totalCount);
              this.loading = false;
              this.selectedRows.length = 0;
              this.selectedRowKeys.length = 0;
            }
          } else {
            this.loading = false;
            this.$exception(res);
          }
        })
        .finally(() => {
          this.loading = false;
        });
    },
    hanlderSubmit(event, form) {
      event.preventDefault();
      // console.log("getFieldsValue:",form.getFieldsValue());
      // this.getData(form.getFieldsValue())
      this.$refs.search.form.validateFields((err, values) => {
        if (!err) {
          console.log("验证通过: ", values);
          this.getData(values);
        }
      });
    },
    // 重置
    hanlderRest(event, form) {
      this.$refs.search.form.resetFields();
    },
    // 补货完成
    checkRow(selectedRowKeys, selectedRows) {
      console.log("操作选中行的checkbox：", selectedRowKeys, selectedRows);
      this.selectedRowKeys = [...selectedRowKeys];
      this.selectedRows = [...selectedRows];
    },
    async finishOk() {
      //   console.log(this.selectedRowKeys);
      let selectedRow = await this.$refs.tableSheet.getDataSource(true, true);
      //   console.log(!this.selectedRows.some((item) => item.status != "1"));
      if (selectedRow.length && !this.selectedRows.some((item) => item.status != "1")) {
        let dtls = selectedRow.map((i) => {
          return {
            lineId: i.detailId,
            toLocationId: i.toWhLocatorId,
          };
        });
        console.log(dtls);
        // return;
        // moveFlag:"[integer] 移库类型 0：下架、1：上架"
        replenish_confirm({
          moveFlag: 1,
          //   lineIds: [...this.selectedRowKeys],
          dtls,
        })
          .then((res) => {
            if (res.success) {
              this.$message.success("上架确认成功！");
              this.getData({ ...this.$refs.search.form.getFieldsValue() });
            } else {
              this.$exception(res);
            }
          })
          .finally(() => {
            this.loading = false;
          });
      } else {
        this.$message.warning("未选择/选择数据含有非下架状态！", 5);
      }
    },
    changePagination(pagination, filters, sorter) {
      // 分页变化
      console.log(pagination, filters, sorter, "分页变化分页变化");
      this.pagination = pagination;
      this.getData({ ...this.$refs.search.form.getFieldsValue() }); //分页查询所有列表
    },
  }, // methods
  mounted() {
    this.getAuthBycargoOwner();
    this.getWmsLoctype();
    // this.getData({...this.$refs.search.form.getFieldsValue()});
  },
};
</script>

<style></style>
