<template>
    <div class="singInWrapper">
        <el-form :model="queryParams" ref="queryForm" inline label-width="68px">
            <el-form-item label="姓名" prop="studenName">
                <el-input v-model="queryParams.studenName" placeholder="请输入姓名" clearable size="small" />
            </el-form-item>
            <el-form-item label="学号" prop="studenCode">
                <el-input v-model="queryParams.studenCode" placeholder="请输入学号" clearable size="small" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        <el-button icon="el-icon-download" type="warning" size="small" style="margin-bottom: 20px" @click="excelExport">导出为excel</el-button>
        <el-table v-loading="loading" :data="signinList">
            <el-table-column label="序号" type="index" />
            <el-table-column label="学生姓名" prop="studenName" align="center" />
            <el-table-column label="学号" prop="studenCode" align="center" />
            <el-table-column v-if="classType == 0" label="专业" prop="major" align="center" />
            <el-table-column label="手机号" prop="mobile" align="center"> </el-table-column>
            <el-table-column label="时间" prop="createTime" align="center"></el-table-column>
            <el-table-column label="操作" align="center">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" @click="delSignInItem(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <pagination
            v-show="total > 0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="signIninit"
        />
    </div>
</template>

<script>
import { getSigninInfo, delSigninItem, exportSignIn } from '@/api/trainService/index'
import { saveAs } from 'file-saver'

export default {
    created() {
        const query = this.$route.query
        this.name = query.name
        this.id = query.id
        this.classType = query.classType
        this.signIninit()
    },
    props: {},
    data() {
        return {
            name: '',
            id: '',
            loading: false,
            total: 0,
            classType:1,
            signinList: [],
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                studenName: '',
                studenCode: '',
            },
            exportParams: {
                studenName: '',
                studenCode: '',
            },
        }
    },
    methods: {
        async signIninit() {
            console.log(this.id)
            const res = await getSigninInfo(this.id,this.queryParams)
            this.total = res.total
            this.signinList = res.rows
        },
        async excelExport() {
            const res = await exportSignIn(this.id,this.exportParams)
            const blob = new Blob([res])
            saveAs(blob, this.name + '-签到记录.xlsx')
            //this.$route.query.name
            // this.download(res)
        },
        delSignInItem(params) {
            this.$confirm('是否确认删除', '警告', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            })
                .then(function () {
                    return delSigninItem(params.signId)
                })
                .then(() => {
                    this.signIninit()
                    this.$message.success('删除成功')
                })
        },
        handleQuery() {
            this.queryParams.pageNum = 1
            this.signIninit()
            this.exportParams.studenName = this.queryParams.studenName
            this.exportParams.studenCode = this.queryParams.studenCode
        },
        resetQuery() {
            this.resetForm('queryForm')
            this.handleQuery()
        },
    },
}
</script>

<style lang="scss" scoped>
.singInWrapper {
    padding: 20px;
    .expBtn {
        display: flex;
        justify-content: space-between;
        margin: 5px;
    }
}
</style>