<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="userName">
              <el-input v-model="queryParams.userName" placeholder="请输入人员姓名" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="班组" prop="teamId">
              <el-select v-model="queryParams.teamId" placeholder="全部" clearable>
                <el-option v-for="dict in ProjectTeam" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
            <el-form-item label="工种" prop="typeOfWork">
              <el-select v-model="queryParams.typeOfWork" placeholder="全部" clearable>
                <el-option v-for="dict in type_of_work" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
            <el-form-item label="打卡日期" prop="clockDate">
              <el-date-picker
                clearable
                v-model="queryParams.clockDate"
                type="date"
                value-format="YYYY-MM-DD"
                placeholder="请选择打卡日期"
                @change="selectDate"
              />
            </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-row :gutter="20">
      <el-col :span="24" :offset="0">
        <el-card shadow="hover">
          <!-- <template #header>
            <PieChart style="width: 1em; height: 1em; vertical-align: middle" />
            <span style="vertical-align: middle">命令统计</span>
          </template> -->
          <div class="el-table el-table--enable-row-hover el-table--medium">
            <div ref="commandstats" style="height: 200px" />
          </div>
        </el-card>
      </el-col>
    </el-row>

    <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="['project:attendance:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['project:attendance:edit']"
              >修改</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['project:attendance:remove']"
              >删除</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['project:attendance:export']">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template> -->

      <el-table v-loading="loading" :data="attendanceList">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="主键id" align="center" prop="id" v-if="false" />
        <el-table-column label="人员姓名" align="center" prop="userName" />
        <el-table-column label="班组" align="center" prop="teamName" />
        <el-table-column label="工种" align="center" prop="typeOfWork">
          <template #default="scope">
            <dict-tag :options="type_of_work" :value="scope.row.typeOfWork" />
          </template>
        </el-table-column>
        <el-table-column label="出勤(天)" align="center" prop="attendanceDays" />
        <el-table-column label="迟到(次)" align="center" prop="lateDays" />
        <el-table-column label="早退(次)" align="center" prop="leaveEarlyDays" />
        <el-table-column label="缺卡(次)" align="center" prop="unClockDays" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button link type="primary" icon="View" @click="handleDetails(scope.row)" v-hasPermi="['project:attendance:edit']">详情</el-button>
          </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="attendanceFormRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="人员id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入人员id" />
        </el-form-item>
        <el-form-item label="人脸照" prop="facePic">
          <image-upload v-model="form.facePic" />
        </el-form-item>
        <el-form-item label="项目id" prop="projectId">
          <el-input v-model="form.projectId" placeholder="请输入项目id" />
        </el-form-item>
        <el-form-item label="上班打卡时间" prop="onClockTime">
          <el-date-picker clearable v-model="form.onClockTime" type="datetime" value-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择上班打卡时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="下班打卡时间" prop="offClockTime">
          <el-date-picker clearable v-model="form.offClockTime" type="datetime" value-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择下班打卡时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="打卡日期" prop="clockDate">
          <el-date-picker clearable v-model="form.clockDate" type="datetime" value-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择打卡日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="1正常,2迟到,3早退,4缺勤,5补卡" prop="clockStatus">
          <el-radio-group v-model="form.clockStatus">
            <el-radio v-for="dict in clock_status_type" :key="dict.value" :value="dict.value">{{ dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="代打人员id" prop="pinchUserId">
          <el-input v-model="form.pinchUserId" placeholder="请输入代打人员id" />
        </el-form-item>
        <el-form-item label="多次打卡时间记录" prop="clockRecord">
          <el-input v-model="form.clockRecord" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="上下班" prop="commuter">
          <el-input v-model="form.commuter" placeholder="请输入上下班" />
        </el-form-item>
        <el-form-item label="日薪" prop="dailyWage">
          <el-input v-model="form.dailyWage" placeholder="请输入日薪" />
        </el-form-item>
        <el-form-item label="经度" prop="lng">
          <el-input v-model="form.lng" placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="纬度" prop="lat">
          <el-input v-model="form.lat" placeholder="请输入纬度" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" 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> -->
    <!-- 考勤详情对话框 -->
    <el-dialog v-model="dialog.details" width="1300px">
      <el-calendar ref="calendar" v-model="calendarDay" class="h170 pos-relative">
        <template #header="{ date }">
          <span>
            <el-date-picker v-model="monthValue" type="month" placeholder="请选择月份" @change="handleMonth" />
          </span>
          <span class="label">{{ date }} —{{ dialog.title }}出勤</span>
          <div class="status-detail flex items-center justify-between">
            <div class="dot1">全天考勤正常</div>
            <div class="dot2">当天存在异常迟到、早退、缺卡</div>
            <div class="dot3">当天提交过补卡申请</div>
          </div>
        </template>
        <template #date-cell="{ data }">
          <div class="flex-c" @click="handleViewPlayCard(data)">
            <p class="time">{{ day(data) }}</p>
            <img v-if="!isplayCard(data)" src="@/assets/icons/svg/empty-CZvxqguX.png" /><span v-if="!isplayCard(data)">暂无打卡记录</span>
            <div v-if="isplayCard(data)" class="flex-r"><div class="circle" :class="'status' + attendanceStatus(data)"></div></div>
            <div v-if="isplayCard(data)" class="flex justify-center flex-col w100% items-center">
              <el-button type="primary" plain size="small" class="w70% my-2" v-if="workTime(data)">{{ workTime(data) }} 上班打卡</el-button>
              <el-button type="danger" plain size="small" class="w50% my-2" v-else>上班缺卡</el-button>
              <span></span>
              <el-button type="warning" plain size="small" class="w70%" v-if="workFromTime(data)">{{ workFromTime(data) }} 下班打卡</el-button>
              <el-button type="danger" plain size="small" class="w50%" v-else>下班缺卡</el-button>
            </div>
          </div>
        </template>
      </el-calendar>
    </el-dialog>
  </div>
</template>

<script setup name="Attendance" lang="ts">
import {
  listAttendance,
  getAttendance,
  delAttendance,
  addAttendance,
  updateAttendance,
  listAttendanceTwoWeek,
  listAttendanceMonth
} from '@/api/project/attendance';
import { option } from '@/api/project/attendance/echarts';
import * as echarts from 'echarts';
import { AttendanceVO, AttendanceQuery, AttendanceForm, AttendanceTwoWeekVO, AttendanceMonthVO } from '@/api/project/attendance/types';
import { listProjectTeam } from '@/api/project/projectTeam';
import { ProjectTeamVO } from '@/api/project/projectTeam/types';
import { useUserStoreHook } from '@/store/modules/user';
import { parseTime } from '@/utils/ruoyi';
const commandstats = ref();
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { clock_status_type, type_of_work } = toRefs<any>(proxy?.useDict('clock_status_type', 'type_of_work'));
import type { CalendarDateType, CalendarInstance } from 'element-plus';
// 获取用户 store
const userStore = useUserStoreHook();
// 从 store 中获取项目列表和当前选中的项目
const currentProject = computed(() => userStore.selectedProject);
const ProjectTeam = computed(() => proxy?.$cache.local.getJSON('ProjectTeamList') || []);

const attendanceList = ref<AttendanceVO[]>([]);
const attendanceTwoWeekList = ref<AttendanceTwoWeekVO[]>([]);
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 calendarDay = ref<Date | null>(null);
const monthValue = ref<Date | null>(null);
const calendarList = ref<AttendanceMonthVO[]>();
const queryFormRef = ref<ElFormInstance>();
const attendanceFormRef = ref<ElFormInstance>();
const commandstatsIntance = ref<any>(null);
const dialog = reactive<DialogOption>({
  visible: false,
  details: false,
  title: ''
});
const echartsOption = ref<any>({});
const initFormData: AttendanceForm = {
  id: undefined,
  userId: undefined,
  facePic: undefined,
  onClockTime: undefined,
  offClockTime: undefined,
  clockDate: undefined,
  clockStatus: undefined,
  pinchUserId: undefined,
  clockRecord: undefined,
  commuter: undefined,
  dailyWage: undefined,
  projectId: currentProject.value.id,
  lng: undefined,
  lat: undefined,
  remark: undefined,
  typeOfWork: undefined,
  teamId: undefined
};
const data = reactive<PageData<AttendanceForm, AttendanceQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    userName: undefined,
    clockDate: undefined,
    clockStatus: undefined,
    commuter: undefined,
    projectId: currentProject.value.id,
    typeOfWork: undefined,
    teamId: undefined,
    params: {}
  },
  rules: {
    id: [{ required: true, message: '主键id不能为空', trigger: 'blur' }],
    userId: [{ required: true, message: '人员id不能为空', trigger: 'blur' }],
    facePic: [{ required: true, message: '人脸照不能为空', trigger: 'blur' }],
    projectId: [{ required: true, message: '项目id不能为空', trigger: 'blur' }],
    clockDate: [{ required: true, message: '打卡日期不能为空', trigger: 'blur' }],
    clockStatus: [{ required: true, message: '1正常,2迟到,3早退,4缺勤,5补卡不能为空', trigger: 'change' }]
  }
});

const day = computed(() => (date) => {
  return date.day.split('-').slice(1).join('-');
});
//是否打卡
const isplayCard = computed(() => (date) => {
  return calendarList.value.some((item) => item.clockDate == date.day);
});
//打卡时间下标
const playCardIdx = computed(() => (date) => {
  return calendarList.value.findIndex((item) => item.clockDate == date.day);
});
//上班时间
const workTime = computed(() => (date) => {
  return calendarList.value[playCardIdx.value(date)].attendanceList[0].clockTime?.slice(10);
});

//下班时间
const workFromTime = computed(() => (date) => {
  return calendarList.value[playCardIdx.value(date)].attendanceList[1]?.clockTime?.slice(10);
});

//考勤状态
const attendanceStatus = computed(() => (date) => {
  return calendarList.value[playCardIdx.value(date)].status;
});

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

const calendar = ref<CalendarInstance>();

const handleMonth = async (e: any) => {
  calendarDay.value = e;
  handleCalendarMonth(e);
};

const selectDate = (e: any) => {
  handleQuery();
};

const handleCalendarMonth = async (e?) => {
  let clockMonth;
  if (e) {
    clockMonth = parseTime(e, '{y}-{m}');
  }

  const res = await listAttendanceMonth({ userId: dialog.id, clockMonth });
  calendarList.value = res.data;
};

/** 查看打卡记录详情 */
const handleViewPlayCard = async (data: any) => {
  if (data.type == 'next-month' || data.type == 'prev-month') {
    monthValue.value = data.date;
    handleCalendarMonth(monthValue.value);
  }
};

/** 查询考勤列表 */
const getList = async () => {
  loading.value = true;
  const res = await listAttendance(queryParams.value);
  attendanceList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 查询近两周考勤列表 */
const getListTwoWeek = async () => {
  loading.value = true;
  const res = await listAttendanceTwoWeek(queryParams.value);
  attendanceTwoWeekList.value = res.data;
  echartsOption.value = { ...option(attendanceTwoWeekList.value) };
  commandstatsIntance.value.setOption(echartsOption.value);
};

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

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

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

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

//处理获取到的月份
const incrementMonth = (dateStr: string, monthsToAdd: number) => {
  const [yearPart, monthPart] = dateStr.replace(/\s/g, '').split('年');
  const year = parseInt(yearPart, 10);
  const month = parseInt(monthPart.replace('月', ''), 10);
  // 创建一个新的 Date 对象，设置为输入日期的第一天
  const date = new Date(year, month - 1, 1);

  // 增加一个月
  date.setMonth(date.getMonth() + monthsToAdd);

  // 提取增加一个月后的年份和月份
  const newYear = date.getFullYear();
  const newMonth = String(date.getMonth() + 1).padStart(2, '0');

  // 返回格式化后的日期字符串
  return `${newYear}-${newMonth}`;
};

/** 详情按钮操作 */
const handleDetails = async (row?: AttendanceVO) => {
  const res = await listAttendanceMonth({ userId: row?.id });
  calendarList.value = res.data;
  dialog.details = true;
  dialog.id = row?.id;
  dialog.title = row?.userName || '';
};

/** 提交按钮 */
const submitForm = () => {
  attendanceFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updateAttendance(form.value).finally(() => (buttonLoading.value = false));
      } else {
        await addAttendance(form.value).finally(() => (buttonLoading.value = false));
      }
      proxy?.$modal.msgSuccess('操作成功');
      dialog.visible = false;
      await getList();
    }
  });
};

/** 删除按钮操作 */
// const handleDelete = async (row?: AttendanceVO) => {
//   const _ids = row?.id || ids.value;
//   await proxy?.$modal.confirm('是否确认删除考勤编号为"' + _ids + '"的数据项？').finally(() => (loading.value = false));
//   await delAttendance(_ids);
//   proxy?.$modal.msgSuccess('删除成功');
//   await getList();
// };

/** 导出按钮操作 */
// const handleExport = () => {
//   proxy?.download(
//     'project/attendance/export',
//     {
//       ...queryParams.value
//     },
//     `attendance_${new Date().getTime()}.xlsx`
//   );
// };
//初始化图表
const init = () => {
  commandstatsIntance.value = echarts.init(commandstats.value, 'macarons');

  commandstatsIntance.value.on('click', function (params) {
    queryParams.value.clockDate = params.name;
    handleQuery();
  });
};

//监听项目id刷新数据
const listeningProject = watch(
  () => currentProject.value.id,
  (nid, oid) => {
    queryParams.value.projectId = nid;
    form.value.projectId = nid;
    getList();
  }
);

onUnmounted(() => {
  listeningProject();
});

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

<style lang="scss" scoped>
.label {
  font-size: 24px;
  position: absolute;
  left: 50%;
  top: 0;
  transform: translate(-50%);
  color: #000;
  font-weight: 500;
}
.status-detail {
  margin: 0 15px;
  position: relative;
  font-size: 12px;
  > div {
    margin: 0 15px;
    position: relative;
    font-size: 12px;
    &::before {
      position: absolute;
      content: '';
      display: inline-block;
      left: -15px;
      top: 30%;
      width: 8px;
      height: 8px;
      border-radius: 50%;
    }
  }
  .dot1 {
    &::before {
      background-color: #1d6fe9;
    }
  }
  .dot2 {
    &::before {
      background-color: #f55f4e;
    }
  }
  .dot3 {
    &::before {
      background-color: #ff8d1a;
    }
  }
}
.flex-c {
  height: 110px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  position: relative;
  .time {
    position: absolute;
    z-index: 10;
    right: 0;
    top: 0;
  }
  img {
    width: 50%;
    height: 50%;
  }
  > span {
    font-size: 12px;
    color: #ccc;
    padding-top: 5px;
  }
}
.el-calendar-table__row {
  height: 100px;
}
.flex-r {
  width: 100%;
  display: flex;
  flex-direction: row;
  justify-content: center;
  padding-top: 5px;
  height: 10px;
  .circle {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    margin: 0 2px;
    position: absolute;
    z-index: 10;
    right: 12px;
    top: 35px;
  }
  .status2 {
    background: #f55f4e;
  }
  .status1 {
    background: #1d6fe9;
  }
  .status3 {
    background: #ff8d1a;
  }
}
::v-deep(.el-calendar) {
  .el-calendar__body {
    height: 600px;
    overflow: auto;
  }
  td {
    height: 110px;
    .el-calendar-day {
      height: 100%;
    }
  }
}
</style>
