<template>
  <el-dialog
    v-dialogDrag
    title="出库"
    :visible.sync="open"
    width="1200px"
    append-to-body
    :before-close="handleClose"
    @open="dialogOpen"
  >
    <el-table v-loading="loading" :data="list" border size="mini">
      <el-table-column label="销售单号" align="center" prop="saleOrderNo" />
      <el-table-column label="出库状态" align="center" prop="outStatus">
        <template slot-scope="scope">
          <dict-tag
            :type="DICT_TYPE.WMS_ISSUE_STATUS"
            :value="scope.row.outStatus"
          />
        </template>
      </el-table-column>
      <el-table-column label="产品编码" align="center" prop="productCode" />
      <el-table-column label="产品名称" align="center" prop="productName" />
      <el-table-column label="计划销售数量" align="center" prop="planQty" />
      <el-table-column label="已扫数量" align="center" prop="sendQty" />
      <el-table-column label="单位" align="center" prop="unitCode" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
        width="160"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-thumb"
            @click="handleIssue(scope.row)"
            >发出</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            >查看</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div slot="footer" class="dialog-footer">
      <!-- <el-button :loading="buttonLoading" type="primary" @click="finishIssueInSome">出库-部分</el-button>
      <el-button :loading="buttonLoading" type="primary" @click="finishIssueInAll">出库-全部</el-button> -->
      <el-button :loading="buttonLoading" plain @click="handleCancel"
        >取消</el-button
      >
    </div>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 发出 -->
    <IssueDetails ref="issueDetails" />
    <!-- 查看 -->
    <ViewIssue ref="view" />
  </el-dialog>
</template>

<script>
import {
  getToOutPartAllPage,
  updateCompetePart,
  updateCompeteAll,
} from "@/api/wms/output/salesIssue";
import IssueDetails from "./IssueDetails.vue";
import ViewIssue from "./ViewIssue.vue";

export default {
  components: {
    IssueDetails,
    ViewIssue,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // WMS 生产发料 主列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      buttonLoading: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        id: null,
      },
    };
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      getToOutPartAllPage(this.queryParams)
        .then((response) => {
          this.list = response.data.list;
          this.total = response.data.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 查看
    handleView(row) {
      const { id } = row;
      this.$refs.view.queryParams.id = id;
      this.$refs.view.open = true;
    },
    // 发出
    handleIssue(row) {
      const { id } = row;
      this.$refs.issueDetails.queryParams.id = id;
      this.$refs.issueDetails.open = true;
    },
    // 完成出库-部分
    finishIssueInSome() {
      // if (this.currentRows.length <= 0) {
      //   this.$modal.msgWarning('请选择您要出库的数据')
      //   return
      // }
      this.$modal
        .confirm("您正在执行出库-部分操作，是否继续？")
        .then(() => {
          const params = {
            masterId: this.queryParams.id,
          };
          this.buttonLoading = true;
          updateCompetePart(params)
            .then((res) => {
              this.$modal.msgSuccess("操作成功");
              this.open = false;
              this.buttonLoading = false;
              this.getList();
              this.$parent.getList();
              this.$emit("complete");
            })
            .finally(() => {
              this.buttonLoading = false;
            });
        })
        .catch(() => {
          this.$modal.msg("您已取消操作");
        });
    },
    // 完成出库-全部
    finishIssueInAll() {
      // if (this.currentRows.length <= 0) {
      //   this.$modal.msgWarning('请选择您要出库的数据')
      //   return
      // }
      // if (this.currentRows.sendQty < this.currentRows.planQty) {
      //   this.$modal.msgWarning('全部出库的已扫数量必须大于计划销售数量')
      //   return
      // }
      this.$modal
        .confirm("您正在执行出库-全部操作，是否继续？")
        .then(() => {
          const params = {
            masterId: this.queryParams.id,
          };
          this.buttonLoading = true;
          updateCompeteAll(params)
            .then((res) => {
              this.$modal.msgSuccess("操作成功");
              this.open = false;
              this.buttonLoading = false;
              this.getList();
              this.$emit("complete");
            })
            .finally(() => {
              this.buttonLoading = false;
            });
        })
        .catch(() => {
          this.$modal.msg("您已取消操作");
        });
    },
    handleCancel() {
      this.open = false;
      this.currentRows = [];
    },
    handleClose(done) {
      this.currentRows = [];
      done && done();
    },
    dialogOpen() {
      this.$nextTick(() => {
        this.getList();
      });
    },
  },
};
</script>

<style></style>
