<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"
      style="width: 100%">
      <el-table-column
        prop="out_trade_no"
        label="订单号"
      >
      </el-table-column>
      <el-table-column
        label="购买人"
        className="f-c"
        width="150">
        <template slot-scope="scope">
          <img class="avatar" :src="scope.row.user_avatar"/> {{scope.row.user_nickname}}<br>(ID:{{scope.row.user_id}})
        </template>
      </el-table-column>
      <el-table-column
        prop="goods_name"
        label="商品名称">
      </el-table-column>
      <el-table-column
        width="250"
        prop="invite_id"
        className="f-c"
        label="推广人属性">
        <template slot-scope="scope">
          <div @click="showSource(scope.row)" v-if="scope.row.invite_earnings > 0 && scope.row.invite_id !== 0" style="display: flex;color: #409eff;cursor: pointer">
            <img :src="scope.row.invite_avatar" class="avatar"/>
            类型:{{scope.row.invite_type}}
            <br>
            收益:{{scope.row.invite_earnings / 100}}
            <br>
            用户ID:{{scope.row.invite_id}}
            <br>
            昵称:{{scope.row.invite_nickname}}
            <br>
            手机:{{scope.row.invite_mobile}}
          </div>
          <div v-if="scope.row.invite_earnings === 0 && scope.row.invite_id !== 0" style="display: flex;">
            <img :src="scope.row.invite_avatar" class="avatar"/>
            类型:{{scope.row.invite_type}}
            <br>
            收益:{{scope.row.invite_earnings / 100}}
            <br>
            用户ID:{{scope.row.invite_id}}
            <br>
            昵称:{{scope.row.invite_nickname}}
            <br>
            手机:{{scope.row.invite_mobile}}
          </div>
          <div v-if="scope.row.invite_id === 0">
            无
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="优惠活动">
        <template slot-scope="scope">
          <span v-if="scope.row.order_coupon_id === 0">
            无
          </span>
          <el-button type="text" v-if="scope.row.order_coupon_id !== 0" @click="showCoupon(scope.row)">
            优惠券
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        label="付款状态"
        width="80">
        <template slot-scope="scope">
          <el-button type="text" v-if="scope.row.status === 5 || scope.row.status === 3" @click="showRef(scope.row)">{{scope.row.status|status}}</el-button>
          <div v-if="scope.row.status !== 5 && scope.row.status !== 3">{{scope.row.status|status}}</div>
        </template>
      </el-table-column>
      <el-table-column
        label="实付金额"
        width="80">
        <template slot-scope="scope">
          {{scope.row.money|moneytFilter}}
        </template>
      </el-table-column>
      <el-table-column
        label="收货地址">
        <template slot-scope="scope">
          <div v-if="scope.row.user_address_id && scope.row.address_info">
            {{scope.row.address_info.receive_name}}<br>
            {{scope.row.address_info.receive_mobile}}<br>
            {{scope.row.address_info.province_name}}{{scope.row.address_info.city}}{{scope.row.address_info.area}}{{scope.row.address_info.address}}
          </div>
        </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"/>
    <source-dialog :dialogObj="sourceDialog"/>
    <refund-detail :dialogObj="refundDetail"/>
    <coupon-dialog :dialogObj="couponDetail"/>
  </div>
</template>

<script>
  import {getOrderListApi,getUserDetailApi,getPeriodsStatisticsApi,getRefundListApi} from "../../service/api";
  import page from '../framework/page'
  import sourceDialog from '../order/sourceDialog'
  import refundDetail from '../order/refundDetail'
  import couponDialog from '../order/couponDialog'
  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,
        sourceDialog:{
          show:false,
          out_trade_no:''
        },
        couponDetail:{
          show:false,
          order_coupon_id:''
        },
        refundDetail:{
          show:false,
          out_trade_no:''
        },
        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,
      refundDetail,
      sourceDialog,
      couponDialog,
    },
    mounted(){
      this.id = this.$route.params.id;
      this.getDetail();
      this.getUser()
    },
    methods:{
      showRef(data){
        this.refundDetail.show = true;
        this.refundDetail.out_trade_no = data.out_trade_no;
      },
      showCoupon(data){
        this.couponDetail.show = true;
        this.couponDetail.order_coupon_id = data.order_coupon_id;
      },
      showSource(data){
        this.sourceDialog.show = true;
        this.sourceDialog.out_trade_no = data.out_trade_no;
      },
      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}班级用户列表`,
          teacherId: data.teacher_id,
          periods_id: data.periods_id
        }
      }
    },
    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;
    height: 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>