<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-col v-if="detail.user_info" :span="6">
          <el-button @click="createInviteLink(detail.user_info.user_id)" size="mini" plain type="primary">
            复制专属链接
          </el-button>
        </el-col>
      </el-row>
    </div>
    <div style="padding: 15px">
      <el-button type="success" @click="onAddUser(true)" v-if="!$store.state.readonly">老师绑定用户</el-button>
    </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"/>
    <choose-good-dialog v-if="chooseGoodDialogObj.show" :dialogObj="chooseGoodDialogObj" @changeShow="changeShow"/>
    <el-dialog :modal="false" :visible.sync="addShow">
      <el-form label-width="90px">
        <!--<el-form-item label="用户id">-->
        <!--<el-input v-model="addId"></el-input>-->
        <!--</el-form-item>-->
        <el-row>
          <el-col :span="8">
            <el-form-item label="ID">
              <el-input v-model="searchFrom.userId"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="昵称">
              <el-input v-model="searchFrom.nickName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="电话">
              <el-input v-model="searchFrom.mobile"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8" :offset="16">
            <el-form-item>
              <el-button style="float: right" type="primary" plain @click="getUser">搜索</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-table
        :data="userList"
        ref="multipleTable"
        @selection-change="handleSelectionChange"
        style="width: 100%">
        <el-table-column
          type="selection"
          width="55">
        </el-table-column>
        <el-table-column
          className="f-c"
          label="用户">
          <template slot-scope="scope">
            <img style="margin-right:5px;width: 50px;height: 50px;border-radius: 50px" :src="scope.row.avatar">{{scope.row.nickname}}(ID:{{scope.row.user_id}})
          </template>
        </el-table-column>
        <el-table-column
          prop="mobile"
          label="手机号">
        </el-table-column>
      </el-table>
      <page :total="total" :limit="limit" @pageChange="onPageChange" @sizeChange="onSizeChange"/>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addShow = false">取 消</el-button>
        <el-button type="primary" @click="onAdd">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {getTeacherDetailApi,getClassStatisticsApi,getUserListApi,teacherBindUserApi} from "../../service/api";
import {TEACHERTYPE} from "../../util/wordbook";
import page from '../framework/page'
import UserList from '../class/userList'
import chooseGoodDialog from './chooseGoodDialog'
  export default {
    name: "index",
    components:{
      page,
      UserList,
      chooseGoodDialog
    },
    data(){
      return {
        searchFrom: {
          user_id:'',
          is_add_teacher: '',
          is_view_course: '',
          start_at:'',
          end_at:''
        },
        userList:[],
        list:[],
        id: '',
        isBindUser:true,
        addShow:false,
        detail: {},
        total: 0,
        limit: 10,
        nowPage: 1,
        userObj:{
          classId:'',
          title:'',
          show:false,
        },
        addId:'',
        multipleSelection:[],
        chooseGoodDialogObj: {
          show: false,
          code: ''
        }
      }
    },
    methods:{
      handleSelectionChange(val) {
        this.multipleSelection = val;
      },
      onAdd(){
        let json = {
          is_buy: 0
        }
        if(this.multipleSelection.length === 0) {
          this.$message({
            type: 'error',
            message: '请选择用户!'
          });
          return
        } else if (this.multipleSelection.length !== 1){
          this.$message({
            type: 'error',
            message: '只能选择一个用户!'
          });
          return
        }
        this.addId = this.multipleSelection[0].user_id;
        if (!this.addId) {
          return
        }
        if (!this.isBindUser) {

        } else {
          teacherBindUserApi(this.id, {user_id: this.addId}).then(res=>{
            this.$message({
              type: 'success',
              message: '绑定成功!'
            });
            this.addShow = false;
          })
        }

      },
      onAddUser(flag){
        this.isBindUser = flag;
        this.addShow = true;
        this.getUser();
      },
      getUser(){
        let json = {
          page: this.nowPage,
          limit:  this.limit,
        };
        if (this.searchFrom.userId) {
          json.user_id = this.searchFrom.userId
        }
        if (this.searchFrom.nickName) {
          json.nickname = this.searchFrom.nickName
        }
        if (this.searchFrom.mobile) {
          json.mobile = this.searchFrom.mobile
        }
        getUserListApi(json).then(res=>{
          this.userList = res.list;
          this.total = res.total;
        })
      },
      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}班级用户列表`,
          teacherId: data.teacher_id,
          periods_id: data.periods_id
        }
      },
      onPageChange(val){
        this.nowPage = val;
        this.getTeacherDetail();
        this.getUser()
      },
      onSizeChange(val){
        this.limit = val;
        this.nowPage = 1;
        this.getTeacherDetail();
         this.getUser();
      },
      getTeacherDetail(){
        this.searchFrom = {
          user_id:'',
          is_add_teacher: '',
          is_view_course: '',
          start_at:'',
          end_at:''
        };
        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;
        })
      },
      createInviteLink(id){
        this.chooseGoodDialogObj.show = true;
        this.chooseGoodDialogObj.code = `CC-TEACHER-${id}`;
      },
      changeShow(){
        this.chooseGoodDialogObj.show = false;
        this.chooseGoodDialogObj.code = '';
      }
    },
    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;
    height: 50px;
  }
  .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>
