<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="moBillNo">
        <el-input
          v-model="queryParams.moBillNo"
          placeholder="请输入生产订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生产令号" prop="moOrderNo">
        <el-input
          v-model="queryParams.moOrderNo"
          placeholder="请输入生产令号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品编码" prop="materialNumber">
        <el-input
          v-model="queryParams.materialNumber"
          placeholder="请输入产品编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="materialName">
        <el-input
          v-model="queryParams.materialName"
          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="transInQty">
        <el-input
          v-model="queryParams.transInQty"
          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="materialStatus">
        <el-select v-model="queryParams.materialStatus" placeholder="请选择物料状态" clearable>
          <el-option
            v-for="dict in dict.type.work_center"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </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="processName">
        <el-input
          v-model="queryParams.processName"
          placeholder="请输入工序名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计划开始时间" prop="operPlanStartTime">
        <el-input
          v-model="queryParams.operPlanStartTime"
          placeholder="请输入计划开始时间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计划完成时间" prop="operPlanFinishTime">
        <el-input
          v-model="queryParams.operPlanFinishTime"
          placeholder="请输入计划完成时间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="延迟天数" prop="delayDays">
        <el-input
          v-model="queryParams.delayDays"
          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:workCenterData: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:workCenterData: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:workCenterData: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:workCenterData:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="workCenterDataList" @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="生产订单号" align="center" prop="moBillNo" />
      <el-table-column label="生产令号" align="center" prop="moOrderNo" />
      <el-table-column label="产品编码" align="center" prop="materialNumber" />
      <el-table-column label="产品名称" align="center" prop="materialName" />
      <el-table-column label="生产数量" align="center" prop="operQty" />
      <el-table-column label="转入数量" align="center" prop="transInQty" />
      <el-table-column label="转出数量" align="center" prop="transOutQty" />
      <el-table-column label="物料状态" align="center" prop="materialStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.work_center" :value="scope.row.materialStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="工序号" align="center" prop="operNumber" />
      <el-table-column label="工序名称" align="center" prop="processName" />
      <el-table-column label="计划开始时间" align="center" prop="operPlanStartTime" />
      <el-table-column label="计划完成时间" align="center" prop="operPlanFinishTime" />
      <el-table-column label="延迟天数" align="center" prop="delayDays" />
      <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:workCenterData:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:workCenterData: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="moBillNo">
          <el-input v-model="form.moBillNo" placeholder="请输入生产订单号" />
        </el-form-item>
        <el-form-item label="生产令号" prop="moOrderNo">
          <el-input v-model="form.moOrderNo" placeholder="请输入生产令号" />
        </el-form-item>
        <el-form-item label="产品编码" prop="materialNumber">
          <el-input v-model="form.materialNumber" placeholder="请输入产品编码" />
        </el-form-item>
        <el-form-item label="产品名称" prop="materialName">
          <el-input v-model="form.materialName" 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="transInQty">
          <el-input v-model="form.transInQty" 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="materialStatus">
          <el-radio-group v-model="form.materialStatus">
            <el-radio
              v-for="dict in dict.type.work_center"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="工序号" prop="operNumber">
          <el-input v-model="form.operNumber" 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="operPlanStartTime">
          <el-input v-model="form.operPlanStartTime" placeholder="请输入计划开始时间" />
        </el-form-item>
        <el-form-item label="计划完成时间" prop="operPlanFinishTime">
          <el-input v-model="form.operPlanFinishTime" placeholder="请输入计划完成时间" />
        </el-form-item>
        <el-form-item label="延迟天数" prop="delayDays">
          <el-input v-model="form.delayDays" 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 { listWorkCenterData, getWorkCenterData, delWorkCenterData, addWorkCenterData, updateWorkCenterData } from "@/api/system/workCenterData";

export default {
  name: "WorkCenterData",
  dicts: ['work_center'],
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 金蝶工段数据表格数据
      workCenterDataList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        moBillNo: undefined,
        moOrderNo: undefined,
        materialNumber: undefined,
        materialName: undefined,
        operQty: undefined,
        transInQty: undefined,
        transOutQty: undefined,
        materialStatus: undefined,
        operNumber: undefined,
        processName: undefined,
        operPlanStartTime: undefined,
        operPlanFinishTime: undefined,
        delayDays: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          { required: true, message: "主键ID不能为空", trigger: "blur" }
        ],
        moBillNo: [
          { required: true, message: "生产订单号不能为空", trigger: "blur" }
        ],
        moOrderNo: [
          { required: true, message: "生产令号不能为空", trigger: "blur" }
        ],
        materialNumber: [
          { required: true, message: "产品编码不能为空", trigger: "blur" }
        ],
        materialName: [
          { required: true, message: "产品名称不能为空", trigger: "blur" }
        ],
        operQty: [
          { required: true, message: "生产数量不能为空", trigger: "blur" }
        ],
        transInQty: [
          { required: true, message: "转入数量不能为空", trigger: "blur" }
        ],
        transOutQty: [
          { required: true, message: "转出数量不能为空", trigger: "blur" }
        ],
        materialStatus: [
          { required: true, message: "物料状态不能为空", trigger: "change" }
        ],
        operNumber: [
          { required: true, message: "工序号不能为空", trigger: "blur" }
        ],
        processName: [
          { required: true, message: "工序名称不能为空", trigger: "blur" }
        ],
        operPlanStartTime: [
          { required: true, message: "计划开始时间不能为空", trigger: "blur" }
        ],
        operPlanFinishTime: [
          { required: true, message: "计划完成时间不能为空", trigger: "blur" }
        ],
        delayDays: [
          { required: true, message: "延迟天数不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "更新时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询金蝶工段数据列表 */
    getList() {
      this.loading = true;
      listWorkCenterData(this.queryParams).then(response => {
        this.workCenterDataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        moBillNo: undefined,
        moOrderNo: undefined,
        materialNumber: undefined,
        materialName: undefined,
        operQty: undefined,
        transInQty: undefined,
        transOutQty: undefined,
        materialStatus: undefined,
        operNumber: undefined,
        processName: undefined,
        operPlanStartTime: undefined,
        operPlanFinishTime: undefined,
        delayDays: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: 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
      getWorkCenterData(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) {
            updateWorkCenterData(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addWorkCenterData(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 delWorkCenterData(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/workCenterData/export', {
        ...this.queryParams
      }, `workCenterData_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
