<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='计划编号' prop='planCode'>
              <el-input v-model='queryParams.planCode' placeholder='请输入计划编号' clearable @keyup.enter='handleQuery' />
            </el-form-item>
            <el-form-item label='物料名称' prop='materialName'>
              <el-input v-model='queryParams.materialName' placeholder='请输入物料名称' clearable @keyup.enter='handleQuery' />
            </el-form-item>
            <el-form-item label='机台名称' prop='releaseId'>
              <el-select v-model='queryParams.releaseId' placeholder='请选择机台名称'>
                <el-option
                  v-for='item in releaseList'
                  :key='item.machineId'
                  :label='item.machineName'
                  :value='item.machineId'
                />
              </el-select>
            </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='模号' 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'>

      <el-table v-loading='loading'
                :data='planInfoList'
                ref='planTableRef'
                @selection-change='handleSelectionChange'
                @row-click='handleRowClick'
                highlight-current-row
      >
        <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='120' />
        <el-table-column label='派工单号' align='center' prop='dispatchCode' v-if='columns[6].visible' />
        <el-table-column label='物料名称' align='center' prop='materialName' v-if='columns[7].visible' width='160' />
        <el-table-column label='物料BOM' align='center' prop='materialBomName' v-if='columns[8].visible' width='160' />
        <el-table-column label='工序名称' align='center' prop='processName' 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='下达机台' align='center' prop='releaseName' 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='班次' align='center' prop='shiftName' v-if='columns[28].visible' />
        <el-table-column label='班组' align='center' prop='teamName' 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>

      <pagination v-show='total > 0' :total='total' v-model:page='queryParams.pageNum'
                  v-model:limit='queryParams.pageSize' @pagination='getList' />
    </el-card>
  </div>
</template>

<script setup name='PlanInfo' lang='ts'>
import {
  listPlanInfo,
  getPlanInfo,
  delPlanInfo,
  addPlanInfo,
  updatePlanInfo,
  orderAddProductPlanList, issuePlanInfo
} from '@/api/mes/planInfo';
import { PlanInfoVO, PlanInfoQuery, PlanInfoForm } from '@/api/mes/planInfo/types';
import { getBaseShiftInfoList } from '@/api/mes/baseShiftInfo';
import { getBaseClassTeamInfoList } from '@/api/mes/baseClassTeamInfo';
import BomSelect from '@/views/mes/materialBom/addBom.vue';
import MaterialSelect from '@/views/mes/baseMaterialInfo/addMaterial.vue';
import { getProcessInfoList } from '@/api/mes/baseProcessInfo';
import { getStationInfoList } from '@/api/mes/baseStationInfo';
import { getProdBaseMachineInfoList } from '@/api/mes/prodBaseMachineInfo';
import { ProdBaseMachineInfoVO } from '@/api/mes/prodBaseMachineInfo/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 sfpBatchDialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

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

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

const shiftList = ref([]);
const classTeamList = ref([]);
const processList = ref([]);
let releaseList = ref([]);
const materialBomOpen = ref(false);
const bomSelectRef = ref();
const materialOpen = ref(false);
const materialSelectRef = ref();

const selectedRow = ref({});
const planTableRef = ref();

const workshopId = ref();

const prodBaseMachineInfoList = ref<ProdBaseMachineInfoVO[]>([]);

const machineIds = ref<Array<string | number>>([]);

/*获取班次下拉框*/
const getShiftSelect = async () => {
  let res = await getBaseShiftInfoList(null);
  shiftList.value = res.data;
};

/*获取班组下拉框*/
const getClassTeamSelect = async () => {
  let res = await getBaseClassTeamInfoList(null);
  classTeamList.value = res.data;
};

/*获取半制品工序下拉框*/
const getProcessSelect = async () => {
  let res = await getProcessInfoList({ processType: 1 });
  processList.value = res.data.filter(item => item.processId !== 17 && item.processId !== 18);
};

/*获取工位下拉框*/
const getReleaseSelect = async () => {
  let res = await getProdBaseMachineInfoList({ processId: form.value.processId });
  releaseList.value = res.data;
  prodBaseMachineInfoList.value = res.data;
};

// 列显隐信息
const columns = ref<FieldOption[]>([
  { key: 0, label: `主键标识`, visible: false },
  { key: 1, label: `租户编号`, visible: false },
  { key: 2, label: `生产订单ID`, visible: false },
  { key: 3, label: `销售订单ID`, visible: false },
  { key: 4, label: `销售订单编号`, visible: false },
  { key: 5, label: `计划编号`, visible: true },
  { key: 6, label: `派工单号`, visible: false },
  { key: 7, label: `物料名称`, visible: true },
  { key: 8, label: `物料BOM`, visible: true },
  { key: 9, label: `工序名称`, visible: true },
  { key: 10, label: `顺序`, visible: false },
  { key: 11, label: `上一工序ID`, visible: false },
  { key: 12, label: `最后工序标识`, visible: false },
  { 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: false },
  { key: 26, label: `完成标识`, visible: true },
  { key: 27, label: `优先级`, visible: true },
  { key: 28, label: `班次`, visible: true },
  { key: 29, label: `班组`, 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: '1',
  releaseId: undefined,
  productionTime: undefined,
  planAmount: 1,
  dispatchAmount: undefined,
  completeAmount: undefined,
  planBeginTime: undefined,
  planEndTime: undefined,
  realBeginTime: undefined,
  realEndTime: undefined,
  attachId: undefined,
  planStatus: undefined,
  importFlag: undefined,
  finishFlag: [],
  priority: 1,
  shiftId: undefined,
  classTeamId: undefined,
  modelCode: undefined,
  remark: undefined,
  materialBoMName: undefined,
  workshopId: 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,
    workshopId: undefined,
    params: {}
  },
  rules: {
    materialId: [
      { required: true, message: '物料名称不能为空', trigger: 'blur' }
    ],
    processId: [
      { required: true, message: '工序不能为空', trigger: 'blur' }
    ],
    releaseId: [
      { required: true, message: '机台名称不能为空', trigger: 'blur' }
    ],
    materialName: [
      { required: true, message: '物料BOM不能为空', trigger: 'blur' }
    ],
    planAmount: [
      { required: true, message: '计划数量不能为空', trigger: 'blur' }
    ]
  }
});

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

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

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


/** 搜索按钮操作 */
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 handleRowClick = (row) => {
  selectedRow.value = row;
};
defineExpose({ planTableRef });
onMounted(() => {
  getList();
});


</script>
