<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="关联主表ID(sfc_operation_planning_main.id)" prop="mainId">
        <el-input
          v-model="queryParams.mainId"
          placeholder="请输入关联主表ID(sfc_operation_planning_main.id)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="主表FID冗余" prop="fid">
        <el-input
          v-model="queryParams.fid"
          placeholder="请输入主表FID冗余"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单据体内码(K3明细内码)" prop="entityEntryId">
        <el-input
          v-model="queryParams.entityEntryId"
          placeholder="请输入单据体内码(K3明细内码)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工序号" prop="operNumber">
        <el-input
          v-model="queryParams.operNumber"
          placeholder="请输入工序号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工序数量" prop="operQty">
        <el-input
          v-model="queryParams.operQty"
          placeholder="请输入工序数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合格数量" prop="qualifiedQty">
        <el-input
          v-model="queryParams.qualifiedQty"
          placeholder="请输入合格数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="转出数量" prop="transOutQty">
        <el-input
          v-model="queryParams.transOutQty"
          placeholder="请输入转出数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="废品数量" prop="unqualifiedQty">
        <el-input
          v-model="queryParams.unqualifiedQty"
          placeholder="请输入废品数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="损耗数量" prop="wastageQty">
        <el-input
          v-model="queryParams.wastageQty"
          placeholder="请输入损耗数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="转入数量" prop="transInQty">
        <el-input
          v-model="queryParams.transInQty"
          placeholder="请输入转入数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="加工车间" prop="departmentName">
        <el-input
          v-model="queryParams.departmentName"
          placeholder="请输入加工车间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工作中心" prop="workCenterName">
        <el-input
          v-model="queryParams.workCenterName"
          placeholder="请输入工作中心"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="作业" prop="processName">
        <el-input
          v-model="queryParams.processName"
          placeholder="请输入作业"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="作业人员" prop="operator">
        <el-input
          v-model="queryParams.operator"
          placeholder="请输入作业人员"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工序序列-计划开始时间" prop="seqPlanStartTime">
        <el-date-picker clearable
          v-model="queryParams.seqPlanStartTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择工序序列-计划开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="工序序列-计划完成时间" prop="seqPlanFinishTime">
        <el-date-picker clearable
          v-model="queryParams.seqPlanFinishTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择工序序列-计划完成时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="工序控制码" prop="optCtrlCode">
        <el-input
          v-model="queryParams.optCtrlCode"
          placeholder="请输入工序控制码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="关键工序" prop="keyOper">
        <el-input
          v-model="queryParams.keyOper"
          placeholder="请输入关键工序"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="活动一名称" prop="activityName">
        <el-input
          v-model="queryParams.activityName"
          placeholder="请输入活动一名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工序说明" prop="operDescription">
        <el-input
          v-model="queryParams.operDescription"
          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-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="['system:operationPlanningDetail: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="['system:operationPlanningDetail: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="['system:operationPlanningDetail: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="['system:operationPlanningDetail:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="operationPlanningDetailList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键ID" align="center" prop="id" v-if="true"/>
      <el-table-column label="关联主表ID(sfc_operation_planning_main.id)" align="center" prop="mainId" />
      <el-table-column label="主表FID冗余" align="center" prop="fid" />
      <el-table-column label="单据体内码(K3明细内码)" align="center" prop="entityEntryId" />
      <el-table-column label="工序号" align="center" prop="operNumber" />
      <el-table-column label="工序数量" align="center" prop="operQty" />
      <el-table-column label="合格数量" align="center" prop="qualifiedQty" />
      <el-table-column label="转出数量" align="center" prop="transOutQty" />
      <el-table-column label="废品数量" align="center" prop="unqualifiedQty" />
      <el-table-column label="损耗数量" align="center" prop="wastageQty" />
      <el-table-column label="转入数量" align="center" prop="transInQty" />
      <el-table-column label="加工车间" align="center" prop="departmentName" />
      <el-table-column label="工作中心" align="center" prop="workCenterName" />
      <el-table-column label="作业" align="center" prop="processName" />
      <el-table-column label="作业人员" align="center" prop="operator" />
      <el-table-column label="状态" align="center" prop="operStatus" />
      <el-table-column label="工序序列-计划开始时间" align="center" prop="seqPlanStartTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.seqPlanStartTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="工序序列-计划完成时间" align="center" prop="seqPlanFinishTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.seqPlanFinishTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="工序控制码" align="center" prop="optCtrlCode" />
      <el-table-column label="关键工序" align="center" prop="keyOper" />
      <el-table-column label="活动一名称" align="center" prop="activityName" />
      <el-table-column label="工序说明" align="center" prop="operDescription" />
      <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="['system:operationPlanningDetail:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:operationPlanningDetail: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="关联主表ID(sfc_operation_planning_main.id)" prop="mainId">
          <el-input v-model="form.mainId" placeholder="请输入关联主表ID(sfc_operation_planning_main.id)" />
        </el-form-item>
        <el-form-item label="主表FID冗余" prop="fid">
          <el-input v-model="form.fid" placeholder="请输入主表FID冗余" />
        </el-form-item>
        <el-form-item label="单据体内码(K3明细内码)" prop="entityEntryId">
          <el-input v-model="form.entityEntryId" placeholder="请输入单据体内码(K3明细内码)" />
        </el-form-item>
        <el-form-item label="工序号" prop="operNumber">
          <el-input v-model="form.operNumber" placeholder="请输入工序号" />
        </el-form-item>
        <el-form-item label="工序数量" prop="operQty">
          <el-input v-model="form.operQty" placeholder="请输入工序数量" />
        </el-form-item>
        <el-form-item label="合格数量" prop="qualifiedQty">
          <el-input v-model="form.qualifiedQty" placeholder="请输入合格数量" />
        </el-form-item>
        <el-form-item label="转出数量" prop="transOutQty">
          <el-input v-model="form.transOutQty" placeholder="请输入转出数量" />
        </el-form-item>
        <el-form-item label="废品数量" prop="unqualifiedQty">
          <el-input v-model="form.unqualifiedQty" placeholder="请输入废品数量" />
        </el-form-item>
        <el-form-item label="损耗数量" prop="wastageQty">
          <el-input v-model="form.wastageQty" placeholder="请输入损耗数量" />
        </el-form-item>
        <el-form-item label="转入数量" prop="transInQty">
          <el-input v-model="form.transInQty" placeholder="请输入转入数量" />
        </el-form-item>
        <el-form-item label="加工车间" prop="departmentName">
          <el-input v-model="form.departmentName" placeholder="请输入加工车间" />
        </el-form-item>
        <el-form-item label="工作中心" prop="workCenterName">
          <el-input v-model="form.workCenterName" placeholder="请输入工作中心" />
        </el-form-item>
        <el-form-item label="作业" prop="processName">
          <el-input v-model="form.processName" placeholder="请输入作业" />
        </el-form-item>
        <el-form-item label="作业人员" prop="operator">
          <el-input v-model="form.operator" placeholder="请输入作业人员" />
        </el-form-item>
        <el-form-item label="工序序列-计划开始时间" prop="seqPlanStartTime">
          <el-date-picker clearable
            v-model="form.seqPlanStartTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择工序序列-计划开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="工序序列-计划完成时间" prop="seqPlanFinishTime">
          <el-date-picker clearable
            v-model="form.seqPlanFinishTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择工序序列-计划完成时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="工序控制码" prop="optCtrlCode">
          <el-input v-model="form.optCtrlCode" placeholder="请输入工序控制码" />
        </el-form-item>
        <el-form-item label="关键工序" prop="keyOper">
          <el-input v-model="form.keyOper" placeholder="请输入关键工序" />
        </el-form-item>
        <el-form-item label="活动一名称" prop="activityName">
          <el-input v-model="form.activityName" placeholder="请输入活动一名称" />
        </el-form-item>
        <el-form-item label="工序说明" prop="operDescription">
          <el-input v-model="form.operDescription" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOperationPlanningDetail, getOperationPlanningDetail, delOperationPlanningDetail, addOperationPlanningDetail, updateOperationPlanningDetail } from "@/api/system/operationPlanningDetail";

export default {
  name: "OperationPlanningDetail",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工序计划明细表格数据
      operationPlanningDetailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        mainId: undefined,
        fid: undefined,
        entityEntryId: undefined,
        operNumber: undefined,
        operQty: undefined,
        qualifiedQty: undefined,
        transOutQty: undefined,
        unqualifiedQty: undefined,
        wastageQty: undefined,
        transInQty: undefined,
        departmentName: undefined,
        workCenterName: undefined,
        processName: undefined,
        operator: undefined,
        operStatus: undefined,
        seqPlanStartTime: undefined,
        seqPlanFinishTime: undefined,
        optCtrlCode: undefined,
        keyOper: undefined,
        activityName: undefined,
        operDescription: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          { required: true, message: "主键ID不能为空", trigger: "blur" }
        ],
        mainId: [
          { required: true, message: "关联主表ID(sfc_operation_planning_main.id)不能为空", trigger: "blur" }
        ],
        fid: [
          { required: true, message: "主表FID冗余不能为空", trigger: "blur" }
        ],
        entityEntryId: [
          { required: true, message: "单据体内码(K3明细内码)不能为空", trigger: "blur" }
        ],
        operNumber: [
          { required: true, message: "工序号不能为空", trigger: "blur" }
        ],
        operQty: [
          { required: true, message: "工序数量不能为空", trigger: "blur" }
        ],
        qualifiedQty: [
          { required: true, message: "合格数量不能为空", trigger: "blur" }
        ],
        transOutQty: [
          { required: true, message: "转出数量不能为空", trigger: "blur" }
        ],
        unqualifiedQty: [
          { required: true, message: "废品数量不能为空", trigger: "blur" }
        ],
        wastageQty: [
          { required: true, message: "损耗数量不能为空", trigger: "blur" }
        ],
        transInQty: [
          { required: true, message: "转入数量不能为空", trigger: "blur" }
        ],
        departmentName: [
          { required: true, message: "加工车间不能为空", trigger: "blur" }
        ],
        workCenterName: [
          { required: true, message: "工作中心不能为空", trigger: "blur" }
        ],
        processName: [
          { required: true, message: "作业不能为空", trigger: "blur" }
        ],
        operator: [
          { required: true, message: "作业人员不能为空", trigger: "blur" }
        ],
        operStatus: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
        seqPlanStartTime: [
          { required: true, message: "工序序列-计划开始时间不能为空", trigger: "blur" }
        ],
        seqPlanFinishTime: [
          { required: true, message: "工序序列-计划完成时间不能为空", trigger: "blur" }
        ],
        optCtrlCode: [
          { required: true, message: "工序控制码不能为空", trigger: "blur" }
        ],
        keyOper: [
          { required: true, message: "关键工序不能为空", trigger: "blur" }
        ],
        activityName: [
          { required: true, message: "活动一名称不能为空", trigger: "blur" }
        ],
        operDescription: [
          { required: true, message: "工序说明不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询工序计划明细列表 */
    getList() {
      this.loading = true;
      listOperationPlanningDetail(this.queryParams).then(response => {
        this.operationPlanningDetailList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        mainId: undefined,
        fid: undefined,
        entityEntryId: undefined,
        operNumber: undefined,
        operQty: undefined,
        qualifiedQty: undefined,
        transOutQty: undefined,
        unqualifiedQty: undefined,
        wastageQty: undefined,
        transInQty: undefined,
        departmentName: undefined,
        workCenterName: undefined,
        processName: undefined,
        operator: undefined,
        operStatus: undefined,
        seqPlanStartTime: undefined,
        seqPlanFinishTime: undefined,
        optCtrlCode: undefined,
        keyOper: undefined,
        activityName: undefined,
        operDescription: undefined,
        createTime: undefined,
        updateTime: undefined,
        createBy: undefined,
        updateBy: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加工序计划明细";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getOperationPlanningDetail(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改工序计划明细";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateOperationPlanningDetail(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addOperationPlanningDetail(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除工序计划明细编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delOperationPlanningDetail(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/operationPlanningDetail/export', {
        ...this.queryParams
      }, `operationPlanningDetail_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
