<template>
  <div class="user">
    <div class="header">
      <div class="clear-both" style="margin-top: 20px" v-if="detail">
        <el-button size="mini" plain type="success" v-if="!$store.state.readonly" @click="bindTeacher" style="float: right">
          绑定老师
        </el-button>
      </div>
      <el-row>
        <el-col :span="6">
          <label>昵称：</label>
          <a :href="detail.qr">
            <img class="avatar" :src="detail.avatar"/>
          </a>
          {{detail.nickname}} (ID: {{id}})
        </el-col>
        <el-col :span="6">
          <label>手机号：</label> {{detail.mobile}}
        </el-col>
        <el-col :span="6">
          <label>生日：</label>{{detail.birthday}}
        </el-col>
        <el-col :span="6">
          <label>等级：</label>{{detail.level}}
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="6">
          <label>邀请类型：</label>{{detail.invite_type | inviteType}}
        </el-col>
        <el-col :span="6">
          <label>邀请人ID：</label>{{detail.invite_user_id ? detail.invite_user_id : ''}}
        </el-col>
        <el-col :span="6">
          <label>已绑定老师：</label>{{detail.teacher_name}}
        </el-col>
        <el-col :span="6">
          <label>注册时间：</label>{{detail.created_at}}
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="6">
          <label>最后登录：</label>{{detail.last_login_at}}
        </el-col>
      </el-row>
    </div>
    <div class="b-title">期数列表</div>
    <el-table
      @expand-change="changeRow"
    :data="periodList"
    style="width: 100%">
      <el-table-column type="expand">
        <template slot-scope="scope">
          <el-table
            :data="list"
            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="期数名称">
        <template slot-scope="scope">
          <router-link :to="{name:'class', query: { goods_id: scope.row.goods_id, periods_id: scope.row.periods_id}}" >
            {{scope.row.periods_title}}
          </router-link>
        </template>
      </el-table-column>
      <el-table-column
        label="看课权限">
        <template slot-scope="scope">
          {{scope.row.is_view_course === 1 ? '是' : '否'}}
        </template>
      </el-table-column>
      <el-table-column
        className="f-c"
        label="老师">
        <template slot-scope="scope">
          <router-link :to="`/teacher/${scope.row.teacher_id}`" >
            <div class="f-c">
              <div>
                <img class="avatar" :src="scope.row.teacher_qr"> {{scope.row.teacher_name}}
              </div>
            </div>
          </router-link>
        </template>
      </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>
    <div class="b-title">订单列表</div>
    <el-table
      :data="userList"
      @expand-change="changeRow2"
      style="width: 100%">
      <el-table-column type="expand">
        <template slot-scope="scope">
          <el-table
            :border="false"
            empty-text="无退款数据"
            :data="scope.row.refundList">
            <el-table-column
              prop="refund_no"
              label="退款编号"
            >
            </el-table-column>
            <el-table-column
              label="退款金额"
            >
              <template slot-scope="item">
                {{item.row.refund_money | moneytFilter}}
              </template>
            </el-table-column>
            <el-table-column
              prop="desc"
              label="备注"
            >
            </el-table-column>
          </el-table>
        </template>
      </el-table-column>
      <el-table-column
        prop="out_trade_no"
        label="订单号">
      </el-table-column>
      <el-table-column
        className="f-c"
        label="用户">
        <template slot-scope="scope">
          <img class="avatar" :src="scope.row.user_avatar"> {{scope.row.user_nickname}}
        </template>
      </el-table-column>
      <el-table-column
        prop="goods_name"
        label="商品名称">
      </el-table-column>
      <el-table-column
        prop="periods_id"
        label="期数ID">
      </el-table-column>
      <el-table-column
        label="实付金额"
      >
        <template slot-scope="scope">
          {{scope.row.money|moneytFilter}}
        </template>
      </el-table-column>
      <el-table-column
        label="推广人" width="150">
        <template slot-scope="scope">
          类型:{{scope.row.invite_type | inviteType}}<br>
          {{scope.row.invite_id ? `ID:${scope.row.invite_id}` : ''}}
        </template>
      </el-table-column>
      <el-table-column
        label="购买方式">
        <template slot-scope="scope">
          {{scope.row.buy_type === 1 ? '团购' : '直购'}}
        </template>
      </el-table-column>
      <el-table-column
        label="是否是团长">
        <template slot-scope="scope">
          {{scope.row.is_captain === 1 ? '是' : '否'}}
        </template>
      </el-table-column>
      <el-table-column
        label="付款状态">
        <template slot-scope="scope">
          {{scope.row.status|status}}
        </template>
      </el-table-column>
      <el-table-column
        prop="pay_at"
        label="支付时间">
      </el-table-column>
      <el-table-column
        prop="created_at"
        label="下单时间">
      </el-table-column>
    </el-table>
    <page :total="total" :limit="limit"  @pageChange="onPageChange" @sizeChange="onSizeChange"/>
    <teacher-dialog :dialogObj="dialogObj" @reflash="getDetail"></teacher-dialog>
    <user-list :userObj="userObj"/>
  </div>
</template>

<script>
  import {getOrderListApi,getUserDetailApi,getPeriodsStatisticsApi,getRefundListApi} from "../../service/api";
  import page from '../framework/page'
  import {INVITETYPE,ORDERSTATUS,BUYTYPE} from "../../util/wordbook";
  import teacherDialog from './dialog'
  import UserList from '../class/userList'
  export default {
    name: "index",
    data(){
      return {
        userList:[],
        total:0,
        nowPage:1,
        limit: 3,
        id: '',
        detail: {},
        periodList: [],
        dialogObj:{
          show:false,
          title:'绑定老师',
          id:0,
          teacher_id: 0
        },
        list: [],
        userObj:{
          classId:'',
          title:'',
          show:false,
        }
      }
    },
    components:{
      page,
      teacherDialog,
      UserList
    },
    mounted(){
      this.id = this.$route.params.id;
      this.getDetail();
      this.getUser()
    },
    methods:{
      changeRow2(data,b){
        if(b.indexOf(data)>-1){
          getRefundListApi({out_trade_no:data.out_trade_no,status:1}).then(res=>{
            data.refundList = res.list
          })
        }
      },
      changeRow(data,b){
          this.list = [];
        if(b.indexOf(data)>-1){
          getPeriodsStatisticsApi(data.periods_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;
            this.list = [data];
          })
        }
      },
      onPageChange(val){
        this.nowPage = val;
        this.getUser();
      },
      onSizeChange(val){
        this.nowPage = 1;
        this.limit = val;
        this.getUser();
      },
      getUser(){
        let json = {
            user_id: this.id,
            limit: this.limit,
            page: this.nowPage,
            status: '1,3'
        };
        getOrderListApi(json).then(res=>{
          res.list.forEach(i=>{
            i.refundList = []
          });
          this.userList = res.list;
          this.total = res.total
        })
      },
      getDetail(){
        if(!this.id) return;
        getUserDetailApi(this.id).then(res=>{
          this.detail = res;
          if (res.periods_list) {
            this.periodList = res.periods_list
          }
        })
      },
      bindTeacher(){
        let data = this.detail
        this.dialogObj = {
          show:true,
          title:'绑定老师',
          id:data.user_id,
          teacher_id: data.teacher_id
        }
      },
      showUser(data){
        this.userObj={
          classId:data.class_id,
          show:true,
          title:`${data.teacher_name}班级用户列表`
        }
      }
    },
    filters:{
      payMentFilter(val){
        return val=='1'?'已付款':'未付款'
      },
      courseTypeFilter(val){
        return val.type=='1'?`正式课(${val.duration}个月)`:`试听课(${val.duration}天)`
      },
      inviteType(value){
        return INVITETYPE[value]
      },
      status(value){
        return ORDERSTATUS[value]
      },
      buyType(value){
        return BUYTYPE[value]
      },
      moneytFilter(val){
        return val = val / 100 + '元'
      },
      percent(val){
        return (val * 100).toFixed(2)+'%'
      }
    }
  }
</script>

<style scoped lang="less">
  @import "../../util/public";
  .avatar {
    width: 50px;
    border-radius: 50%;
  }
  .user{
    height: 100%;
    overflow: auto;
    /*padding: 20px 0;*/
    .btn-content{
      text-align: center;
    }
  }
  .header {
    padding: 0 20px;
    margin-bottom: 20px;
  }
  .header {
    .el-row {
      display: flex;
      justify-content: flex-start;
      align-items: center;
      color: #666;
      font-size: 16px;
      margin: 25px 0;
      label{
        margin-right: 10px;
      }
    }
    .avatar {
      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;
  }
  }
  .b-title {
    padding: 20px;
    color: #666;
  }
</style>

<style>
  .f-c > div {
    display: flex !important;
    flex-flow: row;
    justify-content: flex-start;
    align-items: center;
  }
</style>
