<template>
	<view class="examList-box">
        <view class="examList-import" @click="ImportInformation">
            <van-icon name="plus" />
            <text>导入考试信息</text>
        </view>
		<block v-if="dataList && dataList.length>0">
		<view class="examList activeOne" v-for="(item,index) in dataList" :key="index">
            <view class="examList-center-box" @click="examDetails(item)">
                <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.toFixed(1)}}
                            </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>
        </block>
        <view class="noData" v-else>
        	<image src="https://viitto-1301420277.cos.ap-chengdu.myqcloud.com/Test/Upload/Goods/1653902791000_806.png" mode="widthFix"></image>
        	<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>
				<image src="https://viitto-1301420277.cos.ap-chengdu.myqcloud.com/Test/Upload/Goods/1654136886000_860.png" mode="widthFix" class="examList-popup-icon"></image>
                <view class="examList-popup-content">
                    <input v-model="uploadParm.ExamName" class="examList-popup-name" placeholder="请输入考试名称"/>
					
                    <van-uploader
                    class="examList-popup-fil-box flex"
                    accept="file"
                    preview-size="120rpx"
                    :before-read="beforeRead"
                    @after-read="customUpload"
                    
                    >
                    <!-- <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-button round color='#C91727' icon="add-o" custom-style='width: 360rpx;margin:0 auto;' block>选择文件</van-button>
                    </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:[],
			CourseId :0
		},
        emits:['change'],
		components: {},
		setup(props,ctx) {
            let {
				proxy
			} = getCurrentInstance();
			let data = reactive({
				userData:{},//用户信息
				showPhone: false,
				showLogin: false, //多次点击
                show:false,
                uploadParm: {
                    // Uid:726615,
                    // CourseId:44977497,
                    Uid:0,
                    CourseId:props.CourseId,
                    ExamName:''
                },
                fileList:[],
                Msg:{
                    PageIndex: 1,
                    PageSize: 10,
                    ExamName:''
                },
                dataList: props.dataList,
                loading: false,

			});
			watch(
			  () => props.dataList,
			  (newValue, oldValue) => {
				data.dataList=newValue
			  }
			)
            let methods = {
                examDetails(item){
                    uni.navigateTo({
                        url: `/pages/index/examDetails?exam=${encodeURIComponent(JSON.stringify(item))}`
					});
                },
                closepopup(){
                    data.show = false
                    data.uploadParm = {
                        Uid:'',
                        CourseId:'',
                        ExamName: ''
                    }
                },
                // 重新导入
                clickReimport(item){
                    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 ;
							proxy.$parent.getExamPageList();
                        }
					})
                },
                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){
					console.log(file)
					uni.showLoading({
						title:'正在上传',
						mask:true
					})
                    if(!data.uploadParm.ExamName){
                        uni.showToast({
                            title:'请输入考试名称',
                            icon:'none',
	                        duration: 500
                        })
						uni.hideLoading()
                        return
                    }
                    if(file.url.indexOf('xls')==-1||file.url.indexOf('xlsx')==-1){
                        uni.showToast({
                            title:'请上传xls、xlsx格式的文件',
                            icon:'none',
	                        duration: 500
                        })
						uni.hideLoading()
                        return
                    }
                    let host = ''
                    if (process.env.NODE_ENV === "development") {
                         host = 'https://jjswapi.oytour.com/api'
                    } else {
                        host = 'https://jjswapi.oytour.com/api'
                    }
                    data.userData = uni.getStorageSync('userInfo')
                    data.uploadParm = {
                        Uid:data.userData.Id,
                        CourseId:data.uploadParm.CourseId,
                        ExamName: data.uploadParm.ExamName
                    }
                    data.loading = true
                    
                    uni.uploadFile({
                        url: host + '/Upload/UploadStuExamScore',
                        filePath: file.url,
                        name: 'file',
                        formData: {
							myfile:file,
							params:JSON.stringify(data.uploadParm)
						},
                        success(res) {
							uni.hideLoading()
							var result=JSON.parse(res.data)
                            if(result.Code ==1){
                                uni.showToast({
                                    title: '导入成功',
                                    icon:'none',
                                    duration: 500
                                })
								methods.closepopup()
                                //data.loading = false

                                ctx.emit('change')
                            }else{
                                uni.showToast({
                                    title: '导入失败',
                                    icon:'none',
                                    duration: 500
                                })
                                data.loading = false
                            }
                        },
                        fail(err) {
                            console.log("uploadErr", err)
							uni.hideLoading()
                        }
	                });
                    proxy.$parent.getExamPageList();
                }
            };
			let that = methods;
			return {
				...toRefs(data),
                ...methods
			};
		},
        onLoad(){
            
        },
        onShow() {
            
		}
	};
</script>
<style scoped>
	.examList-box .noData{
		text-align: center;
		flex:1;
		width: 100%;
	}
	.examList-box .noData image{
		width:200rpx;
		margin-bottom: 40rpx;
	}
	.examList-box .noData view{
		font-size: 24rpx;
		color: #cecece;
		text-align: center;
	}
    .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{
        width: 548rpx;
        text-align: center;
    }
    .examList-popup-fil-box{
        display: flex;
        line-height: 88rpx;
        width: 360rpx;
        /* padding: 0 197rpx; */
        border-radius: 44rpx;
        font-size: 30rpx;
        color: #FFFFFF;
        background: #C91727;
		margin:0 auto;
        margin-top: 27rpx;
        text-align: center;
    }
    .examList-popup-name{
        background-color: #FFFFFF;
        text-align: center;
        border: 1rpx solid #AAAAAA;
        height: 88rpx;
        line-height: 88rpx;
        padding: 0 10rpx;
        border-radius: 44rpx;
        font-size: 30rpx;
		width: 439rpx;
		font-weight: bold;
		margin:0 auto;
    }
    .examList-popup-title{
        width: 100%;
        text-align: center;
        font-size: 30rpx;
        font-weight: 500;
        margin-bottom: 70rpx;
    }
	.examList-popup-icon{
		width:188rpx;
		height: 188rpx;
		display: block;
		margin: 0 auto 50rpx auto;
	}
    .examList-popup-closure{
        position: absolute;
        right: 31rpx;
        top: 31rpx;
        color: #282828;
        font-size: 38rpx;
    }
    .examList-popup-box{
        position: relative;
        width: 548rpx;
        padding: 48rpx 55rpx 82rpx 55rpx;
		box-sizing: border-box;
    }
    .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;
        white-space: nowrap;
    }
    .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;
		zoom: .9;
		flex: 1;
		margin-left: 62rpx;
    }
    .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>
