<template>
  <div class="app-container">
    <!-- 搜索工作栏 -->
    <el-form
      v-show="showSearch"
      ref="queryForm"
      :model="queryParams"
      size="small"
      label-width="68px"
    >
      <SearchBar :show-more="false" :show-span="8" :collapse-span="8">
        <template #header>
          <el-col :span="8">
            <el-form-item label="发料单号" prop="outStockNo">
              <el-input
                v-model="queryParams.outStockNo"
                placeholder="请输入发料单号"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="物料批次" prop="wmsLabelMasterId">
              <el-input
                v-model="queryParams.wmsLabelMasterId"
                placeholder="请输入物料批次"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
        </template>
        <template #buttons>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        </template>
      </SearchBar>
    </el-form>
    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-print"
          size="mini"
          :loading="exportLoading"
          @click="handlePrint"
          >单据打印</el-button
        >
      </el-col>
      <right-toolbar
        :columns="columnsList"
        :show-search.sync="showSearch"
        @saveSetting="getColumnsList"
        @queryTable="getList"
      />
    </el-row>
    <el-table
      v-if="columnsList.length > 0"
      ref="multipleTable"
      v-loading="loading"
      :data="list"
      border
      @selection-change="tableSelected"
      @select="select"
      @select-all="selectAll"
    >
      <el-table-column type="selection" width="55" fixed="left" />
      <el-table-column
        v-if="columnsList[0].visible"
        :key="Math.random()"
        label="发料单号"
        align="center"
        prop="outStockNo"
      />
      <el-table-column
        v-if="columnsList[1].visible"
        :key="Math.random()"
        label="发料类型"
        align="center"
        prop="type"
      >
        <template slot-scope="scope">
          <dict-tag :type="DICT_TYPE.WMS_ISSUE_TYPE" :value="scope.row.type" />
        </template>
      </el-table-column>
      <el-table-column
        v-if="columnsList[2].visible"
        :key="Math.random()"
        label="发料人"
        width="100"
        align="center"
        prop="creator"
      />
      <el-table-column
        v-if="columnsList[3].visible"
        :key="Math.random()"
        label="发料时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="90" prop="">
        <template slot-scope="{ row }">
          <el-button
            type="text"
            icon="el-icon-view"
            size="mini"
            @click="handleView(row)"
            >发料明细</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 物料明细 -->
    <materialDetail ref="materialList" />
    <!-- 单据打印 -->
    <IssuePrinit ref="issuePrinit" />
  </div>
</template>

<script>
import { getOutStockPage } from "@/api/wms/output/productionIssue";
import materialDetail from "./materialDetails.vue";
import { mapState } from "vuex";
// import request from '@/utils/request'
const AccessTokenKey = "ACCESS_TOKEN";
import IssuePrinit from "./IssuePrinit.vue";
import { getFormColumnsList } from "@/api/system/saveColumns/index";
import { mapGetters } from "vuex";
import { isArray } from "min-dash";

export default {
  components: {
    materialDetail,
    IssuePrinit,
  },
  data() {
    return {
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      tokens: "",
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      list: [],
      currentRows: [],
      ids: "",
      baseUrl: "",
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      reportId: "",
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        id: null,
        outStockNo: null,
        wmsLabelMasterId: null,
      },
      // 显示隐藏列参数
      columns: [
        { key: 0, label: "发料单号", visible: true },
        { key: 1, label: "发料类型", visible: true },
        { key: 2, label: "发料人", visible: true },
        { key: 3, label: "发料时间", visible: true },
      ],
      // 显示隐藏持久化参数
      columnsList: [],
    };
  },
  computed: {
    ...mapState({
      routeLists: (state) => state.permission.addRoutes,
    }),
    ...mapGetters(["userId"]),
  },
  created() {
    // 获取显隐列列表
    this.getColumnsList();
    this.queryParams.id = this.$route.query.id;
    this.tokens = localStorage.getItem(AccessTokenKey);
    this.reportId = this.$route.meta.reportId;
    this.baseUrl = process.env.VUE_APP_REPORT_API;
    this.getList();
  },

  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      getOutStockPage(this.queryParams)
        .then((response) => {
          this.list = response.data.list;
          this.total = response.data.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 发料明细
    handleView(row) {
      const { id } = row;
      const ref = this.$refs.materialList;
      ref.open = true;
      ref.queryParams.id = id;
    },
    handlePrint() {
      //  'http://192.168.1.94:48080/jmreport/view/786139449334816768?token=ad3787f3f2c84fe697a07e7d27fa24c6'
      if (this.ids === "") {
        this.$modal.msgWarning("请选择你要打印的数据行");
        return;
      }
      const src = `${this.baseUrl}/jmreport/view/${this.reportId}?token=${this.tokens}&id=${this.ids}`;
      this.$refs.issuePrinit.dialogVisible = true;
      this.$refs.issuePrinit.src = src;
    },
    // 表格选中
    tableSelected(val) {
      this.currentRows = val;
      const list = this.currentRows.map((item) => item.outStockNo);
      this.ids = list.join(",");
    },
    select(selection, row) {
      if (selection.length > 1) {
        const del_row = selection.shift();
        this.$refs.multipleTable.toggleRowSelection(del_row, false);
      }
    },
    selectAll(selection) {
      if (selection.length > 1) {
        selection.length = 1;
      }
    },
    // 获取显示隐藏列
    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;
          } else {
            this.columnsList = this.columns;
          }
        })
        .catch((err) => {
          if (err) {
            this.columnsList = this.columns;
          }
        });
    },
  },
};
</script>

<style></style>
