<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="仓库ID" prop="warehouseId">
        <el-input
          v-model="queryParams.warehouseId"
          placeholder="请输入仓库ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </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="moveWay">
        <el-input
          v-model="queryParams.moveWay"
          placeholder="请输入移库方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </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="applyDate">
        <el-date-picker clearable
          v-model="queryParams.applyDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择申请时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="审核人" prop="auditBy">
        <el-input
          v-model="queryParams.auditBy"
          placeholder="请输入审核人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核时间" prop="auditDate">
        <el-date-picker clearable
          v-model="queryParams.auditDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择审核时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="最后更新时间" prop="updateDate">
        <el-date-picker clearable
          v-model="queryParams.updateDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择最后更新时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="开始时间" prop="beginTime">
        <el-date-picker clearable
          v-model="queryParams.beginTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker clearable
          v-model="queryParams.endTime"
          type="date"
          value-format="yyyy-MM-dd"
          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-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['wms:move:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['wms:move:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['wms:move:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['wms:move:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="moveList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="原材料移库合库记录ID" align="center" prop="moveId" />
      <el-table-column label="任务编号" align="center" prop="taskCode" />
      <el-table-column label="仓库ID" align="center" prop="warehouseId" />
      <el-table-column label="原库位编码" align="center" prop="oriLocationCode" />
      <el-table-column label="目标库位编码" align="center" prop="targetLocationCode" />
      <el-table-column label="操作类型(0自动,1人工,2强制,3调度)" align="center" prop="operationType" />
      <el-table-column label="移库方式" align="center" prop="moveWay" />
      <el-table-column label="移库类型(1移库，2合库)" align="center" prop="moveType" />
      <el-table-column label="申请原因" align="center" prop="applyReason" />
      <el-table-column label="审核原因" align="center" prop="auditReason" />
      <el-table-column label="审核状态(0待审核,1审核通过,2审核未通过)" align="center" prop="auditStatus" />
      <el-table-column label="执行状态(0待执行,1执行中,2执行完成)" align="center" prop="executeStatus" />
      <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="auditBy" />
      <el-table-column label="审核时间" align="center" prop="auditDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.auditDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最后更新时间" align="center" prop="updateDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="开始时间" align="center" prop="beginTime" width="180">
        <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" width="180">
        <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-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['wms:move:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['wms:move:remove']"
          >删除</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"
    />

    <!-- 添加或修改移库合库记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="任务编号" prop="taskCode">
          <el-input v-model="form.taskCode" placeholder="请输入任务编号" />
        </el-form-item>
        <el-form-item label="仓库ID" prop="warehouseId">
          <el-input v-model="form.warehouseId" placeholder="请输入仓库ID" />
        </el-form-item>
        <el-form-item label="原库位编码" prop="oriLocationCode">
          <el-input v-model="form.oriLocationCode" placeholder="请输入原库位编码" />
        </el-form-item>
        <el-form-item label="目标库位编码" prop="targetLocationCode">
          <el-input v-model="form.targetLocationCode" placeholder="请输入目标库位编码" />
        </el-form-item>
        <el-form-item label="移库方式" prop="moveWay">
          <el-input v-model="form.moveWay" placeholder="请输入移库方式" />
        </el-form-item>
        <el-form-item label="申请原因" prop="applyReason">
          <el-input v-model="form.applyReason" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="审核原因" prop="auditReason">
          <el-input v-model="form.auditReason" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="申请人" prop="applyBy">
          <el-input v-model="form.applyBy" placeholder="请输入申请人" />
        </el-form-item>
        <el-form-item label="申请时间" prop="applyDate">
          <el-date-picker clearable
            v-model="form.applyDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择申请时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="审核人" prop="auditBy">
          <el-input v-model="form.auditBy" placeholder="请输入审核人" />
        </el-form-item>
        <el-form-item label="审核时间" prop="auditDate">
          <el-date-picker clearable
            v-model="form.auditDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择审核时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="最后更新时间" prop="updateDate">
          <el-date-picker clearable
            v-model="form.updateDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择最后更新时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="开始时间" prop="beginTime">
          <el-date-picker clearable
            v-model="form.beginTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker clearable
            v-model="form.endTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-divider content-position="center">移库合库记录明细;原材料入库记录对应的明细信息信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddWmsMoveDetail">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteWmsMoveDetail">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="wmsMoveDetailList" :row-class-name="rowWmsMoveDetailIndex" @selection-change="handleWmsMoveDetailSelectionChange" ref="wmsMoveDetail">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="库位编码" prop="locationCode" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.locationCode" placeholder="请输入库位编码" />
            </template>
          </el-table-column>
          <el-table-column label="存放物料条码,入库扫描条码时，从打印条码记录表中获取，关联条码信息的barcode_info" prop="materialBarcode" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.materialBarcode" placeholder="请输入存放物料条码,入库扫描条码时，从打印条码记录表中获取，关联条码信息的barcode_info" />
            </template>
          </el-table-column>
          <el-table-column label="批次;入库扫描条码时，从打印条码记录表中获取,关联条码信息的batch_code" prop="instockBatch" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.instockBatch" placeholder="请输入批次;入库扫描条码时，从打印条码记录表中获取,关联条码信息的batch_code" />
            </template>
          </el-table-column>
          <el-table-column label="物料ID" prop="materialId" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.materialId" placeholder="请输入物料ID" />
            </template>
          </el-table-column>
          <el-table-column label="计划数量" prop="planAmount" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.planAmount" placeholder="请输入计划数量" />
            </template>
          </el-table-column>
          <el-table-column label="实际数量" prop="realAmount" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.realAmount" placeholder="请输入实际数量" />
            </template>
          </el-table-column>
          <el-table-column label="执行状态(0待执行,1执行中,2执行完成)" prop="executeStatus" width="150">
            <template slot-scope="scope">
              <el-select v-model="scope.row.executeStatus" placeholder="请选择执行状态(0待执行,1执行中,2执行完成)">
                <el-option label="请选择字典生成" value="" />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="执行人" prop="executePerson" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.executePerson" placeholder="请输入执行人" />
            </template>
          </el-table-column>
          <el-table-column label="执行时间" prop="executeTime" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.executeTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择执行时间" />
            </template>
          </el-table-column>
          <el-table-column label="移库明细类型(1出库2入库)" prop="moveDetailType" width="150">
            <template slot-scope="scope">
              <el-select v-model="scope.row.moveDetailType" placeholder="请选择移库明细类型(1出库2入库)">
                <el-option label="请选择字典生成" value="" />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="使用机台名称;入库扫描条码时，从打印条码记录表中获取" prop="machineName" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.machineName" placeholder="请输入使用机台名称;入库扫描条码时，从打印条码记录表中获取" />
            </template>
          </el-table-column>
          <el-table-column label="创建时间" prop="createDate" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.createDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择创建时间" />
            </template>
          </el-table-column>
          <el-table-column label="最后更新时间" prop="updateDate" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.updateDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择最后更新时间" />
            </template>
          </el-table-column>
          <el-table-column label="有效标记" prop="activeFlag" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.activeFlag" placeholder="请输入有效标记" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMove, getMove, delMove, addMove, updateMove } from "@/api/wms/move";

export default {
  name: "Move",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedWmsMoveDetail: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 移库合库记录表格数据
      moveList: [],
      // 移库合库记录明细;原材料入库记录对应的明细信息表格数据
      wmsMoveDetailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        taskCode: null,
        warehouseId: null,
        oriLocationCode: null,
        targetLocationCode: 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: {},
      // 表单校验
      rules: {
        warehouseId: [
          { required: true, message: "仓库ID不能为空", trigger: "blur" }
        ],
        oriLocationCode: [
          { required: true, message: "原库位编码不能为空", trigger: "blur" }
        ],
        targetLocationCode: [
          { required: true, message: "目标库位编码不能为空", trigger: "blur" }
        ],
        operationType: [
          { required: true, message: "操作类型(0自动,1人工,2强制,3调度)不能为空", trigger: "change" }
        ],
        moveWay: [
          { required: true, message: "移库方式不能为空", trigger: "blur" }
        ],
        moveType: [
          { required: true, message: "移库类型(1移库，2合库)不能为空", trigger: "change" }
        ],
        auditStatus: [
          { required: true, message: "审核状态(0待审核,1审核通过,2审核未通过)不能为空", trigger: "change" }
        ],
        executeStatus: [
          { required: true, message: "执行状态(0待执行,1执行中,2执行完成)不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询移库合库记录列表 */
    getList() {
      this.loading = true;
      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,
        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.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.moveId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加移库合库记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const moveId = row.moveId || this.ids
      getMove(moveId).then(response => {
        this.form = response.data;
        this.wmsMoveDetailList = response.data.wmsMoveDetailList;
        this.open = true;
        this.title = "修改移库合库记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.wmsMoveDetailList = this.wmsMoveDetailList;
          if (this.form.moveId != null) {
            updateMove(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMove(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const moveIds = row.moveId || this.ids;
      this.$modal.confirm('是否确认删除移库合库记录编号为"' + moveIds + '"的数据项？').then(function() {
        return delMove(moveIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 移库合库记录明细;原材料入库记录对应的明细信息序号 */
    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`)
    }
  }
};
</script>
