<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='beginDate'>
              <el-date-picker clearable
                              v-model='queryParams.beginDate'
                              type='date'
                              value-format='YYYY-MM-DD'
                              placeholder='请选择计划日期'
              />
            </el-form-item>
            <el-form-item label='工序名称' prop='processId'>
              <el-select v-model='queryParams.processId' placeholder='请选择所属工序' @keyup.enter='handleQuery'>
                <el-option
                  v-for='item in processInfoList'
                  :key='item.processId'
                  :label='item.processName'
                  :value='item.processId'
                />
              </el-select>
            </el-form-item>
            <el-form-item label='机台名称' prop='machineId'>
              <el-select v-model='queryParams.machineId' placeholder='请选择机台名称' clearable @keyup.enter='handleQuery'>
                <el-option
                  v-for='item in machineInfoList'
                  :key='item.machineId'
                  :label='item.machineName'
                  :value='item.machineId'
                />
              </el-select>
            </el-form-item>
            <el-form-item label='班次名称' prop='shiftId'>
              <el-select v-model='queryParams.shiftId' placeholder='请选择班次名称' clearable @keyup.enter='handleQuery'>
                <el-option
                  v-for='item in shiftList'
                  :key='item.shiftId'
                  :label='item.shiftName'
                  :value='item.shiftId'
                />
              </el-select>
            </el-form-item>
            <el-form-item label='班组名称' prop='classTeamId'>
              <el-select v-model='queryParams.classTeamId' placeholder='请选择班组名称' clearable @keyup.enter='handleQuery'>
                <el-option
                  v-for='item in classTeamList'
                  :key='item.classTeamId'
                  :label='item.teamName'
                  :value='item.classTeamId'
                />
              </el-select>
            </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='物料名称' prop='materialName'>
              <el-input v-model='queryParams.materialName' placeholder='请输入物料名称' clearable @keyup.enter='handleQuery' />
            </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='warning' plain icon='Download' @click='handleExport'
                       v-hasPermi="['mes:baseWorkshopInfo: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='reportList'>
        <!--        <el-table-column type='selection' width='55' align='center' />-->
        <el-table-column label='物料名称' align='center' prop='materialName' v-if='columns[0].visible' width='300' />
        <el-table-column label='当日计划' align='center' prop='dayPlanNumber' v-if='columns[1].visible' />
        <el-table-column label='当日实际' align='center' prop='dayActualNumber' v-if='columns[2].visible' />
        <el-table-column label='当日差异' align='center' prop='dayDeviationNumber' v-if='columns[3].visible' />
        <el-table-column label='日完成率' align='center' prop='dayCompletionRate' v-if='columns[4].visible' />
        <el-table-column label='当月计划' align='center' prop='monthPlanNumber' v-if='columns[5].visible' />
        <el-table-column label='当月实际' align='center' prop='monthActualNumber' v-if='columns[6].visible' />
        <el-table-column label='当月差异' align='center' prop='monthDeviationNumber' v-if='columns[7].visible' />
        <el-table-column label='月完成率' align='center' prop='monthCompletionRate' v-if='columns[8].visible' />
        <el-table-column label='当年计划' align='center' prop='yearPlanNumber' v-if='columns[9].visible' />
        <el-table-column label='当年实际' align='center' prop='yearActualNumber' v-if='columns[10].visible' />
        <el-table-column label='当年差异' align='center' prop='yearDeviationNumber' v-if='columns[11].visible' />
        <el-table-column label='年完成率' align='center' prop='yearCompletionRate' v-if='columns[12].visible' />

      </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='ReportPlanCompletionContrast' lang='ts'>
import { listPlanCompletionContrastReport } from '@/api/mes/prodReport';
import { getProcessInfoList } from '@/api/mes/baseProcessInfo';
import { getProdBaseMachineInfoList } from '@/api/mes/prodBaseMachineInfo';
import { getBaseShiftInfoList } from '@/api/mes/baseShiftInfo';
import { getBaseClassTeamInfoList } from '@/api/mes/baseClassTeamInfo';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { active_flag } = toRefs<any>(proxy?.useDict('active_flag'));

const reportList = ref<[]>([]);
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 processInfoList = ref([]);
const getProcessInfoListSelect = async () => {
  let res = await getProcessInfoList(null);
  processInfoList.value = res.data;
};

/** 查询机台下拉树结构 */
const machineInfoList = ref([]);
const getProdBaseMachineInfoListSelect = async () => {
  let res = await getProdBaseMachineInfoList({ processId: queryParams.value.processId });
  machineInfoList.value = res.data;
};

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

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

// 列显隐信息
const columns = ref<FieldOption[]>([
  { key: 0, label: '物料名称', visible: true },
  { key: 1, label: '当日计划', visible: true },
  { key: 2, label: '当日实际', visible: true },
  { key: 3, label: '当日差异', visible: true },
  { key: 4, label: '日完成率', visible: true },
  { key: 5, label: '当月计划', visible: true },
  { key: 6, label: '当月实际', visible: true },
  { key: 7, label: '当月差异', visible: true },
  { key: 8, label: '月完成率', visible: true },
  { key: 9, label: '当年计划', visible: true },
  { key: 10, label: '当年实际', visible: true },
  { key: 11, label: '当年差异', visible: true },
  { key: 12, label: '年完成率', visible: true }
]);

const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  processId: 18,
  machineId: undefined,
  beginDate: '',
  params: {}
});

/** 查询车间信息列表 */
const getList = async () => {
  loading.value = true;
  const res = await listPlanCompletionContrastReport(queryParams.value);
  reportList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

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

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

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('mes/prodReport/planCompletionContrastReport/export', {
    ...queryParams.value
  }, `生产计划完成率对比报表_${new Date().getTime()}.xlsx`);
};

watch(
  () => queryParams.value.processId,
  async (newValue, oldValue) => {
    // 监听到变化时执行的函数
    loading.value = true;
    await getProdBaseMachineInfoListSelect();
    await getList();
    loading.value = false;
  }
);

onMounted(() => {
  resetQuery();
  getProcessInfoListSelect();
  getProdBaseMachineInfoListSelect();
  getShiftSelect();
  getClassTeamSelect();
  getList(); // 初始化列表数据
});
</script>
