<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="操作" width="400px">
        <template slot-scope="scope">
          <el-button @click="showUser(scope.row)" size="mini" type="primary"> 班级成员</el-button>
          <el-button  size="mini" type="warning"  @click="editPrivilege(scope.row)">编辑</el-button>
          <el-button  size="mini" type="primary" @click="onGetUserDescList(scope.row)">备注</el-button>
          <el-button  size="mini"  type="primary"  @click="userLook(scope.row)" v-if="!$store.state.readonly">看课情况 </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"/>
    <!-- 编辑 -->
     <el-dialog :modal="false" :visible.sync="privilegeDialog.show" title="编辑权限">
      <el-form label-width="120px">
        <el-form-item label="是否添加老师">
          <el-select v-model="privilegeDialog.is_add_teacher" placeholder="请选择">
            <el-option
              v-for="(data,index) in isAddTeacherList"
              :key="index"
              :label="data.label"
              :value="data.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="看课权限">
          <el-switch
            v-model="privilegeDialog.isAllowView"
            active-color="#13ce66"
            inactive-color="#ff4949">
          </el-switch>
        </el-form-item>
        <el-form-item label="意向等级">
          <el-input-number v-model="privilegeDialog.weight" :min="0" :step="1"></el-input-number>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="privilegeDialog.show = false">取 消</el-button>
        <el-button type="primary" @click="changePrivilegeSub()">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 备注 -->
    <el-dialog :modal="false" :visible.sync="descDialog.show" title="备注列表" width="60%">
      <el-form label-width="90px">
        <el-form-item>
          <el-button style="float: right" type="primary" plain @click="editComment(descDialog.id)">添加备注</el-button>
        </el-form-item>
      </el-form>
      <el-table
        :data="descDialog.descList"
        style="width: 100%">
        <el-table-column
          label="用户" className="f-c" min-width="150">
          <template slot-scope="scope" v-if="descDialog.userInfo">
            <img :src="descDialog.userInfo.avatar" style="width: 40px;height: 40px;border-radius: 50px"> {{descDialog.userInfo.nickname}}(ID:{{descDialog.userInfo.user_id}})
          </template>
        </el-table-column>
        <el-table-column
          prop="desc"
          label="备注">
        </el-table-column>
        <el-table-column
          prop="operator"
          label="备注人">
        </el-table-column>
        <el-table-column
        prop="created_at"
        label="创建时间">
        </el-table-column>
        <el-table-column
        prop="updated_at"
        label="更新时间">
        </el-table-column>
      </el-table>
      <page-desc :nowPage="descDialog.nowPage" :total="descDialog.total" :limit="descDialog.limit" @pageChange="onDescDialogPageChange" @sizeChange="onDescDialogSizeChange"/>
    </el-dialog>
<!-- 看课情况 -->
    <el-dialog :modal="false" :visible.sync="lookDetail.show">
      <el-table
        v-loading="lookDetail.loading"
        :data="lookDetail.list"
        style="width: 100%">
        <el-table-column
          prop="start_at"
          label="日期">
        </el-table-column>
        <el-table-column
          label="爸妈看一看">
          <template slot-scope="scope">
            <span v-if="scope.row.watch_list && scope.row.watch_list.length > 0 && scope.row.watch_list.find(i=>{return i.page_id === 1})">
              {{scope.row.watch_list.find(i=>{return i.page_id === 1}).stay_time}}
            </span>
            <span v-if="!scope.row.watch_list.find(i=>{return i.page_id === 1})">
              暂未上课
            </span>
          </template>
        </el-table-column>
        <el-table-column
          label="宝贝玩一玩">
          <template slot-scope="scope">
            <span v-if="scope.row.watch_list && scope.row.watch_list.length > 0 && scope.row.watch_list.find(i=>{return i.page_id === 2})">
              {{scope.row.watch_list.find(i=>{return i.page_id === 2}).stay_time}}
            </span>
            <span v-if="!scope.row.watch_list.find(i=>{return i.page_id === 2})">
              暂未上课
            </span>
          </template>
        </el-table-column>
        <el-table-column
          label="爸妈秀宝贝">
          <template slot-scope="scope">
            <span v-if="scope.row.watch_list && scope.row.watch_list.length > 0 && scope.row.watch_list.find(i=>{return i.page_id === 3})">
              <div v-html="scope.row.watch_list.find(i=>{return i.page_id === 3}).learn_report"></div>
            </span>
            <span v-if="!scope.row.watch_list.find(i=>{return i.page_id === 3})">
              暂未秀宝贝
            </span>
          </template>
        </el-table-column>
        <el-table-column
          label="多元趣味课">
          <template slot-scope="scope">
            <span v-if="scope.row.watch_list && scope.row.watch_list.length > 0 && scope.row.watch_list.find(i=>{return i.page_id === 5})">
              {{scope.row.watch_list.find(i=>{return i.page_id === 5}).stay_time}}
            </span>
            <span v-if="!scope.row.watch_list.find(i=>{return i.page_id === 5})">
              暂未上课
            </span>
          </template>
        </el-table-column>
        <el-table-column
          label="分享">
          <template slot-scope="scope">
            <span v-if="scope.row.watch_list && scope.row.watch_list.length > 0 && scope.row.watch_list.find(i=>{return i.page_id === 4})">
              已分享
            </span>
            <span v-if="!scope.row.watch_list.find(i=>{return i.page_id === 4})">
              暂未分享
            </span>
          </template>
        </el-table-column>
      </el-table>
      <page2 :nowPage="lookDetail.nowPage" :total="lookDetail.total" :limit="lookDetail.limit" @pageChange="onLookDetailPageChange" @sizeChange="onLookDetailSizeChange"/>
    </el-dialog>

  </div>
</template>

<script>
  import {getOrderListApi,getUserDetailApi,getPeriodsStatisticsApi,getRefundListApi,userLookApi,getUserDescListApi,addPeriodsClassUserDescApi,updateUserPrivilegeApi} from "../../service/api";
  import page from '../framework/page'
   import page2 from '../framework/page'
  import pageDesc 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,
        },

         privilegeDialog: {
          show: false,
          isAllowView: false,
          is_add_teacher: 0,
          weight: 0,
          id: ''
        },
         descDialog: {
          show: false,
          descList: [],
          nowPage: 1,
          limit: 10,
          total:0,
          userInfo: null
        },
         lookDetail:{
          show:false,
          loading:false,
          list:[],
          nowPage: 1,
          limit: 10,
          total:0
        },
        isAddTeacherList: [
          {
            label: '未加好友',
            value: 0
          },
          {
            label: '老师主动添加用户',
            value: 1
          },
          {
            label: '用户主动添加老师',
            value: 2
          }
        ],
      }
    },
    components:{
      page,
      teacherDialog,
      UserList,
      refundDetail,
      sourceDialog,
      couponDialog,
        page2,
      pageDesc,
    },
    mounted(){
      this.id = this.$route.params.id;
      this.getDetail();
      this.getUser()
    },
    methods:{
       getUserLook(){
        if(!this.lookDetail.data) return;
        this.lookDetail.show = true;
        this.lookDetail.loading = true;
        let json = {
          limit: this.lookDetail.limit,
          page: this.lookDetail.nowPage
        }
        let data = this.lookDetail.data;
        userLookApi(data.periods_id,data.user_id, json).then(res=>{
          this.lookDetail.list = res.list;
          this.lookDetail.loading = false;
          this.lookDetail.total = res.total;
        })
      },
       onLookDetailPageChange(val){
        this.lookDetail.nowPage = val
        this.getUserLook();
      },
      onLookDetailSizeChange(val){
        this.lookDetail.limit = val
        this.lookDetail.nowPage = 1;
        this.getUserLook();
      },
       onDescDialogPageChange(val){
        this.descDialog.nowPage = val
        this.getUserDescList()
      },
      onDescDialogSizeChange(val){
        this.descDialog.limit = val
        this.descDialog.nowPage = 1;
        this.getUserDescList()
      },
      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
        }
      },
      //编辑备注 看课情况
        editPrivilege(val){
        this.privilegeDialog = {
          id: val.id,
          isAllowView: val.is_view_course === 1,
          is_add_teacher: val.is_add_teacher ? val.is_add_teacher : 0,
          weight: val.weight ? val.weight : 0,
          show: true
        }
      },
       onGetUserDescList(val){
        this.descDialog.show = true;

        this.descDialog.userInfo = {
          user_id: val.user_id,
          avatar: this.detail.avatar,
          nickname: this.detail.nickname
        };
        this.descDialog.id = val.id;
        this.getUserDescList();
      },
       getUserDescList(){
        let json = {
          page: this.descDialog.nowPage,
          limit: this.descDialog.limit
        };
        getUserDescListApi(this.descDialog.id,json).then(res=>{
          this.descDialog.descList = res.list;
          this.descDialog.total = res.total;
        })
      },
       userLook(data){
        this.lookDetail.show = true;
        this.lookDetail.loading = true;
        let json = {
          limit: this.lookDetail.limit,
          page: this.lookDetail.nowPage
        }
        this.lookDetail.data = {
          periods_id:data.periods_id,
          user_id: data.user_id
        }
        userLookApi(data.periods_id,data.user_id, json).then(res=>{
          this.lookDetail.list = res.list;
          this.lookDetail.loading = false;
          this.lookDetail.total = res.total;
        })
      },
       editComment(id) {
        this.$prompt('', '添加备注', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputValue: ''
        }).then(({ value }) => {
          addPeriodsClassUserDescApi(id,{desc: value}).then(res=>{
            this.$message({
              type: 'success',
              message: '编辑备注成功'
            })
            this.getUserDescList();
          });
        })
      },
       changePrivilegeSub(){
        this.$confirm('此操作将修改用户权限?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let _json = {
            is_view_course: this.privilegeDialog.isAllowView ? 1 : 0,
            is_add_teacher: this.privilegeDialog.is_add_teacher,
            weight: this.privilegeDialog.weight
          }
          updateUserPrivilegeApi(this.privilegeDialog.id,_json).then(res=>{
            this.$message({
              type: 'success',
              message: '修改成功!'
            });
            this.privilegeDialog.show = false;
            // this.getUser()
            this.getDetail()
          });
        });
      },
    },
    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>
