<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="日期" style="width: 300px">
              <el-date-picker
                v-model="dateRange"
                value-format="YYYY-MM-DD"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-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" />
        </el-row>
      </template>

      <el-table v-loading="loading" :data="reportList">
        <el-table-column label="派工单" align="center" prop="dispatchCode" v-if="columns[0].visible" />
        <el-table-column label="班组" align="center" prop="teamName" v-if="columns[1].visible" />
        <el-table-column label="工位" align="center" prop="stationName" v-if="columns[2].visible" />
        <el-table-column label="物料名称" align="center" prop="materialName" v-if="columns[3].visible" width="260" />
        <el-table-column label="工序名称" align="center" prop="processName" v-if="columns[4].visible" />
        <el-table-column label="报工数量" align="center" prop="completeAmount" v-if="columns[5].visible" />
        <el-table-column label="标准工时(h)" align="center" prop="standardWorkHour" v-if="columns[6].visible" />
        <el-table-column label="报工工时(h)" align="center" prop="reportWorkHour" v-if="columns[7].visible" />
        <el-table-column label="生产效率" align="center" prop="productionEfficiency" v-if="columns[8].visible" />
        <el-table-column label="派工单状态" align="center" prop="planStatus" v-if="columns[9].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="remark" v-if="columns[10].visible" show-overflow-tooltip />
      </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="ReportWorkHour" lang="ts">
import { getCurrentInstance, ref, watch, onMounted } from 'vue';
import type { ComponentInternalInstance } from 'vue';
import type { ElFormInstance } from 'element-plus';
import { listWorkHourReport } 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 {
  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 reportList = ref<[]>([]);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const queryFormRef = ref<ElFormInstance>();
const dateRange = ref<string[]>(['', '']);

// 下拉数据
const processInfoList = ref([]);
const machineInfoList = ref([]);
const shiftList = ref([]);
const classTeamList = ref([]);

// 列显隐信息
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: '标准工时(h)', visible: true },
  { key: 7, label: '报工工时(h)', visible: true },
  { key: 8, label: '生产效率', visible: true },
  { key: 9, label: '派工单状态', visible: true },
  { key: 10, label: '备注', visible: true }
]);

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

// 下拉获取
const getProcessInfoListSelect = async () => {
  const res = await getProcessInfoList(null);
  processInfoList.value = res.data;
};

const getProdBaseMachineInfoListSelect = async () => {
  const res = await getProdBaseMachineInfoList({ processId: queryParams.value.processId });
  machineInfoList.value = res.data;
};

const getShiftSelect = async () => {
  const res = await getBaseShiftInfoList(null);
  shiftList.value = res.data;
};

const getClassTeamSelect = async () => {
  const res = await getBaseClassTeamInfoList(null);
  classTeamList.value = res.data;
};

// 列表
const getList = async () => {
  loading.value = true;
  queryParams.value.beginDate = dateRange.value?.[0];
  queryParams.value.endDate = dateRange.value?.[1];
  const res = await listWorkHourReport(queryParams.value);
  reportList.value = res.rows || [];
  total.value = res.total || 0;
  loading.value = false;
};

// 搜索
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

// 重置
const resetQuery = () => {
  queryFormRef.value?.resetFields?.();
  const nowDate = proxy?.parseTime(new Date(), '{y}-{m}-{d}');
  dateRange.value = [nowDate, nowDate];
  queryParams.value.processId = 18;
  queryParams.value.machineId = undefined;
  queryParams.value.shiftId = undefined;
  queryParams.value.classTeamId = undefined;
  queryParams.value.planCode = '';
  queryParams.value.materialName = '';
  handleQuery();
};

// 导出
const handleExport = () => {
  queryParams.value.beginDate = dateRange.value?.[0];
  queryParams.value.endDate = dateRange.value?.[1];
  proxy?.download('mes/prodReport/workHourReport/export', { ...queryParams.value }, `工时报表_${new Date().getTime()}.xlsx`);
};

// 监听工序变化联动机台
watch(
  () => queryParams.value.processId,
  async () => {
    loading.value = true;
    await getProdBaseMachineInfoListSelect();
    await getList();
    loading.value = false;
  }
);

onMounted(async () => {
  resetQuery();
  await getProcessInfoListSelect();
  await getProdBaseMachineInfoListSelect();
  await getShiftSelect();
  await getClassTeamSelect();
  await getList();
});
</script>
