<template>
  <div class="GeneralLawExam">
    <div class="search_box">
      <u-search placeholder="请输入需要搜索的考试" bg-color="#FFF" v-model="title" 
      :show-action="false" @search="getListInit" @clear="title='',getListInit()"></u-search>
    </div>

    <p class="all_test">全部考试</p>
    <div class="card_list" v-if="testList.length">
      <scroll-view :style="{height: height + 'px'}" scroll-y>
        <div class="card" v-for="item in testList" :key="item.id">
          <div class="card_top">
            <div class="card_title" :class="item.myRecord ? 'min-wid' : '' ">{{ item.examinationName }}</div>
            <div class="card_count">
              共<span class="col_blue">{{ item.allSubjectNumber }}</span>题
            </div>
            <div class="card_statistics">
              <span>{{ item.passNumber }}</span>人通过/<span>{{ item.examinationNumber }}</span>人参与
            </div>
            <div class="result" v-if="item.myRecord">
              <img class="result-img" v-show="item.myRecord.assessmentType == 0" src="https://cdn.cunwuyun.cn/qujing/xuefuwuche.png" alt="">
              <img class="result-img" v-show="item.myRecord.assessmentType == 1" src="https://cdn.cunwuyun.cn/qujing/caigaobadou.png" alt="">
              <img class="result-img" v-show="item.myRecord.assessmentType == 2" src="https://cdn.cunwuyun.cn/qujing/hege.png" alt="">
              <img class="result-img" v-show="item.myRecord.assessmentType == 3" src="https://cdn.cunwuyun.cn/qujing/buhege.png" alt="">
            </div>
            <div class="grade" :class="{'error':item.myRecord.assessmentType == 3}">{{ item.myRecord.score }}</div>
            <div class="tips" v-if="item.examinationType" :class="item.examinationType == 0? 'auth': ''">{{ item.examinationType==0? '学习测试':'考试认证' }}</div>
          </div>
          <div class="card_bottom" :class="item.myRecord?'col_gray':'col_blue'" @click="handleToTest(item)">{{ item.myRecord? '重新考试':'参加考试'}}</div>
        </div>
      </scroll-view>
    </div>
    <AiEmpty :description="`暂无数据`" class="emptyWrap" v-else/>
    
    <u-popup v-model="showAuth" mode="bottom" border-radius="32">
			<div class="auth_box">
        <div class="auth_title">居民信息认证</div>
        
        <div class="auth_form">
          <div class="form_name"><span class="red">*</span>姓名</div>
          <input class="input" type="text" placeholder="请输入姓名" v-model="form.name"/>
          <div class="form_name"><span class="red">*</span>身份证号</div>
          <input class="input" type="idcard" placeholder="请输入18位身份证号" v-model="form.idNumber"/>
          <div class="form_name"><span class="red">*</span>地区<span class="mini-text">（本地人员填户籍所在地，外地人员填常住地）</span></div>
          <AiAreaPicker class="ai-area" v-model="form.areaId" :areaId="$areaId" :fullName.sync="form.areaName">
            <div class="ai-area__wrapper">
              <div>
                <span v-if="form.areaName">{{ form.areaName }}</span>
                <i v-else style="color: #999">请选择</i>
              </div>
              <u-icon name="arrow-right" color="#ddd"/>
            </div>
          </AiAreaPicker>
        </div>

        <div class="auth_btn">
          <div class="cancel" @click="showAuth = false,form = {}">取消</div>
          <div class="submit" @click="comfirm">确定</div>
        </div>
      </div>
		</u-popup>
  </div>
</template>

<script>
import { mapState } from "vuex"
export default {
  props: {
    height: Number,
  },
  data() {
    return {
      showAuth: false,
      title: '',
      current: 1,
      form: {
        name: '',
        idNumber: '',
        areaId: '',
        areaName: '',
      },
      flag: false,
      current: 1,
      testList: [],
      testId: '',
      studyDuration: '',
      pages: 2
    }
  },
  computed: {
    ...mapState(['user'])
  },
  methods: {
    nextList() {
      this.current ++
      this.getList()
    },
    getListInit() {
      this.current = 1
      this.pages = 2
      this.getList()
    },
    getList() {
      console.log(this.current, this.pages)
      if(this.current > this.pages) return
      this.$instance.post(`/app/appexaminationinfo/listForXCX`, null, {
        params: {
          current: this.current,
          size: 10,
          title: this.title
        }
      }).then(res=> {
        if(res?.data) {
          // this.testList = res.data.records
          this.testList = this.current==1? res.data.records: [...this.testList,...res.data.records]
          this.pages = res.data.pages
          this.getStudyDuration()
        }
      })
    },
    getUserInfo() {
      this.$instance.post(`/app/appwechatuserqujing/check`).then(res=> {
        if(res?.data) {
          this.$store.commit('setUser', res.data)
        }
      })
    },
    handleToTest({id, examinationDuration: time, examinationType }) {
      if(examinationType == 1) {
        if(this.studyDuration < time) {
          return uni.showModal({
            title: '考试说明',
            content: `「考试认证」类考试，需要在线课堂中学习普法知识满足${time}分钟才能参加，考试通过并审核后可获取知法明理证书。`,
            confirmText: '去学习',
            confirmColor: '#2D7DFF',
            success: (res)=> {
              if (res.confirm) {
                uni.$emit('update', { inx: 0 })
              } else if (res.cancel) {
                // 停留
              }
            }
          });
        }
      }
      if(!this.user.areaId) {
        this.showAuth = true;
        this.testId = id;
      } else { 
        this.$emit('toTest',id)
      }
    },
    // 查看累计时长
    getStudyDuration() {
      this.$instance.post(`/app/appwechatuserqujing/queryStudyDuration`).then(res => {
        if (res?.data) {
          this.studyDuration = res.data.studyDuration
        }
      })
    },
    comfirm() {
      if(this.flag) return

      if(!this.form.name) {
        return this.$u.toast('请输入姓名')
      }

      if(!this.form.idNumber) {
        return this.$u.toast('请输入身份证号')
      }
      if (!this.$idCardNoUtil.checkIdCardNo(this.form.idNumber)) {
        return this.$u.toast('请输入正确的身份证号码')
      }

      if(!this.form.areaId) {
        return this.$u.toast('请选择地区')
      }

      this.flag = true
      this.$instance.post(`/app/appwechatuserqujing/idNumberAttestation`, this.form).then(res=> {
        if(res?.code == 0) {
          this.flag = false
          this.showAuth = false
          this.$u.toast("认证成功");
          setTimeout(()=> {
            this.$emit('toTest',this.testId)
          }, 500)
        } else {
          this.flag = false
          this.showAuth = false
          this.$u.toast('认证失败')
        }
      }).catch((err)=> {
        this.flag = false
        this.showAuth = false
        this.$u.toast(err.msg)
      })
    },
  },
}
</script>

<style lang="scss" scoped>
.GeneralLawExam {
  .search_box {
    margin: 24px 0;
    padding: 0 32px;
    box-sizing: border-box;
  }
  
  .all_test {
    font-size: 28px;
    color: #333333;
    font-weight: 600;
    padding: 0 32px;
    box-sizing: border-box;
  }

  .card_list {
    padding: 8px 32px;
    box-sizing: border-box;

    .card {
      margin-top: 24px;
      .card_top,
      .card_bottom {
        padding: 24px;
        box-sizing: border-box;
        background: #FCFCFC;
      }
      
      .card_top {
        position: relative;
        border-radius: 16px 16px 0 0;
        padding: 38px;
        box-sizing: border-box;
        

        .card_title {
          font-weight: 500;
          font-size: 36px;
          color: #333333;
          overflow: hidden; 
          text-overflow:ellipsis;
          display: -webkit-box;
          -webkit-box-orient: vertical;
          -webkit-line-clamp: 2;
        }

        .min-wid {
          width: 438px;
        }

        .card_count,
        .card_statistics {
          font-weight: 400;
          font-size: 26px;
          color: #999999;
          margin-top: 16px;
        }

        .result {
          position: absolute;
          right: 0;
          top: 33px;
          .result-img {
            width: 200px;
            height: 200px;
          }
        }

        .grade {
          position: absolute;
          right: 75px;
          top: 92px;
          font-weight: 500;
          font-size: 40px;
          color: #0FC484;
        }

        .error {
          color: #E23C3C;
        }

        .tips {
          position: absolute;
          top: 0;
          right: 0;
          width: 116px;
          height: 38px;
          line-height: 38px;
          background: #FF772D;
          border-radius: 0 16px 0 16px;
          color: #FFF;
          font-size: 24px;
          text-align: center;
        }

        .auth {
          background: #8F5AFF;
        }

      }

      .card_bottom {
        border-top: 1px solid #EEEEEE;
        border-radius: 0 0 16px 16px;
        text-align: center; 
      }
      .col_blue {
        color: #2D7DFF;
      }
      .col_gray {
        color: #687DA6;
      }
    }
  }

  .auth_box {
    padding: 32px;
    box-sizing: border-box;

    .auth_title {
      font-weight: 600;
      font-size: 34px;
      color: #333333;
      text-align: center;
    }
    .auth_form {
      margin-top: 30px;

      .form_name {
        font-weight: 500;
        font-size: 28px;
        color: #333333;
        margin-bottom: 16px;

        .red {
          color: #FF4466;
        }
        .mini-text {
          color: #999;
          font-size: 24px;
        }
      }

      .input {
        height: 96px;
        background: #F4F5FA;
        border-radius: 16px;
        margin-bottom: 32px;
        padding-left: 24px;
      }
   
      .ai-area  .ai-area__wrapper {
        display: flex;
        justify-content: space-between;
        align-items: center;
        background: #F4F5FA;
        border-radius: 16px;
        margin-bottom: 32px;
        padding: 24px;
        box-sizing: border-box;
      }
    }

    .auth_btn {
      display: flex;
      justify-content: space-between;
      height: 100px;
      padding: 10px 0;
      box-sizing: border-box;
      .cancel {
        background: #F2F2F2;
        color: #2D7DFF;
      }
      .submit {
        background: #2D7DFF;
        color: #FFF;
      }
      .cancel,
      .submit {
        width: 48%;
        height: 80px;
        line-height: 80px;
        text-align: center;
        border-radius: 16px;
      }
    }
  }
}
</style>
