<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="任务编号" prop="taskCode">
        <el-input
          v-model="queryParams.taskCode"
          placeholder="请输入任务编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库" prop="warehouseId">
        <el-select v-model="queryParams.warehouseId">
          <el-option
            v-for="item in warehouseOptions"
            :key="item.warehouseId"
            :label="item.warehouseName"
            :value="item.warehouseId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="计划编号" prop="planCode">
        <el-input
          v-model="queryParams.planCode"
          placeholder="请输入计划编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="明细编号" prop="planDetailCode">
        <el-input
          v-model="queryParams.planDetailCode"
          placeholder="请输入计划明细编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料条码" prop="materialBarcode">
        <el-input
          v-model="queryParams.materialBarcode"
          placeholder="请输入物料条码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="退库类型" prop="taskType">
        <el-select
          v-model="queryParams.taskType"
          placeholder="请选择退库类型"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.wms_raw_return_task_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="申请人" prop="applyBy">
        <el-input
          v-model="queryParams.applyBy"
          placeholder="请输入申请人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-table v-loading="loading" :data="rawreturnList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="任务编号" align="center" prop="taskCode"/>
      <el-table-column label="仓库" align="center" prop="warehouseName"/>
      <el-table-column label="计划编号" align="center" prop="planCode"/>
      <el-table-column label="计划明细编号" align="center" prop="planDetailCode"/>
      <el-table-column label="物料条码" align="center" prop="materialBarcode"/>
      <el-table-column label="计划退库数量" align="center" prop="planAmount"/>
      <el-table-column label="退库类型" align="center" prop="taskType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.wms_raw_return_task_type" :value="scope.row.taskType"/>
        </template>
      </el-table-column>
      <el-table-column label="申请人" align="center" prop="applyBy"/>
      <el-table-column label="申请时间" align="center" prop="applyDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.applyDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="申请原因" align="center" prop="applyReason"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-info"
            @click="handleDetail(scope.row)"
            v-hasPermi="['wms:rawreturn:audit:query']"
          >查看
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


  </div>
</template>

<script>
import {listRawreturn, getWarehouses,} from "@/api/wms/rawreturn";

export default {
  name: "Rawreturn",
  dicts: ['wms_audit_status', 'wms_execute_status', 'wms_raw_return_task_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedWmsRawReturnDetail: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 原材料退库记录表格数据
      rawreturnList: [],
      // 原材料退库记录明细表格数据
      wmsRawReturnDetailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        taskCode: null,
        warehouseId: null,
        locationCode: null,
        planCode: null,
        planDetailCode: null,
        productId: null,
        materialId: null,
        materialBarcode: null,
        batchCode: null,
        planAmount: null,
        returnAmount: null,
        operationType: null,
        taskType: null,
        endStationCode: null,
        applyReason: null,
        auditReason: null,
        auditStatus: null,
        executeStatus: null,
        applyBy: null,
        applyDate: null,
        auditBy: null,
        auditDate: null,
        updateDate: null,
        beginTime: null,
        endTime: null,
        erpStatus: null,
        erpAmount: null
      },
      // 表单参数
      form: {},
      // 表单校验
      // 仓库列表
      warehouseOptions: [],
    };
  },
  created() {
    this.getList();
    this.getWarehouseList();
  },
  activated() {
    const time = this.$route.query.t;
    if (time != null && time != this.uniqueId) {
      this.uniqueId = time;
      this.queryParams.pageNum = Number(this.$route.query.pageNum);
      this.getList();
    }
  },
  methods: {
    /** 查询原材料退库记录列表 */
    getList() {
      this.loading = true;
      listRawreturn(this.queryParams).then(response => {
        this.rawreturnList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 查询仓库列表 */
    getWarehouseList() {
      getWarehouses().then(response => {
        this.warehouseOptions = response.data;
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.rawReturnId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },

    /** 审核按钮操作 */
    handleDetail(row) {
      const rawReturnId = row.rawReturnId
      const taskCode = row.taskCode
      const warehouseName = row.warehouseName;
      const params = {pageNum: this.queryParams.pageNum};
      this.$tab.openPage("退库详情[" + taskCode + "]", '/wms/raw-return/detail/' + rawReturnId + '/1/' + warehouseName, params);
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download('wms/rawreturn/export', {
        ...this.queryParams
      }, `rawreturn_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
