<template>
  <div class="teacher">
    <div class="header">
      <el-row>
        <el-col :span="6">
          <label>老师：</label>{{detail.type | teacherType}} {{detail.name}}
        </el-col>
        <el-col :span="6">
          <label>微信号：</label> {{detail.alias}}
        </el-col>
        <el-col :span="6">
          <label>二维码：</label>
          <a :href="detail.qr">
            <img class="avatar" :src="detail.qr"/>
          </a>
        </el-col>
        <el-col :span="6">
          <label>老师状态：</label>{{detail.status === 0 ? '正常' : '禁用'}}
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="6">
          <label>带班总人数：</label>{{detail.total_join_num}}
        </el-col>
        <el-col :span="6" v-if="detail.user_info">
          <label>绑定用户：</label>
          <a :href="detail.user_info.avatar" target="_blank">
            <img class="avatar" :src="detail.user_info.avatar"/>
          </a>
          {{detail.user_info.nickname}}(ID:{{detail.user_info.user_id}})
        </el-col>
      </el-row>
    </div>
    <el-table
      @expand-change="changeRow"
      :data="list"
      style="width: 100%">
      <el-table-column type="expand">
        <template slot-scope="scope">
          <el-table
            :data="[scope.row]"
            style="width: 100%">
            <el-table-column
              label="到课率">
              <template slot-scope="scope2">
                <span>{{ scope2.row.arrive_course_rate  | percent}}</span>
              </template>
            </el-table-column>
            <el-table-column
              label="看课率">
              <template slot-scope="scope2">
                <span>{{ scope2.row.watch_course_rate  | percent}}</span>
              </template>
            </el-table-column>
            <el-table-column
              label="完课率">
              <template slot-scope="scope2">
                <span>{{ scope2.row.over_course_rate  | percent}}</span>
              </template>
            </el-table-column>
            <el-table-column
              label="作业率">
              <template slot-scope="scope2">
                <span>{{ scope2.row.work_rate  | percent}}</span>
              </template>
            </el-table-column>
            <el-table-column
              label="全勤作业率">
              <template slot-scope="scope2">
                <span>{{ scope2.row.over_work_rate  | percent}}</span>
              </template>
            </el-table-column>
            <el-table-column
              label="打卡率">
              <template slot-scope="scope2">
                <span>{{ scope2.row.clock_rate  | percent}}</span>
              </template>
            </el-table-column>
            <el-table-column
              label="全勤打卡率">
              <template slot-scope="scope2">
                <span>{{ scope2.row.over_clock_rate  | percent}}</span>
              </template>
            </el-table-column>
            <el-table-column
              label="转化率">
              <template slot-scope="scope2">
                <span>{{ scope2.row.transform_rate  | percent}}</span>
              </template>
            </el-table-column>
          </el-table>
        </template>
      </el-table-column>
      <el-table-column
        prop="periods_title"
        label="期数名称">
      </el-table-column>
      <el-table-column
        prop="join_num"
        label="参加人数">
      </el-table-column>
      <el-table-column
        prop="max_join_num"
        label="最大班级人数">
      </el-table-column>
      <el-table-column
        prop="start_at"
        label="开始时间">
      </el-table-column>
      <el-table-column
        prop="created_at"
        label="创建时间">
      </el-table-column>
      <el-table-column
        label="操作">
        <template slot-scope="scope">
          <el-button @click="showUser(scope.row)" size="mini" plain type="primary">
            班级成员
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <page :total="total" :limit="limit" @pageChange="onPageChange" @sizeChange="onSizeChange"/>
    <user-list :userObj="userObj" @reflash="getTeacherDetail"/>
  </div>
</template>

<script>
import {getTeacherDetailApi,getClassStatisticsApi} from "../../service/api";
import {TEACHERTYPE} from "../../util/wordbook";
import page from '../framework/page'
import UserList from '../class/userList'
  export default {
    name: "index",
    components:{
      page,
      UserList
    },
    data(){
      return {
        list:[],
        id: '',
        detail: {},
        total: 0,
        limit: 10,
        nowPage: 1,
        userObj:{
          classId:'',
          title:'',
          show:false,
        }
      }
    },
    methods:{
      changeRow(data,b){
        if(b.indexOf(data)>-1){
          getClassStatisticsApi(data.periods_id,data.id).then(res=>{
            data.arrive_course_rate = res.arrive_course_rate;
            data.watch_course_rate = res.watch_course_rate;
            data.over_course_rate = res.over_course_rate;
            data.work_rate = res.work_rate;
            data.over_work_rate = res.over_work_rate;
            data.clock_rate = res.clock_rate;
            data.over_clock_rate = res.over_clock_rate;
            data.transform_rate = res.transform_rate;
          })
        }
      },
      showUser(data){
        this.userObj={
          classId:data.id,
          show:true,
          title:`${this.detail.name}班级用户列表`
        }
      },
      onPageChange(val){
        this.nowPage = val
        this.getTeacherDetail()
      },
      onSizeChange(val){
        this.nowPage = 1
        this.limit = val;
        this.getTeacherDetail()
      },
      getTeacherDetail(){
        let id = this.id;
        let json = {
          limit: this.limit,
          page: this.nowPage
        }
        getTeacherDetailApi(id, json).then((res)=>{
          if (res.class_list) {
            res.class_list.list.forEach(data=>{
              data.arrive_course_rate = 0;
              data.watch_course_rate = 0;
              data.over_course_rate = 0;
              data.work_rate = 0;
              data.over_work_rate = 0;
              data.clock_rate = 0;
              data.over_clock_rate = 0;
              data.transform_rate = 0;
            });
            this.list = res.class_list.list || [];
            this.total = res.class_list.total;
          }
          this.detail = res;
        })
      }
    },
    filters:{
      teacherType(value){
        return TEACHERTYPE[value]
      },
      percent(val){
        return (val * 100).toFixed(2)+'%'
      }
    },

    mounted(){
      this.id = this.$route.params.id;
      this.getTeacherDetail();
    }
  }
</script>

<style scoped lang="less">
  .teacher {
    padding: 20px 0;
  }
  .avatar {
    width: 80px;
    margin-right: 5px;
  }
  .header {
    padding: 0 20px;
    margin-bottom: 20px;
  }
  .el-row {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    color: #666;
    font-size: 16px;
    label{
      margin-right: 10px;
    }
  }
  .el-col{
    /*height: 50px;*/
    display: flex;
    justify-content: flex-start;
    align-items: center;
    img{
      width: 50px;
      border-radius: 100px;
    }
    label{
      color: #5982e6;
    }
  }
</style>
<style>
  .demo-table-expand {
    font-size: 0;
  }
  .demo-table-expand label {
    width: 90px;
    color: #99a9bf;
  }
  .demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 50%;
  }
</style>
