import { action, makeObservable, observable } from "mobx";
// 用户信息
import BaseStore from "./baseStore";
import { TagDataType } from "@/model/userModel";
import baseHttp from '@/service/base';

class SigninConfig {
    static LISTs: string = "/v1/user/signin"
    static Export: string = "/v1/user/signin/export"

}
class SigninStore extends BaseStore<TagDataType> {
    signList = []
    constructor() {
        super(SigninConfig)
        makeObservable(this, {
            signList: observable,
            getSignList: action,
        })
    }
    async getSignList(query) {
        let list = await baseHttp.get(SigninConfig.LISTs, {
            size: query?.Size ?? 20,
            offset: query?.Offset ?? 1,
            user_name: query?.user_name,
            id_card: query?.id_card ?? '',
            start_time: query?.start_time,
            end_time: query?.end_time,
        })
        this.signList = list?.data.record;
    }

    async exports(query) {
        let res = await baseHttp.downloadFile(SigninConfig.Export, {
            size: query?.Size ?? 20,
            offset: query?.Offset ?? 1,
            user_name: query?.user_name,
            id_card: query?.id_card ?? '',
            start_time: query?.start_time,
            end_time: query?.end_time,
        })
        const url = window.URL.createObjectURL(res.data); // 创建一个临时的URL
        const a = document.createElement('a'); // 创建一个<a>元素
        a.style.display = 'none'; // 隐藏该元素
        a.href = url; // 设置href属性为目标URL
        a.download = '考勤.csv'; // 设置下载后的文件名
        document.body.appendChild(a); // 将其添加到DOM中
        a.click(); // 触发点击事件开始下载
        window.URL.revokeObjectURL(url); // 释放之前创建的URL对象
        document.body.removeChild(a); // 下载完成后可以从DOM中移除该元素

    }
}

export const signinStore = new SigninStore();
export default signinStore;
