<template>
	<view class="examList-box">
		<view style="display: flex;">
			<view class="examList-import" @click="goUrlWebview">
			    <van-icon name="plus" />
			    <text>导入考试信息</text>
			</view>
			<view class="examList-import" @click="setCensus">
			    <text>生成分析统计表</text>
			</view>
		</view>
        
		<block v-if="dataList && dataList.length>0">
		<view style="border-radius: 50rpx;overflow: hidden;box-shadow: 0px 6px 29px 0px rgba(76, 76, 76, 0.09);margin-bottom: 40rpx;" 
        class="examList activeOne" :class="{'fenxi':item.Type==3}" v-for="(item,index) in dataList" :key="index">
            <van-swipe-cell
			  id="swipe-cell"
			  :right-width="116"
			  async-close
			  :key="index"
			  @close="onClose">
			  
            <view class="examList-center-box">
                <view class="examList-center-line"></view>
                <view class="examList-center flex" @click="examDetails(item)">
                    <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" @click="examDetails(item)">
                   <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 slot="right" class="van-swipe-cell__right">
                    <van-button square type="danger" @click.stop="DelExamInfo(item)" custom-style="height:100%;">删除</van-button>
                </view>
            </view>
            
            </van-swipe-cell>
        </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";
	import { setExamStatAnalysis } from '@/api/index'
	export default {
		props: {
			dataList:[],
			CourseId :0
		},
        emits:['change'],
		components: {},
		setup(props,ctx) {
            let {
				proxy
			} = getCurrentInstance();
			uni.setNavigationBarTitle({
				title:'上传考试成绩'
			})
			
			let data = reactive({
				userData:{},//用户信息
				showPhone: false,
				platform:'',
				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,

			});
			wx.getSystemInfo({
			  success: (res) => {
				// window | mac为pc端
				// android | ios为手机端
				data.platform=res.platform;
			  }
			});
			watch(
			  () => props.dataList,
			  (newValue, oldValue) => {
				data.dataList=newValue
			  }
			)
            let methods = {
                onClose(event) {
				    const { position, instance } = event.detail;
                    console.log(position,'55555'+instance)
				    switch (position) {
				      case 'left':
				      case 'cell':
					  case 'right':
				        instance.close();
				        break;
				    }
				},
				setCensus(){
                    uni.showModal({
                        title: '提示',
                        content: '此操作将生成统计表的1-8题统计分析,是否生成',
                        cancelText:'不生成',
                        confirmText:'生成',
                        success: function(res) {
                            if (res.confirm) {
                                //1、调用后台生成接口
                                uni.showLoading({
                                    title:'正在生成中...'
                                })
                                setExamStatAnalysis({
                                    'CourseId':props.CourseId,
                                    'AnalysisType':1
                                }).then(r=>{
                                    setTimeout(()=>{
                                        uni.hideLoading()
                                    },3000)
                                    ctx.emit('change')
                                }) 
                            } else if (res.cancel) {
                                setExamStatAnalysis({
                                    'CourseId':props.CourseId,
                                    'AnalysisType':2
                                }).then(r=>{
                                    setTimeout(()=>{
                                        uni.hideLoading()
                                    },3000)
                                    ctx.emit('change')
                                }) 
                            }
                        }
                    });
					
					//2、重新调用列表接口请求
				},
                // 跳web上传
                goUrlWebview(){
					if(data.platform=='windows'||data.platform=='mac'){
                        data.userData = uni.getStorageSync('userInfo')
                        let dataContent = {
                            Uid:data.userData.Id,
                            CourseId:data.uploadParm.CourseId,
                        }
                        uni.navigateTo({
                            url: `/pages/webview/upload?exam=${encodeURIComponent(JSON.stringify(dataContent))}`
                        });
					}else{
						methods.ImportInformation()
					}
                    
                },
                examDetails(item){
                    uni.navigateTo({
                        url: `/pages/index/examDetails?exam=${encodeURIComponent(JSON.stringify(item))}`
					});
                },
                closepopup(){
                    data.show = false
                    data.uploadParm = {
                        Uid:'',
                        CourseId:'',
                        ExamName: ''
                    }
                },
                // 重新导入
                clickReimport(item){
                    this.goUrlWebview()
                },
                // 删除接口
                DelExamInfo(item){
                    uni.showModal({
                        title: '提示',
                        content: '将要清除此考试数据,是否继续',
                        success: function (res) {
                            if (res.confirm) {
                                let Msg = {
                                    ExamId: item.ExamId
                                }
                                proxy.$request("/Exam/DelExamInfo", Msg).then(res => {
                                    if(res.Code ==1){
                                        uni.showToast({
                                            title:'删除成功',
                                            icon:'none',
                                            duration: 500
                                        })
                                        
                                        proxy.$parent.getExamPageList();
                                    }
                                })
                            } else if (res.cancel) {
                                uni.showToast({
                                    title:'已取消',
                                    icon:'none',
                                    duration: 500
                                })
                            }
                        }
                    });
                    
                },
                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'//'http://192.168.10.11:8082/api'
                    } else {
                        host = 'https://jjswapi.oytour.com/api'
                    }
                    data.userData = uni.getStorageSync('userInfo')
                    data.uploadParm = {
                        Uid:data.userData.Id,
                        CourseId:props.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>
    .van-swipe-cell__left,
	.van-swipe-cell__right {
		position: absolute;
		top: 0;
		height: 100%;
		right: -111rpx;
		width:232px;
		-webkit-transform: translate3d(100%, 0, 0);
		transform: translate3d(100%, 0, 0);
	}
	.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;
    }
	.fenxi .examList-right{
		color:#282828;
		background: rgba(255,255,255,.6);
	}
    .examList.activeOne .examList-state-text{
        color: #BEBEBE;
    }
	.fenxi .examList-state-text{
	    color: #282828 !important;
	}
    .examList-state-text{
        font-size: 20rpx;
        letter-spacing: 2px;
    }
    .examList-state van-icon{
        color: #DADADA;
        font-size: 24rpx;
        margin-right: 10rpx;
    }
	.fenxi .examList-state van-icon{
        color: #282828 !important;
    }
    .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: #E64150;
        position: absolute;
        left: 0;
        top: 46rpx;
    }
	.fenxi .examList-center-line{
	    background: #282828 !important;
	}
    .examList.activeOne .examList-center-box{
        /* background: #FFFFFF; */
    }
	.examList{
	    background: #FFFFFF;
	}
	.examList.fenxi{
	    background: #F9E0E2;
	    box-shadow: 0px 6px 29px 0px rgba(76,76,76,0.09);
	}
    .examList-center-box{
        flex-grow: 1;
        position: relative;
        padding: 31rpx 40rpx 0 22rpx;
        /* margin-bottom: 50rpx; */
       
    }
	.examList{
        margin: 0 50rpx;
    }
    .examList-import text{
        margin-left: 15rpx;
    }
    .examList-import{
        width: 200rpx;
        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>
