<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
            label-width="68px">
            <el-form-item label="学号" prop="stuNo">
                <el-input v-model="queryParams.stuNo" placeholder="请输入学号" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="姓名" prop="name">
                <el-input v-model="queryParams.name" placeholder="请输入姓名" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="学生状态" prop="status">
                <el-select v-model="queryParams.status" placeholder="请选择学生状态" clearable style="width: 240px">
                    <el-option v-for="dict in dict.type.srs_stu_status" :key="dict.value" :label="dict.label"
                        :value="dict.value" />
                </el-select>
            </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-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                    v-hasPermi="['system:student:add']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                    v-hasPermi="['system:student:edit']">修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
                    @click="handleDelete" v-hasPermi="['system:student:remove']">删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="info" plain icon="el-icon-upload2" size="mini" @click="handleImport"
                    v-hasPermi="['system:stuent:import']">导入</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                    v-hasPermi="['system:student:export']">导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
        <el-table v-loading="loading" :data="studentList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="学号" align="center" prop="stuNo" width="150" />
            <el-table-column label="姓名" align="center" prop="name" width="100" />
            <el-table-column label="性别" align="center" prop="gender" />
            <!-- <el-table-column label="生日" align="center" prop="birthday" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.birthday, '{y}-{m}-{d}') }}</span>
          </template>
</el-table-column> -->
            <!-- <el-table-column label="身份证" align="center" prop="idCard" /> -->
            <el-table-column label="学院名称" align="center" prop="deptId">
                <template slot-scope="scope">
                    <span>{{ scope.row.dept.deptName }}</span>
                </template>
            </el-table-column>

            <el-table-column label="专业名称" align="center" prop="majorId" width="250">
                <template slot-scope="scope">
                    <span>{{ scope.row.srsMajors.majorName }}</span>
                </template>
            </el-table-column>
            <!-- <el-table-column label="年级id" align="center" prop="gradeId" /> -->
            <el-table-column label="班级名称" align="center" prop="classId" width="270">
                <template slot-scope="scope">
                    <span>{{ scope.row.srsClass.className }}</span>
                </template>
            </el-table-column>
            <!-- <el-table-column label="手机号码" align="center" prop="phone" />
        <el-table-column label="家庭地址" align="center" prop="address" /> -->
            <el-table-column label="学生状态" align="center" prop="status">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.srs_stu_status" :value="scope.row.status" />
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                        >修改</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                        >删除</el-button>
                    <el-button size="mini" type="text" icon="el-icon-refresh"
                        @click="doInitPwdOne(scope.row)">初始化密码</el-button>
                    <el-button size="mini" type="text" icon="el-icon-view"
                        @click="picVClick(scope.row)">查看画像</el-button>
                    <el-button size="mini" type="text" icon="el-icon-view"
                        @click="lookDetail(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="getList" />
        <!-- 添加或修改学生信息对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="姓名" prop="name">
                    <el-input v-model="form.name" placeholder="请输入姓名" />
                </el-form-item>
                <el-form-item label="学号" prop="stuNo">
                    <el-input v-model="form.stuNo" placeholder="请输入学号" />
                </el-form-item>
                <el-form-item label="性别" prop="gender">
                    <el-input v-model="form.gender" placeholder="请输入性别" />
                </el-form-item>
                <el-form-item label="生日" prop="birthday">
                    <el-date-picker clearable v-model="form.birthday" type="date" value-format="yyyy-MM-dd"
                        placeholder="请选择生日">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="身份证号码" prop="idCard">
                    <el-input v-model="form.idCard" placeholder="请输入身份证号码" readonly="true" />
                </el-form-item>
                <el-form-item label="所属班级" prop="classId">
                    <el-cascader placeholder="请选择班级" v-model="classVlue2" :show-all-levels="false"
                        :options="ClassNameList" @change="handleChange2" clearable filterable>
                        <template slot-scope="{ node, data }">
                            <span>{{ data.label }}</span>
                            <span v-if="!node.isLeaf"> ({{ data.children.length }}) </span>
                        </template>
                    </el-cascader>

                </el-form-item>
                <el-form-item label="手机号码" prop="phone">
                    <el-input v-model="form.phone" placeholder="请输入手机号码" />
                </el-form-item>
                <el-form-item label="家庭地址" prop="address">
                    <el-input v-model="form.address" placeholder="请输入家庭地址" />
                </el-form-item>
                <!-- <el-form-item label="删除标志" prop="delFlag">
            <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
          </el-form-item> -->
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
        <!-- 用户导入对话框 -->
        <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
            <el-upload ref="upload" :limit="1" accept=".xlsx, .xls" :headers="upload.headers"
                :action="upload.url + '?updateSupport=' + upload.updateSupport" :disabled="upload.isUploading"
                :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess" :auto-upload="false" drag>
                <i class="el-icon-upload"></i>
                <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
                <div class="el-upload__tip text-center" slot="tip">
                    <!-- <div class="el-upload__tip" slot="tip">
                        <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的学生数据
                    </div> -->
                    <span>仅允许导入xls、xlsx格式文件。</span>
                    <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;"
                        @click="importTemplate">下载模板</el-link>
                </div>
            </el-upload>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitFileForm">确 定</el-button>
                <el-button @click="upload.open = false">取 消</el-button>
            </div>
        </el-dialog>
        <!-- 用户导入学生状态对话框 -->
        <el-dialog :title="upload2.title" :visible.sync="upload2.open" width="400px" append-to-body>
            <el-upload ref="upload2" :limit="1" accept=".xlsx, .xls" :headers="upload2.headers"
                :action="upload2.url + '?updateSupport=' + upload2.updateSupport" :disabled="upload2.isUploading"
                :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess2" :auto-upload="false" drag>
                <i class="el-icon-upload"></i>
                <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
                <div class="el-upload__tip text-center" slot="tip">
                    <div class="el-upload__tip" slot="tip">
                        <el-checkbox v-model="upload2.updateSupport" /> 是否更新已经存在的学生数据
                    </div>
                    <span>仅允许导入xls、xlsx格式文件。</span>
                    <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;"
                        @click="importStatusTemplate">下载模板</el-link>
                </div>
            </el-upload>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitFileForm2">确 定</el-button>
                <el-button @click="upload2.open = false">取 消</el-button>
            </div>
        </el-dialog>
        <el-dialog :visible.sync="picV" fullscreen>
            <StuPic v-if="picV" :stuNo="pic_stu_no" />
        </el-dialog>
        <!---查看详情-->
        <el-dialog :title="查看详情" :visible.sync="openDetail" width="900px" append-to-body>
            <el-form ref="form" :model="form" label-width="80px">
                <el-form-item label="姓名" prop="name">
                    <el-input v-model="form.name" :readonly="true" />
                </el-form-item>
                <el-form-item label="学号" prop="stuNo">
                    <el-input v-model="form.stuNo" :readonly="true" />
                </el-form-item>
                <el-form-item label="性别" prop="gender">
                    <el-input v-model="form.gender" :readonly="true" />
                </el-form-item>
                <el-form-item label="民族" prop="mz">
                    <el-input v-model="form.mz" :readonly="true" />
                </el-form-item>
                <el-form-item label="生日" prop="birthday">
                    <el-input v-model="form.birthday" :readonly="true" />
                </el-form-item>
                <el-form-item label="政治面貌" prop="zzmm">
                    <el-input v-model="form.zzmm" :readonly="true" />
                </el-form-item>
                <el-form-item label="身份证号码" prop="idCard">
                    <el-input v-model="form.idCard" :readonly="true" />
                </el-form-item>
                <el-form-item label="籍贯" prop="jg">
                    <el-input v-model="form.jg" :readonly="true" />
                </el-form-item>
                <el-form-item label="血型" prop="xx">
                    <el-input v-model="form.xx" :readonly="true" />
                </el-form-item>
                <el-form-item label="学院" prop="deptName">
                    <el-input v-model="form.deptName" :readonly="true" />
                </el-form-item>
                <el-form-item label="专业" prop="majorName">
                    <el-input v-model="form.majorName" :readonly="true" />
                </el-form-item>
                <el-form-item label="所属班级" prop="className">
                    <el-input v-model="form.className" :readonly="true" />
                </el-form-item>
                <el-form-item label="手机号码" prop="phone">
                    <el-input v-model="form.phone" :readonly="true" />
                </el-form-item>
                <el-form-item label="家庭地址" prop="address">
                    <el-input v-model="form.address" :readonly="true" />
                </el-form-item>
            </el-form>
        </el-dialog>
    </div>
</template>

<script>
import { addStudent, delStudent, getClassName, getStudent, initOnePwd, initPwd, listOwnStu as listStudent, updateStudent } from "@/api/stuCQS/basedata/student";
// import {  } from "@/api/stuCQS/basedata/class";
import { getTokenKeySessionStorage } from "@/utils/auth";

import StuPic from "@/views/basedata/private/StuPic";

export default {
    name: "own-Student",
    dicts: ['srs_stu_status'],

    components: {
        StuPic
    },
    data() {
        return {
            pic_stu_no: null,
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 学生信息表格数据
            studentList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            openDetail: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                name: null,
                stuNo: null,
                gender: null,
                birthday: null,
                idCard: null,
                gradeId: null,
                deptId: null,
                majorId: null,
                classId: null,
                phone: null,
                address: null,
                status: null,
            },
            // 学生导入参数
            upload: {
                // 是否显示弹出层（用户导入）
                open: false,
                // 弹出层标题（用户导入）
                title: "",
                // 是否禁用上传
                isUploading: false,
                // 是否更新已经存在的学生数据
                updateSupport: 0,
                // 设置上传的请求头部
                headers: { Authorization: "Bearer " + getTokenKeySessionStorage() },
                // 上传的地址
                url: process.env.VUE_APP_BASE_API + "/system/student/importData"
            },
            // 学生状态导入参数
            upload2: {
                // 是否显示弹出层（用户导入）
                open: false,
                // 弹出层标题（用户导入）
                title: "",
                // 是否禁用上传
                isUploading: false,
                // 是否更新已经存在的学生数据
                updateSupport: 0,
                // 设置上传的请求头部
                headers: { Authorization: "Bearer " + getTokenKeySessionStorage() },
                // 上传的地址
                url: process.env.VUE_APP_BASE_API + "/system/student/imporStatustData"
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                name: [
                    { required: true, message: "名字不能为空", trigger: "change" }
                ],
                stuNo: [
                    { required: true, message: "学号不能为空", trigger: "change" }
                ],
                gender: [
                    { required: true, message: "性别不能为空", trigger: "change" }
                ],
                idCard: [
                    { required: true, message: "身份证不能为空", trigger: "change" }
                ],
                majorId: [
                    { required: true, message: "所属班级不能为空", trigger: "change" }
                ],
                phone: [
                    { required: true, message: "手机号不能为空", trigger: "change" }
                ],
                address: [
                    { required: true, message: "手机号不能为空", trigger: "change" }
                ],
                delFlag: [
                    { required: true, message: "删除标志不能为空", trigger: "blur" }
                ]
            },

            ClassNameList: [],//班级名称
            classVlue1: [],//班级搜索选择
            classVlue2: [],//班级添加修改选择

            picV: false,
            token: getTokenKeySessionStorage()
        };
    },
    created() {
        this.getList();
        this.getClassNameList();
    },
    methods: {
        lookDetail(row) {
            this.reset();
            const stuId = row.stuId || this.ids
            getStudent(stuId).then(response => {
                this.form = response.data;
                this.form.className = response.data.srsClass.className;
                this.form.deptName = response.data.dept.deptName;
                this.form.majorName = response.data.srsMajors.majorName;
                this.form.mz = response.data.cphStuExtraInfo.mz;
                this.form.zzmm = response.data.cphStuExtraInfo.zzmm;
                this.form.jg = response.data.cphStuExtraInfo.jg;
                this.form.xx = response.data.cphStuExtraInfo.xx;
                this.openDetail = true;
            });
        },
        picVClick(row) {
            this.pic_stu_no = row.stuNo;
            this.picV = true;
        },
        async doInitPwdOne(val) {
            let sdata = {
                username: val.stuNo
            }
            let res = await initOnePwd(sdata);
            if (res.code == 200) {
                this.$message.success(res.msg);
            }
        },
        doInitPwd() {
            this.$message.info("正在初始化中，请稍候");
            initPwd().then(res => {
                this.$message.info(res.msg);
            });

        },
        /** 查询学生信息列表 */
        getList() {
            this.loading = true;
            listStudent(this.queryParams).then(response => {
                this.studentList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        getClassNameList() {
            getClassName().then(res => {
                this.ClassNameList = res.data
            })
        },
        //搜索班级选择
        handleChange1(value) {
            this.queryParams.classId = value[2]
            console.log(value);
        },
        //班级选择添加修改
        handleChange2(value) {
            this.form.classId = value[2]
            console.log(value);
        },

        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                stuId: null,
                name: null,
                stuNo: null,
                gender: null,
                birthday: null,
                idCard: null,
                deptId: null,
                majorId: null,
                classId: null,
                phone: null,
                address: null,
                status: null,
                createBy: null,
                createTime: null,
                updateBy: null,
                updateTime: null,
                delFlag: null
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.classVlue1 = [],
                this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.stuId)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.classVlue2 = [],
                this.open = true;
            this.title = "添加学生信息";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const stuId = row.stuId || this.ids
            getStudent(stuId).then(response => {
                this.form = response.data;
                var data = response.data;
                this.classVlue2 = [data.dept.deptId, data.srsMajors.majorId, data.srsClass.classId]
                console.log(this.classVlue2);
                this.open = true;
                this.title = "修改学生信息";
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.stuId != null) {
                        updateStudent(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addStudent(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const stuIds = row.stuId || this.ids;
            this.$modal.confirm('是否确认删除学生信息编号为"' + stuIds + '"的数据项？').then(function () {
                return delStudent(stuIds);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => { });
        },
        /** 导入按钮操作 */
        handleImport() {
            this.upload.title = "学生导入";
            this.upload.open = true;
        },
        handleImportStatus() {
            this.upload2.title = "学生状态导入";
            this.upload2.open = true;
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('system/student/export', {
                ...this.queryParams
            }, `student_${new Date().getTime()}.xlsx`)
        },
        /** 下载模板操作 */
        importTemplate() {
            this.download('system/student/importTemplate', {
            }, `student_info_template_${new Date().getTime()}.xlsx`)
        },
        /** 下载模板操作 */
        importStatusTemplate() {
            debugger;
            this.download('system/student/importTemplate2', {
            }, `studentstatus_info_template_${new Date().getTime()}.xlsx`)
        },
        // 文件上传中处理
        handleFileUploadProgress(event, file, fileList) {
            this.upload.isUploading = true;
            this.upload2.isUploading = true;
        },
        // 文件上传成功处理
        handleFileSuccess(response, file, fileList) {
            this.upload.open = false;
            this.upload.isUploading = false;
            this.$refs.upload.clearFiles();
            this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
            this.getList();
        },
        // 提交上传文件
        submitFileForm() {
            // console.log(this.token);
            this.$refs.upload.submit();
        },
        // 提交上传文件
        submitFileForm2() {
            this.$refs.upload2.submit();
        },
        // 文件上传成功处理
        handleFileSuccess2(response, file, fileList) {
            debugger;
            this.upload2.open = false;
            this.upload2.isUploading = false;
            this.$refs.upload2.clearFiles();
            this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
            this.getList();
        },
    }
};
</script>
<style scoped>
.stu_pic {
    margin: 0px;
    background: url("~@/assets/stu_pic/stu.png") no-repeat;
    background-size: 100% 100%;
    width: 100%;
    height: 100vh;
}
</style>
