<template>
  <div class="app-container">
    <!-- 搜索工作栏 -->
    <el-form
      v-show="showSearch"
      ref="queryForm"
      :model="queryParams"
      size="small"
      label-width="68px"
    >
      <QueryFormWrapper>
        <el-col :span="8">
          <el-form-item label="领料单号" prop="workOrderBomNo">
            <el-input
              v-model="queryParams.workOrderBomNo"
              placeholder="请输入领料单号"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="生产订单" prop="workOrderNo">
            <el-input
              v-model="queryParams.workOrderNo"
              placeholder="请输入生产订单"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="物料编码" prop="materialNo">
            <el-input
              v-model="queryParams.materialNo"
              placeholder="请输入物料编码"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <div slot="right">
          <el-form-item label-width="0" class="text-right" style="word">
            <EasyAdvancedQuery
              class="mr10"
              label-width="98px"
              @advQuery="advQuery"
            >
              <template #default="{ model }">
                <el-row :gutter="10" type="flex">
                  <el-col :span="12">
                    <el-form-item label="物料名称" prop="materialName">
                      <el-input
                        v-model="model.materialName"
                        placeholder="请输入物料名称"
                        clearable
                        @keyup.enter.native="handleQuery"
                      />
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="单据状态" prop="status">
                      <el-select
                        v-model="model.status"
                        placeholder="请选择单据状态"
                        clearable
                        size="small"
                      >
                        <el-option
                          v-for="dict in getDictDatas(
                            DICT_TYPE.WMS_DELIVERY_STATUS
                          )"
                          :key="dict.value"
                          :label="dict.label"
                          :value="dict.value"
                        />
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="单据日期" prop="createTime">
                      <el-date-picker
                        v-model="model.createTime"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        type="daterange"
                        range-separator="-"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
                        :default-time="['00:00:00', '23:59:59']"
                      />
                    </el-form-item>
                  </el-col>
                </el-row>
              </template>
            </EasyAdvancedQuery>
            <el-button type="primary" icon="el-icon-search" @click="handleQuery"
              >搜索</el-button
            >
            <el-button icon="el-icon-refresh" @click="resetQuery"
              >重置</el-button
            >
          </el-form-item>
        </div>
      </QueryFormWrapper>
    </el-form>

    <!-- 右侧工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          size="mini"
          type="danger"
          plain
          icon="el-icon-delete"
          @click="handleDelete"
          >删除</el-button
        >
      </el-col>
      <right-toolbar
        :columns="columnsList"
        :show-search.sync="showSearch"
        @saveSetting="getColumnsList"
        @queryTable="getList"
      />
    </el-row>

    <!-- 列表 -->
    <AutoResizer :given-height="177">
      <template #default="{ height }">
        <el-table
          v-if="columnsList.length > 0"
          v-loading="loading"
          :data="list"
          border
          :height="height"
          :span-method="arraySpanMethod"
          @selection-change="tableSelected"
        >
          <el-table-column
            type="selection"
            width="55"
            fixed="left"
            align="center"
          />
          <el-table-column
            v-if="columnsList[0].visible"
            label="生产领料单"
            align="center"
            prop="workOrderBomNo"
            width="160"
            fixed="left"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[1].visible"
            label="生产订单"
            align="center"
            prop="workOrderNo"
            width="160"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[2].visible"
            label="签收状态"
            align="center"
            prop="signStatus"
            width="120"
          >
            <template slot-scope="scope">
              <dict-tag
                :type="DICT_TYPE.WMS_SIGN_STATUS"
                :value="scope.row.signStatus"
              />
            </template>
          </el-table-column>
          <el-table-column
            v-if="columnsList[3].visible"
            label="物料编码"
            align="center"
            prop="erpMaterialId"
            width="160"
          />
          <el-table-column
            v-if="columnsList[4].visible"
            label="物料名称"
            align="center"
            prop="materialName"
            width="150"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[5].visible"
            label="供应商"
            align="center"
            prop="supplierCode"
            width="160"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[6].visible"
            label="仓库"
            align="center"
            prop="wmsStockId"
            width="120"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[7].visible"
            label="区域"
            align="center"
            prop="wmsStockAreaId"
            width="120"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[8].visible"
            label="货位"
            align="center"
            prop="wmsStockLocationId"
            width="120"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[9].visible"
            label="批次数量"
            align="center"
            prop="batchQty"
          />
          <el-table-column
            v-if="columnsList[10].visible"
            label="发料人"
            align="center"
            prop="creator"
          />
          <el-table-column
            v-if="columnsList[11].visible"
            label="发料单号"
            align="center"
            prop="outStockNo"
            width="160"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[12].visible"
            label="物料批次"
            align="center"
            prop="wmsLabelMasterId"
            width="170"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[13].visible"
            label="单位"
            align="center"
            prop="unitCode"
          />
          <el-table-column
            v-if="columnsList[14].visible"
            label="单据状态"
            align="center"
            prop="status"
          />
          <el-table-column
            v-if="columnsList[15].visible"
            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"
            fixed="right"
          >
            <template slot-scope="scope">
              <el-button
                v-hasPermi="['wms:out-stock-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>
      </template>
    </AutoResizer>

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

<script>
import {
  getOutStockMainDetailPage,
  deleteStockDetail,
} from "@/api/wms/inquirePurchase/complexDocuments";
import { getFormColumnsList } from "@/api/system/saveColumns/index";
import { mapGetters } from "vuex";
import { isArray } from "min-dash";

export default {
  name: "ProductPicking",
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // mes生产领料单主表-同步列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 高级查询参数
      advParams: {},
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        relatedId: null,
        workOrderBomNo: null,
        workOrderNo: null,
        productAmount: null,
        productCode: null,
        productName: null,
        bomVersion: null,
        receiveStatus: null,
        sendStatus: null,
        progress: null,
        createTime: [],
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        relatedId: [
          { required: true, message: "关联mes主表ID不能为空", trigger: "blur" },
        ],
        workOrderBomNo: [
          { required: true, message: "领料单号不能为空", trigger: "blur" },
        ],
        workOrderNo: [
          { required: true, message: "生产订单不能为空", trigger: "blur" },
        ],
      },
      // 显示隐藏列参数
      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 },
      ],
      // 显示隐藏持久化参数
      columnsList: [],
      // 表格选中
      currentRows: [],
    };
  },
  // 获取当前登录用户id
  computed: {
    ...mapGetters(["userId"]),
  },
  created() {
    this.queryParams.workOrderBomNo = this.$route.query.workOrderBomNo;
    // 获取显隐列列表
    this.getColumnsList();
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      const params = { ...this.queryParams, ...this.advParams };
      getOutStockMainDetailPage(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,
        relatedId: undefined,
        workOrderBomNo: undefined,
        workOrderNo: undefined,
        productAmount: undefined,
        productCode: undefined,
        productName: undefined,
        bomVersion: undefined,
        receiveStatus: undefined,
        sendStatus: undefined,
        progress: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.advParams = {};
      this.handleQuery();
    },
    // 删除领料记录
    handleDelete() {
      if (this.currentRows.length === 0) {
        this.$modal.msgWarning("请选择您要删除的记录!");
        return;
      }
      const id = this.currentRows
        .map((item) => {
          const { id } = item;
          return id;
        })
        .join(",");
      // const { id, workOrderBomNo } = row
      const message = `是否确认删除选中的${this.currentRows.length}条领料记录?`;
      this.$modal
        .confirm(message)
        .then(function () {
          return deleteStockDetail(id);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功,请在erp系统同步删除!");
        })
        .catch(() => {});
    },
    // 表格选中
    tableSelected(val) {
      this.currentRows = val;
    },
    // 表格行合并
    arraySpanMethod({ row, column, rowIndex, columnIndex }) {
      // const newData = this.list.map((item, index) => {
      //   const filterData = this.list.filter(ele => {
      //     return item.workOrderBomNo === ele.workOrderBomNo
      //   })
      //   const dataIndex = this.list.findIndex(eleIndex => {
      //     return item.workOrderBomNo === eleIndex.workOrderBomNo
      //   })
      //   if (index === dataIndex) {
      //     item.rowspan = filterData.length
      //   } else {
      //     item.rowspan = 0
      //   }
      //   return newData
      // })
      // if (columnIndex === 0 || columnIndex === 1) {
      //   return {
      //     rowspan: row.rowspan,
      //     colspan: 1
      //   }
      // } else {
      //   return false
      // }
    },
    // 高级查询
    advQuery(advQueryParams) {
      this.advParams = advQueryParams;
      this.handleQuery();
    },
    // 获取显示隐藏列
    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>
