<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="planCode">
        <el-input
          v-model="queryParams.planCode"
          placeholder="请输入计划编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="派工单号" prop="dispatchCode">
        <el-input
          v-model="queryParams.dispatchCode"
          placeholder="请输入派工单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工位ID" prop="stationId">
        <el-input
          v-model="queryParams.stationId"
          placeholder="请输入工位ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="实际完成时间" prop="realEndTime">
        <el-date-picker clearable
          v-model="queryParams.realEndTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择实际完成时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="计划状态" prop="planStatus">
        <el-select v-model="queryParams.planStatus" placeholder="请选择计划状态" clearable>
          <el-option
            v-for="dict in dict.type.product_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </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="['mes:productplan: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="['mes:productplan: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="['mes:productplan: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="['mes:productplan:export']"
        >导出</el-button>
      </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="productplanList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键标识" align="center" prop="planId" v-if="columns[0].visible"/>
      <el-table-column label="生产工单ID" align="center" prop="productOrderId" v-if="columns[1].visible"/>
      <el-table-column label="计划编号" align="center" prop="planCode" v-if="columns[2].visible"/>
      <el-table-column label="派工单号" align="center" prop="dispatchCode" v-if="columns[3].visible"/>
      <el-table-column label="物料ID" align="center" prop="materialId" v-if="columns[4].visible"/>
      <el-table-column label="物料bomID" align="center" prop="materialBomId" v-if="columns[5].visible"/>
      <el-table-column label="工序ID" align="center" prop="processId" v-if="columns[6].visible"/>
      <el-table-column label="顺序" align="center" prop="processOrder" v-if="columns[7].visible"/>
      <el-table-column label="上一工序ID" align="center" prop="lastProcessId" v-if="columns[8].visible"/>
      <el-table-column label="工位ID" align="center" prop="stationId" v-if="columns[9].visible"/>
      <el-table-column label="用户ID" align="center" prop="userId" v-if="columns[10].visible"/>
      <el-table-column label="标准工时" align="center" prop="productionTime" v-if="columns[11].visible"/>
      <el-table-column label="计划数量" align="center" prop="planAmount" v-if="columns[12].visible"/>
      <el-table-column label="完成数量" align="center" prop="completeAmount" v-if="columns[13].visible"/>
      <el-table-column label="计划开始时间" align="center" prop="planBeginTime" width="180" v-if="columns[14].visible">
        <template slot-scope="scope">
            <span>{{ parseTime(scope.row.planBeginTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="计划结束时间" align="center" prop="planEndTime" width="180" v-if="columns[15].visible">
        <template slot-scope="scope">
            <span>{{ parseTime(scope.row.planEndTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="实际开始时间" align="center" prop="realBeginTime" width="180" v-if="columns[16].visible">
        <template slot-scope="scope">
            <span>{{ parseTime(scope.row.realBeginTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="实际完成时间" align="center" prop="realEndTime" width="180" v-if="columns[17].visible">
        <template slot-scope="scope">
            <span>{{ parseTime(scope.row.realEndTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="附件信息" align="center" prop="attachId" v-if="columns[18].visible"/>
      <el-table-column label="计划状态" align="center" prop="planStatus" v-if="columns[19].visible" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.product_status" :value="scope.row.planStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="是否标识" align="center" prop="isFlag" v-if="columns[20].visible" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.active_flag" :value="scope.row.isFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" v-if="columns[21].visible"/>
      <el-table-column label="创建人" align="center" prop="createBy" v-if="columns[22].visible"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180" v-if="columns[23].visible">
        <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新人" align="center" prop="updateBy" v-if="columns[24].visible"/>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180" v-if="columns[25].visible">
        <template slot-scope="scope">
            <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</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="['mes:productplan:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mes:productplan: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" prop="productOrderId">
          <el-input v-model="form.productOrderId" placeholder="请输入生产工单ID" />
        </el-form-item>
        <el-form-item label="计划编号" prop="planCode">
          <el-input v-model="form.planCode" placeholder="请输入计划编号" />
        </el-form-item>
        <el-form-item label="派工单号" prop="dispatchCode">
          <el-input v-model="form.dispatchCode" placeholder="请输入派工单号" />
        </el-form-item>
        <el-form-item label="物料ID" prop="materialId">
          <el-input v-model="form.materialId" placeholder="请输入物料ID" />
        </el-form-item>
        <el-form-item label="物料bomID" prop="materialBomId">
          <el-input v-model="form.materialBomId" placeholder="请输入物料bomID" />
        </el-form-item>
        <el-form-item label="工序ID" prop="processId">
          <el-input v-model="form.processId" placeholder="请输入工序ID" />
        </el-form-item>
        <el-form-item label="顺序" prop="processOrder">
          <el-input v-model="form.processOrder" placeholder="请输入顺序" />
        </el-form-item>
        <el-form-item label="上一工序ID" prop="lastProcessId">
          <el-input v-model="form.lastProcessId" placeholder="请输入上一工序ID" />
        </el-form-item>
        <el-form-item label="工位ID" prop="stationId">
          <el-input v-model="form.stationId" placeholder="请输入工位ID" />
        </el-form-item>
        <el-form-item label="用户ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户ID" />
        </el-form-item>
        <el-form-item label="标准工时" prop="productionTime">
          <el-input v-model="form.productionTime" placeholder="请输入标准工时" />
        </el-form-item>
        <el-form-item label="计划数量" prop="planAmount">
          <el-input v-model="form.planAmount" placeholder="请输入计划数量" />
        </el-form-item>
        <el-form-item label="完成数量" prop="completeAmount">
          <el-input v-model="form.completeAmount" placeholder="请输入完成数量" />
        </el-form-item>
        <el-form-item label="计划开始时间" prop="planBeginTime">
          <el-date-picker clearable
            v-model="form.planBeginTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择计划开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="计划结束时间" prop="planEndTime">
          <el-date-picker clearable
            v-model="form.planEndTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择计划结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="实际开始时间" prop="realBeginTime">
          <el-date-picker clearable
            v-model="form.realBeginTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择实际开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="实际完成时间" prop="realEndTime">
          <el-date-picker clearable
            v-model="form.realEndTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择实际完成时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="附件信息" prop="attachId">
          <el-input v-model="form.attachId" placeholder="请输入附件信息" />
        </el-form-item>
        <el-form-item label="计划状态" prop="planStatus">
          <el-select v-model="form.planStatus" placeholder="请选择计划状态">
            <el-option
              v-for="dict in dict.type.product_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否标识" prop="isFlag">
          <el-radio-group v-model="form.isFlag">
            <el-radio
              v-for="dict in dict.type.active_flag"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </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="handleAddMesProductPlanDetail">添加</el-button>-->
<!--          </el-col>-->
<!--          <el-col :span="1.5">-->
<!--            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteMesProductPlanDetail">删除</el-button>-->
<!--          </el-col>-->
<!--        </el-row>-->
<!--        <el-table :data="mesProductPlanDetailList" :row-class-name="rowMesProductPlanDetailIndex" @selection-change="handleMesProductPlanDetailSelectionChange" ref="mesProductPlanDetail">-->
<!--          <el-table-column type="selection" width="50" align="center" />-->
<!--          <el-table-column label="序号" align="center" prop="index" width="50"/>-->
<!--          <el-table-column label="明细编号" prop="planDetailCode" width="150">-->
<!--            <template slot-scope="scope">-->
<!--              <el-input v-model="scope.row.planDetailCode" placeholder="请输入明细编号" />-->
<!--            </template>-->
<!--          </el-table-column>-->
<!--          <el-table-column label="计划编号,关联mes_product_plan_info的plan_code" prop="planCode" width="150">-->
<!--            <template slot-scope="scope">-->
<!--              <el-input v-model="scope.row.planCode" placeholder="请输入计划编号,关联mes_product_plan_info的plan_code" />-->
<!--            </template>-->
<!--          </el-table-column>-->
<!--          <el-table-column label="操作人员ID，关联sys_user的user_id;如果后续一个工位有多个人，则一个明细有多个人执行，则可以保存执行记录信息，需要新增表" prop="userId" width="150">-->
<!--            <template slot-scope="scope">-->
<!--              <el-input v-model="scope.row.userId" placeholder="请输入操作人员ID，关联sys_user的user_id;如果后续一个工位有多个人，则一个明细有多个人执行，则可以保存执行记录信息，需要新增表" />-->
<!--            </template>-->
<!--          </el-table-column>-->
<!--          <el-table-column label="操作人员名称，关联sys_user的user_name" prop="userName" width="150">-->
<!--            <template slot-scope="scope">-->
<!--              <el-input v-model="scope.row.userName" placeholder="请输入操作人员名称，关联sys_user的user_name" />-->
<!--            </template>-->
<!--          </el-table-column>-->
<!--          <el-table-column label="开始时间" prop="realBeginTime" width="240">-->
<!--            <template slot-scope="scope">-->
<!--              <el-date-picker clearable v-model="scope.row.realBeginTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择开始时间" />-->
<!--            </template>-->
<!--          </el-table-column>-->
<!--          <el-table-column label="完成时间" prop="realEndTime" width="240">-->
<!--            <template slot-scope="scope">-->
<!--              <el-date-picker clearable v-model="scope.row.realEndTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择完成时间" />-->
<!--            </template>-->
<!--          </el-table-column>-->
<!--          <el-table-column label="明细状态：1-未开始；2-已开始；3-已完成" prop="planDetailStatus" width="150">-->
<!--            <template slot-scope="scope">-->
<!--              <el-select v-model="scope.row.planDetailStatus" placeholder="请选择明细状态：1-未开始；2-已开始；3-已完成">-->
<!--                <el-option label="请选择字典生成" value="" />-->
<!--              </el-select>-->
<!--            </template>-->
<!--          </el-table-column>-->
<!--          <el-table-column label="是否标识：1-是；0-否" prop="isFlag" width="150">-->
<!--            <template slot-scope="scope">-->
<!--              <el-input v-model="scope.row.isFlag" placeholder="请输入是否标识：1-是；0-否" />-->
<!--            </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 { listProductplan, getProductplan, delProductplan, addProductplan, updateProductplan } from "@/api/mes/productplan";

export default {
  name: "Productplan",
  dicts: ['active_flag', 'product_status', 'plan_status', 'dispatch_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedMesProductPlanDetail: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 生产派工表格数据
      productplanList: [],
      // 生产计划明细表格数据
      mesProductPlanDetailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productOrderId: null,
        planCode: null,
        dispatchCode: null,
        materialId: null,
        materialBomId: null,
        processId: null,
        processOrder: null,
        lastProcessId: null,
        stationId: null,
        userId: null,
        productionTime: null,
        planAmount: null,
        completeAmount: null,
        planBeginTime: null,
        planEndTime: null,
        realBeginTime: null,
        realEndTime: null,
        attachId: null,
        planStatus: null,
        isFlag: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        productOrderId: [
          { required: true, message: "生产工单ID不能为空", trigger: "blur" }
        ],
        planCode: [
          { required: true, message: "计划编号不能为空", trigger: "blur" }
        ],
        dispatchCode: [
          { required: true, message: "派工单号不能为空", trigger: "blur" }
        ],
        materialId: [
          { required: true, message: "物料ID不能为空", trigger: "blur" }
        ],
        materialBomId: [
          { required: true, message: "物料bomID不能为空", trigger: "blur" }
        ],
        stationId: [
          { required: true, message: "工位ID不能为空", trigger: "blur" }
        ],
        planStatus: [
          { required: true, message: "计划状态不能为空", trigger: "change" }
        ],
        isFlag: [
          { required: true, message: "是否标识不能为空", trigger: "change" }
        ],
      },
      columns: [
                { key: 0, label: `主键标识`, visible: true },
                { key: 1, label: `生产工单ID`, visible: true },
                { key: 2, label: `计划编号`, visible: true },
                { key: 3, label: `派工单号`, visible: true },
                { key: 4, label: `物料ID`, visible: true },
                { key: 5, label: `物料bomID`, visible: true },
                { key: 6, label: `工序ID`, visible: true },
                { key: 7, label: `顺序`, visible: true },
                { key: 8, label: `上一工序ID`, visible: true },
                { key: 9, label: `工位ID`, visible: true },
                { key: 10, label: `用户ID`, visible: true },
                { key: 11, label: `标准工时`, visible: true },
                { key: 12, label: `计划数量`, visible: true },
                { key: 13, label: `完成数量`, visible: true },
                { key: 14, label: `计划开始时间`, visible: true },
                { key: 15, label: `计划结束时间`, visible: true },
                { key: 16, label: `实际开始时间`, visible: true },
                { key: 17, label: `实际完成时间`, visible: true },
                { key: 18, label: `附件信息`, visible: true },
                { key: 19, label: `计划状态`, visible: true },
                { key: 20, label: `是否标识`, visible: true },
                { key: 21, label: `备注`, visible: true },
                { key: 22, label: `创建人`, visible: true },
                { key: 23, label: `创建时间`, visible: true },
                { key: 24, label: `更新人`, visible: true },
                { key: 25, label: `更新时间`, visible: true },
      ],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询生产派工列表 */
    getList() {
      this.loading = true;
      listProductplan(this.queryParams).then(response => {
        this.productplanList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        planId: null,
        productOrderId: null,
        planCode: null,
        dispatchCode: null,
        materialId: null,
        materialBomId: null,
        processId: null,
        processOrder: null,
        lastProcessId: null,
        stationId: null,
        userId: null,
        productionTime: null,
        planAmount: null,
        completeAmount: null,
        planBeginTime: null,
        planEndTime: null,
        realBeginTime: null,
        realEndTime: null,
        attachId: null,
        planStatus: null,
        isFlag: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.mesProductPlanDetailList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.planId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加生产派工";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const planId = row.planId || this.ids
      getProductplan(planId).then(response => {
        this.form = response.data;
        this.mesProductPlanDetailList = response.data.mesProductPlanDetailList;
        this.open = true;
        this.title = "修改生产派工";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.mesProductPlanDetailList = this.mesProductPlanDetailList;
          if (this.form.planId != null) {
            updateProductplan(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProductplan(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const planIds = row.planId || this.ids;
      this.$modal.confirm('是否确认删除生产派工编号为"' + planIds + '"的数据项？').then(function() {
        return delProductplan(planIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 生产计划明细序号 */
    rowMesProductPlanDetailIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 生产计划明细添加按钮操作 */
    handleAddMesProductPlanDetail() {
      let obj = {};
      obj.planDetailCode = "";
      obj.planCode = "";
      obj.userId = "";
      obj.userName = "";
      obj.realBeginTime = "";
      obj.realEndTime = "";
      obj.planDetailStatus = "";
      obj.isFlag = "";
      obj.remark = "";
      this.mesProductPlanDetailList.push(obj);
    },
    /** 生产计划明细删除按钮操作 */
    handleDeleteMesProductPlanDetail() {
      if (this.checkedMesProductPlanDetail.length == 0) {
        this.$modal.msgError("请先选择要删除的生产计划明细数据");
      } else {
        const mesProductPlanDetailList = this.mesProductPlanDetailList;
        const checkedMesProductPlanDetail = this.checkedMesProductPlanDetail;
        this.mesProductPlanDetailList = mesProductPlanDetailList.filter(function(item) {
          return checkedMesProductPlanDetail.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleMesProductPlanDetailSelectionChange(selection) {
      this.checkedMesProductPlanDetail = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('mes/productplan/export', {
        ...this.queryParams
      }, `productplan_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
