<template>
	<view class="examList-box">
        <view class="examList-import" @click="ImportInformation">
            <van-icon name="plus" />
            <text>导入考试信息</text>
        </view>
		<view class="examList activeOne" v-for="(item,index) in dataList" :key="index">
            <view class="examList-center-box" @click="examDetails(item.ExamId)">
                <view class="examList-center-line"></view>
                <view class="examList-center flex">
                    <view class="examList-left">
                        <view class="examList-name">{{item.ExamName}}</view>
                        <view class="examList-state flex">
                            <van-icon name="clock-o" />
                            <view class="examList-state-text">{{item.CreateTime}}</view>
                        </view>
                    </view>
                    <view class="examList-right flex">
                        <view class="homework-score flex">
                            <view class="homework-score-title">人数</view>
                            <view class="homework-score-num flex">
                                {{item.StuNum}}
                            </view>
                        </view>
                        <view class="homework-score flex">
                            <view class="homework-score-title">平均得分</view>
                            <view class="homework-score-num flex">
                                {{item.AvgScore}}
                            </view>
                        </view>
                    </view>
                </view>
                <view class="examList-center-hint flex" v-if="item.FailNum>0">
                   <view class="examList-hint-left flex">
                        <van-icon name="warning" />
                        <view>
                            导入失败({{item.FailNum}}学员信息不匹配)可点击查看具体情况
                        </view>
                   </view>
                   <view class="examList-hint-right">
                       <view @click.stop="clickReimport(item)">重新导入</view>
                   </view>
                </view>
            </view>
            
        </view>
        <van-popup :show="show" :round="true" @close="closepopup">
            <view class="examList-popup-box">
                <van-icon class="examList-popup-closure" name="cross" @click="closepopup"/>
                <view class="examList-popup-title">导入考试信息</view>
                <view class="examList-popup-content">
                    <input v-model="uploadParm.ExamName" class="examList-popup-name" placeholder="请输入考试名称"/>
                    <van-uploader
                    class="examList-popup-fil-box flex"
                    multiple
                    accept=".excel"
                    preview-size="120rpx"
                    :before-read="beforeRead"
                    @after-read="customUpload"
                    :disabled="loading"
                    >
                    <view class="examList-popup-fil">
                       <van-image class="img" width="32rpx" height="32rpx" fit="cover"
				src="https://viitto-1301420277.cos.ap-chengdu.myqcloud.com/Test/Upload/Goods/1653639041000_305.png" />
                    选择文件 
                    </view>
                    </van-uploader>
                </view>
            </view>
        </van-popup>
	</view>
</template>
<script>
	import {
		reactive,
		toRefs,
		onMounted,
		getCurrentInstance,
		inject,
        watch
	} from "vue";
    import { uploadFile } from "@/utils/index";
	export default {
		props: {
			dataList:[],
		},
		components: {},
		setup(props) {
            let {
				proxy
			} = getCurrentInstance();
			let data = reactive({
				userData:{},//用户信息
				showPhone: false,
				showLogin: false, //多次点击
                show:false,
                uploadParm: {
                    // Uid:726615,
                    // CourseId:44977497,
                    Uid:0,
                    CourseId:44977497,
                    ExamName:''
                },
                fileList:[],
                Msg:{
                    PageIndex: 1,
                    PageSize: 10,
                    ExamName:''
                },
                dataList: props.dataList,
                loading: false,

			});
            let methods = {
                examDetails(item){
                    uni.reLaunch({
                        url: '/pages/index/examDetails?examId=' + item
					});
                },
                closepopup(){
                    data.show = false
                    data.uploadParm = {
                        Uid:'',
                        CourseId:'',
                        ExamName: ''
                    }
                },
                // 重新导入
                clickReimport(item){
                    // data.uploadParm.ExamName = item.ExamName
                    // data.uploadParm.Uid = data.userData.Id
                    // data.uploadParm.CourseId = data.Msg.CourseId
                    // data.uploadParm.ExamName = tem.ExamName
                    uni.showModal({
                        title: '提示',
                        content: '重新导入将要删除该数据,是否继续',
                        success: function (res) {
                            if (res.confirm) {
                                that.DelExamInfo(item)
                            } else if (res.cancel) {
                                uni.showToast({
                                    title:'已取消删除',
                                    icon:'none',
                                    duration: 500
                                })
                            }
                        }
                    });
                },
                // 删除接口
                DelExamInfo(item){
                    let Msg = {
                        ExamId: item.ExamId
                    }
                    proxy.$request("/Exam/DelExamInfo", Msg).then(res => {
						if(res.Code ==1){
                            uni.showToast({
                                title:'删除成功',
                                icon:'none',
                                duration: 500
                            })
                            data.show = true 
                        }
					})
                },
                ImportInformation(){
                    data.show = true
                },
                beforeRead(file){
                    if(!data.uploadParm.ExamName){
                        uni.showToast({
                            title:'请输入考试名称',
                            icon:'none',
	                        duration: 500
                        })
                        return
                    }
                    if (file.url.indexOf('xls') ==-1 ||file.url.indexOf('xlsx') ==-1) {
                        uni.showToast({
                            title:'请上传 xls/xlsx 格式图片',
                            icon:'none',
	                        duration: 500
                        })
                        return false;
                    }
                    return true;
                },
                // 添加导入
                customUpload(event){
                    data.userData = uni.getStorageSync('userInfo')
                    const { file } = event.detail;
                    that.uploadFileFun(file)
                    // uploadFile(params, file, (res) => {
                    //     uni.showModal({
                    //         title:res.Message
                    //     })
                    // });
                    // return
                    
                },
                uploadFileFun(file){
                    if(!data.uploadParm.ExamName){
                        uni.showToast({
                            title:'请输入考试名称',
                            icon:'none',
	                        duration: 500
                        })
                        return
                    }
                    if(file[0].url.indexOf('xls')==-1||file[0].url.indexOf('xlsx')==-1){
                        uni.showToast({
                            title:'请上传xls、xlsx格式的文件',
                            icon:'none',
	                        duration: 500
                        })
                        return
                    }
                    let host = ''
                    if (process.env.NODE_ENV === "development") {
                        host = 'http://192.168.10.36:8082/api'
                    } else {
                        host = 'https://eduapi.oytour.com/api'
                    }
                    data.userData = uni.getStorageSync('userInfo')
                    data.uploadParm = {
                        Uid:data.userData.Id,
                        CourseId:data.uploadParm.CourseId,
                        ExamName: data.uploadParm.ExamName
                    }
                    uni.uploadFile({
                        url: host + '/Upload/UploadStuExamScore',
                        filePath: file[0].url,
                        name: 'file',
                        formData: {
							myfile:file[0],
							params:JSON.stringify(data.uploadParm)
						},
                        success(res) {
                            data.loading = true
                            if(res.Code ==1){
                                
                                uni.showToast({
                                    title: '导入成功',
                                    icon:'none',
                                    duration: 500
                                })
                                uni.hideLoading();
                                data.uploadParm = {
                                    Uid:'',
                                    CourseId:'',
                                    ExamName: ''
                                }
                                data.loading = false
                            }else{
                                uni.showToast({
                                    title: '导入失败',
                                    icon:'none',
                                    duration: 500
                                })  
                                uni.hideLoading();
                                data.loading = false
                            }
                            this.$parent.getExamPageList();
                        },
                        fail(err) {
                            console.log("uploadErr", err)
                        }
	                });
                }
            };
			let that = methods;
			return {
				...toRefs(data),
                ...methods
			};
		},
        onLoad(){
            
        },
        onShow() {
            
		}
	};
</script>
<style scoped>
    .examList-hint-right view{
        padding: 16rpx 30rpx;
        background: #C91727;
        border-radius: 50rpx;
        color: #FFFFFF;
        font-size: 24rpx;
        font-weight: bold;
        letter-spacing: 1rpx;
    }
    .examList-hint-right{
        flex-shrink: 0;
        margin-left: 50rpx;
    }
    .examList-hint-left view{
        background: url('https://viitto-1301420277.cos.ap-chengdu.myqcloud.com/Test/Upload/Goods/1653648241000_545.png')no-repeat center;
        background-size: 100% 100%;
        padding: 11rpx 0 11rpx 30rpx;
    }
    .examList-hint-left van-icon{
        font-size: 42rpx;
        margin-right: 10rpx;
    }
    .examList-hint-left{
        font-size: 22rpx;
        color: #C91727;
        letter-spacing: 1rpx; 
        align-items: center;
    }
    .examList-center-hint{
        justify-content: space-between;
        padding: 10rpx 0;
        border-top: 1rpx solid #F6F6F6;
        align-items: center;
    }
    .img {
        display: inline-block;
        position: relative;
        top: 6rpx;
        margin-right: 10rpx;
    }
    .examList-popup-fil{
        
    }
    .examList-popup-fil-box{
        display: flex;
        line-height: 88rpx;
        padding: 0 197rpx;
        border-radius: 44rpx;
        font-size: 30rpx;
        color: #FFFFFF;
        background: #C91727;
        margin-top: 27rpx;
        text-align: center;
    }
    .examList-popup-name{
        background-color: #FFFFFF;
        text-align: center;
        border: 1px solid #AAAAAA;
        height: 86rpx;
        line-height: 86rpx;
        padding: 0 10rpx;
        border-radius: 44rpx;
        font-size: 30rpx;
    }
    .examList-popup-title{
        width: 100%;
        text-align: center;
        font-size: 30rpx;
        font-weight: 500;
        margin-bottom: 73rpx;
        letter-spacing: 1rpx;
    }
    .examList-popup-closure{
        position: absolute;
        right: 31rpx;
        top: 31rpx;
        color: #282828;
        font-size: 38rpx;
    }
    .examList-popup-box{
        position: relative;
        width: 548rpx;
        padding: 48rpx 55rpx 98rpx 55rpx;
    }
    .homework-score-num view:last-child{
        font-size: 23rpx;
        margin-top: 25rpx;
    }
    .homework-score-num view:first-child{
        margin-right: 10rpx;
        font-size: 46rpx;
        font-weight: 800;
    }
    .homework-score-num{
        font-weight: 800;
        font-style: italic;
        font-size: 36rpx;
    }
    .homework-score-title{
        font-size: 20rpx;
        margin-bottom: 0rpx;
        font-weight: 400;
        letter-spacing: 1px;
        margin-bottom: 15rpx;
    }
    .homework-score:last-child{
        margin-right: 0rpx;
    }
    .homework-score{
        margin-right: 16rpx;
        flex-direction: column;
        justify-content: center;
    }
    .examList-right{
        height: 122rpx;
        background: #FCEEEF;
        padding: 23rpx 37rpx 15rpx 37rpx;
        border-radius: 30rpx;
        justify-content: space-between;
        flex-shrink: 0;
        margin-left: 11rpx;
        color: #CE8086;
    }
    .examList.activeOne .examList-state-text{
        color: #BEBEBE;
    }
    .examList-state-text{
        font-size: 20rpx;
        letter-spacing: 2px;
    }
    .examList-state van-icon{
        color: #DADADA;
        font-size: 24rpx;
        margin-right: 10rpx;
    }
    .examList-state{
        margin-top: 35rpx;
        font-size: 20rpx;
    }
    .examList.activeOne .examList-name{
        color: #282828;
    }
    .examList-name{
        height: 70rpx;
        font-size: 26rpx;
        word-break: break-all;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 2;
        overflow: hidden;
        margin-top: 9rpx;
        letter-spacing: 1px;
        font-weight: bold;
    }
    .examList-left{
        flex-grow: 1;
    }
    .examList-center{
        justify-content: space-between;
        padding: 0 0 20rpx 0;
    }
    .examList-center-line{
        width: 4rpx;
        height: 23rpx;
        background: #282828;
        position: absolute;
        left: 0;
        top: 46rpx;
    }
    .examList.activeOne .examList-center-box{
        background: #FFFFFF; 
    }
    .examList-center-box{
        flex-grow: 1;
        position: relative;
        border-radius: 30rpx;
        padding: 31rpx 40rpx 0 22rpx;
        margin-bottom: 50rpx;
        box-shadow: 0px 6px 29px 0px rgba(76, 76, 76, 0.09);
        border-radius: 30px;
    }
	.examList{
        margin: 0 50rpx;
    }
    .examList-import text{
        margin-left: 15rpx;
    }
    .examList-import{
        width: 247rpx;
        border: 1rpx solid #F2A1A8;
        background: #FFFFFF;
        line-height: 54rpx;
        padding: 0 38rpx;
        margin: auto;
        margin-bottom: 48rpx;
        border-radius: 50rpx;
        color: #F2A1A8;
        font-size: 24rpx;
        text-align: center;
    }
    .examList-box{
        
    }
</style>
