<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="88px">
      <el-form-item label="类型" prop="moveType">
        <el-select
          v-model="queryParams.moveType"
          placeholder="请选择类型"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.wms_move_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>


      <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" placeholder="请选择仓库" clearable>
          <el-option
            v-for="(category, index) in warehouseOptions"
            :key="index"
            :label="category.warehouseName"
            :value="category.warehouseId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="原库位" prop="oriLocationCode">
        <el-input
          v-model="queryParams.oriLocationCode"
          placeholder="请输入原库位编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="目标库位" prop="targetLocationCode">
        <el-input
          v-model="queryParams.targetLocationCode"
          placeholder="请输入目标库位编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="状态" prop="executeStatus">
        <el-select
          v-model="queryParams.executeStatus"
          placeholder="请选择状态"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.wms_move_status"
            :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 label="申请时间" prop="endTime">
        <el-date-picker
          v-model="dateRangeApplyTime"
          style="width: 340px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="-"
          start-placeholder=""
          end-placeholder=""
        ></el-date-picker>
      </el-form-item>


      <el-form-item label="开始时间" prop="beginTime">
        <el-date-picker
          v-model="dateRangeBeginTime"
          style="width: 340px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="-"
          start-placeholder=""
          end-placeholder=""
        ></el-date-picker>
      </el-form-item>

      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker
          v-model="dateRangeEndTime"
          style="width: 340px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="-"
          start-placeholder=""
          end-placeholder=""
        ></el-date-picker>
      </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="moveList" @selection-change="handleSelectionChange">
      <el-table-column label="类型" align="center" prop="moveType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.wms_move_type" :value="scope.row.moveType"/>
        </template>
      </el-table-column>
      <el-table-column label="任务编号" align="center" prop="taskCode"/>
      <el-table-column label="仓库" align="center" prop="warehouseName"/>
      <el-table-column label="原库位" align="center" prop="oriLocationCode"/>
      <el-table-column label="目标库位" align="center" prop="targetLocationCode"/>
      <el-table-column label="执行状态" align="center" prop="executeStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.wms_move_status" :value="scope.row.executeStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="申请人" align="center" prop="applyBy"/>
      <el-table-column label="申请时间" align="center" prop="applyDate">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.applyDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="开始时间" align="center" prop="beginTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.beginTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <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="handleOpenDetail(scope.row)"
            v-hasPermi="['wms:move:detail']"
          >详情
          </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 {listMove, getWarehouses} from "@/api/wms/move";

export default {
  name: "Move",
  dicts: ['wms_move_type', 'wms_move_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedWmsMoveDetail: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 移库合库记录表格数据
      moveList: [],
      // 移库合库记录明细表格数据
      wmsMoveDetailList: [],
      // 开始时间时间范围
      dateRangeApplyTime: [],
      // 开始时间时间范围
      dateRangeBeginTime: [],
      // 结束时间时间范围
      dateRangeEndTime: [],

      warehouseOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        taskCode: null,
        warehouseId: null,
        oriLocationCode: null,
        targetLocationCode: null,
        instockBatch: null,
        materialId: null,
        planAmount: null,
        realOutstockAmount: null,
        realInstockAmount: null,
        operationType: null,
        moveWay: null,
        moveType: null,
        applyReason: null,
        auditReason: null,
        auditStatus: null,
        executeStatus: null,
        applyBy: null,
        applyDate: null,
        auditBy: null,
        auditDate: null,
        updateDate: null,
        beginTime: null,
        endTime: null
      },
      // 表单参数
      form: {},

    };
  },
  created() {
    this.getWarehouses();
    this.getList();
  },
  methods: {
    /** 查询移库合库记录列表 */
    getList() {
      this.loading = true;
      this.queryParams = this.addDateRange(this.queryParams, this.dateRangeApplyTime, "ApplyTime");

      this.queryParams = this.addDateRange(this.queryParams, this.dateRangeBeginTime, "BeginTime");

      this.queryParams = this.addDateRange(this.queryParams, this.dateRangeEndTime, "EndTime");

      listMove(this.queryParams).then(response => {
        this.moveList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        moveId: null,
        taskCode: null,
        warehouseId: null,
        oriLocationCode: null,
        targetLocationCode: null,
        instockBatch: null,
        materialName: null,
        planAmount: null,
        realOutstockAmount: null,
        realInstockAmount: null,
        operationType: null,
        moveWay: null,
        moveType: null,
        applyReason: null,
        auditReason: null,
        auditStatus: null,
        executeStatus: null,
        applyBy: null,
        applyDate: null,
        auditBy: null,
        auditDate: null,
        updateBy: null,
        updateDate: null,
        beginTime: null,
        endTime: null
      };
      this.wmsMoveDetailList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRangeApplyTime = [];
      this.dateRangeBeginTime = [];
      this.dateRangeEndTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.moveId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },

    /** 移库合库记录明细序号 */
    rowWmsMoveDetailIndex({row, rowIndex}) {
      row.index = rowIndex + 1;
    },
    /** 移库合库记录明细添加按钮操作 */
    handleAddWmsMoveDetail() {
      let obj = {};
      obj.locationCode = "";
      obj.materialBarcode = "";
      obj.instockBatch = "";
      obj.materialId = "";
      obj.planAmount = "";
      obj.realAmount = "";
      obj.executeStatus = "";
      obj.executePerson = "";
      obj.executeTime = "";
      obj.moveDetailType = "";
      obj.machineName = "";
      obj.createDate = "";
      obj.updateDate = "";
      obj.activeFlag = "";
      this.wmsMoveDetailList.push(obj);
    },
    /** 移库合库记录明细删除按钮操作 */
    handleDeleteWmsMoveDetail() {
      if (this.checkedWmsMoveDetail.length == 0) {
        this.$modal.msgError("请先选择要删除的移库合库记录明细数据");
      } else {
        const wmsMoveDetailList = this.wmsMoveDetailList;
        const checkedWmsMoveDetail = this.checkedWmsMoveDetail;
        this.wmsMoveDetailList = wmsMoveDetailList.filter(function (item) {
          return checkedWmsMoveDetail.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleWmsMoveDetailSelectionChange(selection) {
      this.checkedWmsMoveDetail = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('wms/move/export', {
        ...this.queryParams
      }, `move_${new Date().getTime()}.xlsx`)
    },

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

    handleOpenDetail(row) {
      const moveId = row.moveId;
      const taskCode = row.taskCode
      const warehouseName = row.warehouseName;
      const materialCode = row.materialCode;
      const materialName = row.materialName;
      const params = {pageNum: this.queryParams.pageNum};
      const moveType = row.moveType;
      let title = "";
      if (moveType === "1") {//移库
        title = "移库详情[" + taskCode + "]"
      } else {
        title = "合库详情[" + taskCode + "]"
      }
      this.$tab.openPage(title, '/wms/move/detail/' + moveId + '/' + taskCode + '/' + warehouseName + '/' + materialCode + '/' + materialName, params);
    },

  }
};
</script>
