/**
 * 请假逻辑
 * 作者:lxm
 * 包括工时填报
 */

import moment = require("moment");
import { ASKFORLEAVETYPE } from "../config/enum";
import { addData, selectData } from "../data/admin/askForLeave";
import { changeEnumValue } from "../util/tools";



/**
 * 请假
 * @param des 描述 
 * @param startTime 开始时间 
 * @param endTime 结束时间
 * @param long 时长
 * @param type 请假类型
 */
export async function askForLeave(userId:string, des:string, startTime:number, endTime:number, type:number, time:number, day:number) {
    
    let st = new Date(startTime);
    let et = new Date(endTime);

    await addData(des, st, et, userId, time, type, day);

    return {isSuccess:true, message:"添加成功"}
}


export async function myAskForLeaveList(userId:string) {
    let dataList = await selectData(userId);

    let result = [];
    dataList.forEach(info => {
        let { id, des, startTime, endTime, time, type, day } = info;
        let changeType = changeEnumValue(ASKFORLEAVETYPE, type);
        let changeStartTime = moment(startTime).format("YYYY/MM/DD HH:mm");
        let changeEndTimee = moment(endTime).format("YYYY/MM/DD HH:mm");
        let timeStr = `${day}日${time}小时`;
        result.push({id, des, startTime:changeStartTime, endTime:changeEndTimee, time:timeStr, type:changeType});
    });

    return {dataList:result}
}
