<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
<!--            <el-form-item label="生产订单ID" prop="productOrderId">-->
<!--              <el-input v-model="queryParams.productOrderId" placeholder="请输入生产订单ID" clearable @keyup.enter="handleQuery" />-->
<!--            </el-form-item>-->
            <el-form-item label="计划编号" prop="planCode">
              <el-input v-model="queryParams.planCode" placeholder="请输入计划编号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
<!--            <el-form-item label="物料ID" prop="materialId">-->
<!--              <el-input v-model="queryParams.materialId" placeholder="请输入物料ID" clearable @keyup.enter="handleQuery" />-->
<!--            </el-form-item>-->
<!--            <el-form-item label="物料BOM" prop="materialBomId">-->
<!--              <el-input v-model="queryParams.materialBomId" placeholder="请输入物料BOM" clearable @keyup.enter="handleQuery" />-->
<!--            </el-form-item>-->
            <el-form-item label="工序ID" prop="processId">
              <el-input v-model="queryParams.processId" placeholder="请输入工序ID" clearable @keyup.enter="handleQuery" />
            </el-form-item>
<!--            <el-form-item label="下达ID" prop="releaseId">-->
<!--              <el-input v-model="queryParams.releaseId" placeholder="请输入下达ID" clearable @keyup.enter="handleQuery" />-->
<!--            </el-form-item>-->
<!--            <el-form-item label="计划开始时间" prop="planBeginTime">-->
<!--              <el-date-picker clearable-->
<!--                v-model="queryParams.planBeginTime"-->
<!--                type="date"-->
<!--                value-format="YYYY-MM-DD"-->
<!--                placeholder="请选择计划开始时间"-->
<!--              />-->
<!--            </el-form-item>-->
            <el-form-item label="计划状态" prop="planStatus">
              <el-select v-model="queryParams.planStatus" placeholder="请选择计划状态" clearable >
                <el-option v-for="dict in mes_plan_status" :key="dict.value" :label="dict.label" :value="dict.value"/>
              </el-select>
            </el-form-item>
<!--            <el-form-item label="班次ID" prop="shiftId">-->
<!--              <el-input v-model="queryParams.shiftId" placeholder="请输入班次ID" clearable @keyup.enter="handleQuery" />-->
<!--            </el-form-item>-->
<!--            <el-form-item label="班组ID" prop="classTeamId">-->
<!--              <el-input v-model="queryParams.classTeamId" placeholder="请输入班组ID" clearable @keyup.enter="handleQuery" />-->
<!--            </el-form-item>-->
            <el-form-item label="模号" prop="modelCode">
              <el-select v-model="queryParams.modelCode" placeholder="请选择模号" clearable >
                <el-option v-for="dict in mes_model_code" :key="dict.value" :label="dict.label" :value="dict.value"/>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['mes:planInfo:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['mes:planInfo:edit']">修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['mes:planInfo:remove']">删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['mes:planInfo:export']">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" :columns="columns" :search="true" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="planInfoList" @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[2].visible" width="100"/>
        <el-table-column label="销售订单ID" align="center" prop="saleOrderId" v-if="columns[3].visible"/>
        <el-table-column label="销售订单编号" align="center" prop="saleorderCode" v-if="columns[4].visible"/>
        <el-table-column label="计划编号" align="center" prop="planCode" v-if="columns[5].visible" width="100"/>
        <el-table-column label="派工单号" align="center" prop="dispatchCode" v-if="columns[6].visible"/>
        <el-table-column label="物料ID" align="center" prop="materialId" v-if="columns[7].visible"/>
        <el-table-column label="物料BOM" align="center" prop="materialBomId" v-if="columns[8].visible"  width="100"/>
        <el-table-column label="工序ID" align="center" prop="processId" v-if="columns[9].visible"/>
        <el-table-column label="顺序" align="center" prop="processOrder" v-if="columns[10].visible"/>
        <el-table-column label="上一工序ID" align="center" prop="lastProcessId" v-if="columns[11].visible"  width="100"/>
        <el-table-column label="最后工序标识" align="center" prop="finalProcessFlag" v-if="columns[12].visible"  width="110">
          <template #default="scope">
            <dict-tag :options="active_flag" :value="scope.row.finalProcessFlag"/>
          </template>
        </el-table-column>
        <el-table-column label="下达类型" align="center" prop="releaseType" v-if="columns[13].visible">
          <template #default="scope">
            <dict-tag :options="mes_release_type" :value="scope.row.releaseType"/>
          </template>
        </el-table-column>
        <el-table-column label="下达ID" align="center" prop="releaseId" v-if="columns[14].visible"/>
        <el-table-column label="单位生产时间(秒)" align="center" prop="productionTime" v-if="columns[15].visible"/>
        <el-table-column label="计划数量" align="center" prop="planAmount" v-if="columns[16].visible"/>
        <el-table-column label="派工数量" align="center" prop="dispatchAmount" v-if="columns[17].visible"/>
        <el-table-column label="完成数量" align="center" prop="completeAmount" v-if="columns[18].visible"/>
        <el-table-column label="计划开始时间" align="center" prop="planBeginTime" width="180" v-if="columns[19].visible">
          <template #default="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[20].visible">
          <template #default="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[21].visible">
          <template #default="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[22].visible">
          <template #default="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[23].visible"/>
        <el-table-column label="计划状态" align="center" prop="planStatus" v-if="columns[24].visible">
          <template #default="scope">
            <dict-tag :options="mes_plan_status" :value="scope.row.planStatus"/>
          </template>
        </el-table-column>
        <el-table-column label="导入类型" align="center" prop="importFlag" v-if="columns[25].visible">
          <template #default="scope">
            <dict-tag :options="mes_import_flag" :value="scope.row.importFlag"/>
          </template>
        </el-table-column>
        <el-table-column label="完成标识" align="center" prop="finishFlag" v-if="columns[26].visible">
          <template #default="scope">
            <dict-tag :options="mes_finish_flag" :value="scope.row.finishFlag ? scope.row.finishFlag.split(',') : []"/>
          </template>
        </el-table-column>
        <el-table-column label="优先级" align="center" prop="priority" v-if="columns[27].visible"/>
        <el-table-column label="班次ID" align="center" prop="shiftId" v-if="columns[28].visible"/>
        <el-table-column label="班组ID" align="center" prop="classTeamId" v-if="columns[29].visible"/>
        <el-table-column label="模号" align="center" prop="modelCode" v-if="columns[30].visible">
          <template #default="scope">
            <dict-tag :options="mes_model_code" :value="scope.row.modelCode"/>
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center" prop="remark" v-if="columns[31].visible"/>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['mes:planInfo:edit']"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['mes:planInfo:remove']"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
    <!-- 添加或修改生产工单信息对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="500px" append-to-body>
      <el-form ref="planInfoFormRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="生产订单ID" prop="productOrderId">
          <el-input v-model="form.productOrderId" placeholder="请输入生产订单ID" />
        </el-form-item>
<!--        <el-form-item label="销售订单ID" prop="saleOrderId">-->
<!--          <el-input v-model="form.saleOrderId" placeholder="请输入销售订单ID" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="销售订单编号" prop="saleorderCode">-->
<!--          <el-input v-model="form.saleorderCode" placeholder="请输入销售订单编号" />-->
<!--        </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="物料BOM" prop="materialBomId">
          <el-input v-model="form.materialBomId" placeholder="请输入物料BOM" />
        </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-number v-model="form.processOrder" placeholder="请输入顺序" style="width: 200px;"/>
        </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="最后工序标识" prop="finalProcessFlag">-->
<!--          <el-radio-group v-model="form.finalProcessFlag">-->
<!--            <el-radio-->
<!--              v-for="dict in active_flag"-->
<!--              :key="dict.value"-->
<!--              :value="dict.value"-->
<!--            >{{dict.label}}</el-radio>-->
<!--          </el-radio-group>-->
<!--        </el-form-item>-->
        <el-form-item label="下达类型" prop="releaseType">
          <el-radio-group v-model="form.releaseType">
            <el-radio
              v-for="dict in mes_release_type"
              :key="dict.value"
              :value="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="下达ID" prop="releaseId">
          <el-input v-model="form.releaseId" 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-number v-model="form.planAmount" placeholder="请输入计划数量" style="width: 200px;" />
        </el-form-item>
        <el-form-item label="派工数量" prop="dispatchAmount">
          <el-input-number v-model="form.dispatchAmount" placeholder="请输入派工数量" style="width: 200px;" />
        </el-form-item>
        <el-form-item label="计划开始时间" prop="planBeginTime">
          <el-date-picker clearable
            v-model="form.planBeginTime"
            type="datetime"
            value-format="YYYY-MM-DD HH:mm:ss"
            placeholder="请选择计划开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="计划结束时间" prop="planEndTime">
          <el-date-picker clearable
            v-model="form.planEndTime"
            type="datetime"
            value-format="YYYY-MM-DD HH:mm:ss"
            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="priority">
          <el-input-number v-model="form.priority" placeholder="请输入优先级" style="width: 200px;" />
        </el-form-item>
        <el-form-item label="班次ID" prop="shiftId">
          <el-input v-model="form.shiftId" placeholder="请输入班次ID" />
        </el-form-item>
        <el-form-item label="班组ID" prop="classTeamId">
          <el-input v-model="form.classTeamId" placeholder="请输入班组ID" />
        </el-form-item>
        <el-form-item label="模号" prop="modelCode">
          <el-radio-group v-model="form.modelCode">
            <el-radio
              v-for="dict in mes_model_code"
              :key="dict.value"
              :value="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="PlanInfo" lang="ts">
import { listPlanInfo, getPlanInfo, delPlanInfo, addPlanInfo, updatePlanInfo } from '@/api/mes/planInfo';
import { PlanInfoVO, PlanInfoQuery, PlanInfoForm } from '@/api/mes/planInfo/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { mes_import_flag, active_flag, mes_plan_status, mes_release_type, mes_finish_flag, mes_model_code } = toRefs<any>(proxy?.useDict('mes_import_flag', 'active_flag', 'mes_plan_status', 'mes_release_type', 'mes_finish_flag', 'mes_model_code'));

const planInfoList = ref<PlanInfoVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const planInfoFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

// 列显隐信息
const columns = ref<FieldOption[]>([
        { key: 0, label: `主键标识`, visible: false },
        { key: 1, label: `租户编号`, visible: false },
        { key: 2, label: `生产订单ID`, visible: true },
        { key: 3, label: `销售订单ID`, visible: false },
        { key: 4, label: `销售订单编号`, visible: false },
        { key: 5, label: `计划编号`, visible: true },
        { key: 6, label: `派工单号`, visible: true },
        { key: 7, label: `物料ID`, visible: true },
        { key: 8, label: `物料BOM`, visible: true },
        { key: 9, label: `工序ID`, visible: true },
        { key: 10, label: `顺序`, visible: true },
        { key: 11, label: `上一工序ID`, visible: true },
        { key: 12, label: `最后工序标识`, visible: true },
        { key: 13, label: `下达类型`, visible: true },
        { key: 14, label: `下达ID`, visible: true },
        { key: 15, label: `单位生产时间(秒)`, visible: false },
        { 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 },
        { key: 26, label: `完成标识`, visible: true },
        { key: 27, label: `优先级`, visible: true },
        { key: 28, label: `班次ID`, visible: true },
        { key: 29, label: `班组ID`, visible: true },
        { key: 30, label: `模号`, visible: true },
        { key: 31, label: `备注`, visible: false },
        { key: 32, label: `创建部门`, visible: false },
        { key: 33, label: `创建人`, visible: false },
        { key: 34, label: `创建时间`, visible: false },
        { key: 35, label: `更新人`, visible: false },
        { key: 36, label: `更新时间`, visible: false },
]);

const initFormData: PlanInfoForm = {
  planId: undefined,
  productOrderId: undefined,
  saleOrderId: undefined,
  saleorderCode: undefined,
  planCode: undefined,
  dispatchCode: undefined,
  materialId: undefined,
  materialBomId: undefined,
  processId: undefined,
  processOrder: undefined,
  lastProcessId: undefined,
  finalProcessFlag: undefined,
  releaseType: undefined,
  releaseId: undefined,
  productionTime: undefined,
  planAmount: undefined,
  dispatchAmount: undefined,
  completeAmount: undefined,
  planBeginTime: undefined,
  planEndTime: undefined,
  realBeginTime: undefined,
  realEndTime: undefined,
  attachId: undefined,
  planStatus: undefined,
  importFlag: undefined,
  finishFlag: [],
  priority: undefined,
  shiftId: undefined,
  classTeamId: undefined,
  modelCode: undefined,
  remark: undefined,
}
const data = reactive<PageData<PlanInfoForm, PlanInfoQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    planId: undefined,
    productOrderId: undefined,
    saleOrderId: undefined,
    saleorderCode: undefined,
    planCode: undefined,
    dispatchCode: undefined,
    materialId: undefined,
    materialBomId: undefined,
    processId: undefined,
    processOrder: undefined,
    lastProcessId: undefined,
    finalProcessFlag: undefined,
    releaseType: undefined,
    releaseId: undefined,
    productionTime: undefined,
    planAmount: undefined,
    dispatchAmount: undefined,
    completeAmount: undefined,
    planBeginTime: undefined,
    planEndTime: undefined,
    realBeginTime: undefined,
    realEndTime: undefined,
    attachId: undefined,
    planStatus: undefined,
    importFlag: undefined,
    finishFlag: undefined,
    priority: undefined,
    shiftId: undefined,
    classTeamId: undefined,
    modelCode: undefined,
    params: {
    }
  },
  rules: {
    planCode: [
      { required: true, message: "计划编号不能为空", trigger: "blur" }
    ],
    materialId: [
      { required: true, message: "物料ID不能为空", trigger: "blur" }
    ],
    materialBomId: [
      { required: true, message: "物料BOM不能为空", trigger: "blur" }
    ],
    planAmount: [
      { required: true, message: "计划数量不能为空", trigger: "blur" }
    ],
    dispatchAmount: [
      { required: true, message: "派工数量不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询生产工单信息列表 */
const getList = async () => {
  loading.value = true;
  const res = await listPlanInfo(queryParams.value);
  planInfoList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
}

/** 表单重置 */
const reset = () => {
  form.value = {...initFormData};
  planInfoFormRef.value?.resetFields();
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: PlanInfoVO[]) => {
  ids.value = selection.map(item => item.planId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = "添加生产工单信息";
}

/** 修改按钮操作 */
const handleUpdate = async (row?: PlanInfoVO) => {
  reset();
  const _planId = row?.planId || ids.value[0]
  const res = await getPlanInfo(_planId);
  Object.assign(form.value, res.data);
  form.value.finishFlag = form.value.finishFlag.split(",");
  dialog.visible = true;
  dialog.title = "修改生产工单信息";
}

/** 提交按钮 */
const submitForm = () => {
  planInfoFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
        form.value.finishFlag = form.value.finishFlag.join(",");
      if (form.value.planId) {
        await updatePlanInfo(form.value).finally(() =>  buttonLoading.value = false);
      } else {
        await addPlanInfo(form.value).finally(() =>  buttonLoading.value = false);
      }
      proxy?.$modal.msgSuccess("操作成功");
      dialog.visible = false;
      await getList();
    }
  });
}

/** 删除按钮操作 */
const handleDelete = async (row?: PlanInfoVO) => {
  const _planIds = row?.planId || ids.value;
  await proxy?.$modal.confirm('是否确认删除生产工单信息编号为"' + _planIds + '"的数据项？').finally(() => loading.value = false);
  await delPlanInfo(_planIds);
  proxy?.$modal.msgSuccess("删除成功");
  await getList();
}

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('mes/planInfo/export', {
    ...queryParams.value
  }, `planInfo_${new Date().getTime()}.xlsx`)
}

onMounted(() => {
  getList();
});
</script>
