<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="项目编号" prop="projectCode">
        <el-input v-model="queryParams.projectCode" placeholder="请输入项目编号" clearable style="width: 240px" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="项目名称" prop="projectName">
        <el-input v-model="queryParams.projectName" placeholder="请输入项目名称" clearable style="width: 240px" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="部门" prop="deptName">
        <el-select v-model="queryParams.deptName" placeholder="请输入部门名称" filterable clearable style="width: 240px" @change="handleQuery">
          <el-option v-for="item in deptOptions" :key="item.deptId" :label="item.deptName" :value="item.deptName" />
        </el-select>
      </el-form-item>
      <el-form-item label="项目类别" prop="projectCategory">
        <el-select v-model="queryParams.projectCategory" placeholder="请选择项目类别" clearable style="width: 240px">
          <el-option v-for="dict in project_category" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="月份范围">
        <el-date-picker v-model="monthRange[0]" type="month" value-format="YYYYMM" placeholder="起始月份" style="width: 150px" />
        <span class="el-range-separator" style="margin: 0 5px">-</span>
        <el-date-picker v-model="monthRange[1]" type="month" value-format="YYYYMM" placeholder="结束月份" style="width: 150px" />
      </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-button type="warning" plain icon="Download" @click="handleExport">导出</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="reportList"
      show-summary
      :summary-method="getSummary"
      :span-method="objectSpanMethod"
      :row-class-name="tableRowClassName"
      border
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="60" align="center" />
      <el-table-column label="部门" align="center" prop="deptName" />
      <el-table-column label="项目经理" align="center" prop="managerName" />
      <el-table-column label="项目名称" align="center" prop="projectName" />
      <el-table-column label="项目编号" align="center" prop="projectCode" />
      <el-table-column label="项目类别" align="center" prop="projectCategory">
        <template #default="scope">
          <dict-tag :options="project_category" :value="scope.row.projectCategory" />
        </template>
      </el-table-column>
      <el-table-column :label="totalHoursLabel" align="center" prop="totalHours" />
      <el-table-column label="跨部门工时" align="center" prop="crossDeptHours">
        <template #default="scope">
          {{ Number(scope.row.totalHours) === Number(scope.row.crossDeptHours) ? '' : scope.row.crossDeptHours }}
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script setup name="ProjectManHourReport" lang="ts">
import {
  listProjectManHourReport,
  exportProjectManHourReport,
  ProjectManHourReportVO,
  ProjectManHourReportQuery
} from '@/api/oa/erp/timesheetReport';
import { getErpProjectInfoList } from '@/api/oa/erp/projectInfo';
import { allListDept, listDept } from '@/api/system/dept';
import { FormInstance } from 'element-plus';

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

const reportList = ref<ProjectManHourReportVO[]>([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const monthRange = ref<[string, string]>(['', '']);

const projectOptions = ref<any[]>([]);
const deptOptions = ref<any[]>([]);
const projectLoading = ref(false);
const queryRef = ref<FormInstance>();

const queryParams = ref<ProjectManHourReportQuery>({
  pageNum: 1,
  pageSize: 10000, // 报表暂时不分页或前端分页
  projectCode: undefined,
  projectName: undefined,
  projectCategory: undefined,
  deptName: undefined
});

const totalHoursLabel = computed(() => {
  if (monthRange.value && monthRange.value[0] && monthRange.value[1]) {
    const start = monthRange.value[0];
    const end = monthRange.value[1];
    const fmt = (m: string) => m.substring(0, 4) + '年' + m.substring(4) + '月';
    return `工时 (${fmt(start)} 至 ${fmt(end)})`;
  }
  return '工时';
});

/** 查询列表 */
const getList = async () => {
  loading.value = true;
  const params = { ...queryParams.value };
  if (monthRange.value && monthRange.value[0] && monthRange.value[1]) {
    params.startTime = monthRange.value[0];
    params.endTime = monthRange.value[1];
  }
  const res = await listProjectManHourReport(params);
  reportList.value = res.data;

  if ((res as any).rows) {
    reportList.value = (res as any).rows;
  } else {
    reportList.value = (res as any).data || [];
  }
  getSpanArr(reportList.value);
  loading.value = false;
};

/** 搜索按钮操作 */
const handleQuery = () => {
  getList();
};

/** 多选框选中数据 */
const handleSelectionChange = (selection: ProjectManHourReportVO[]) => {
  console.log('Selection:', selection);
};

/** 重置按钮操作 */
const resetQuery = () => {
  monthRange.value = ['', ''];
  queryRef.value?.resetFields();
  handleQuery();
};

/** 导出按钮操作 */
const handleExport = () => {
  const params = { ...queryParams.value };
  if (monthRange.value && monthRange.value[0] && monthRange.value[1]) {
    params.startTime = monthRange.value[0];
    params.endTime = monthRange.value[1];
  }
  proxy?.download('/oa/erp/timesheetReport/exportProjectManHour', params, `项目工时统计报表_${new Date().getTime()}.xlsx`);
};

/** 合计行计算 */
const getSummary = (param: any) => {
  const { columns, data } = param;
  const sums: string[] = [];
  columns.forEach((column: any, index: number) => {
    if (index === 0) {
      sums[index] = '总计';
      return;
    }
    // 不计算非工时列
    if (!['totalHours'].includes(column.property)) {
      sums[index] = '';
      return;
    }

    const values = data.map((item: any) => Number(item[column.property]));
    if (!values.every((value: any) => Number.isNaN(value))) {
      const total = values.reduce((prev: any, curr: any) => {
        const value = Number(curr);
        if (!Number.isNaN(value)) {
          return prev + curr;
        } else {
          return prev;
        }
      }, 0);
      sums[index] = total.toFixed(1);
    } else {
      sums[index] = '';
    }
  });

  return sums;
};

onMounted(() => {
  getList();
  getDeptList();
  getProjectList();
});

const getProjectList = async () => {
  // 加载所有项目用于前端筛选
  const res = await getErpProjectInfoList({});
  projectOptions.value = res.data || res.rows || [];
};

const getDeptList = async () => {
  // 查询所有部门用于前端筛选
  const res = await allListDept({ deptCategory: '03' } as any);
  deptOptions.value = res.data || res.rows || [];
};

const spanArr = ref<number[]>([]);
const pos = ref(0);

const getSpanArr = (data: ProjectManHourReportVO[]) => {
  spanArr.value = [];
  pos.value = 0;
  for (let i = 0; i < data.length; i++) {
    if (i === 0) {
      spanArr.value.push(1);
      pos.value = 0;
    } else {
      // 判断依据：projectId 相同则合并
      if (data[i].projectId === data[i - 1].projectId) {
        spanArr.value[pos.value] += 1;
        spanArr.value.push(0);
      } else {
        spanArr.value.push(1);
        pos.value = i;
      }
    }
  }
};

const objectSpanMethod = ({
  row,
  column,
  rowIndex,
  columnIndex
}: {
  row: ProjectManHourReportVO;
  column: any;
  rowIndex: number;
  columnIndex: number;
}) => {
  if (columnIndex === 7) {
    const _row = spanArr.value[rowIndex];
    const _col = _row > 0 ? 1 : 0;
    return {
      rowspan: _row,
      colspan: _col
    };
  }
};

/** 行样式 */
const tableRowClassName = ({ row }: { row: ProjectManHourReportVO }) => {
  if (row.crossDeptHours && row.totalHours && Number(row.crossDeptHours) > Number(row.totalHours)) {
    return 'warning-row';
  }
  return '';
};
</script>

<style scoped>
:deep(.warning-row) {
  background-color: #ffffcc !important;
}
</style>
