<template>
  <div class="user" v-loading="loading">
    <el-form ref="searchFrom" :model="searchFrom" label-width="80px" inline>
          <el-form-item label="ID">
            <el-input v-model="searchFrom.userId"></el-input>
          </el-form-item>
          <el-form-item label="昵称">
            <el-input v-model="searchFrom.nickName"></el-input>
          </el-form-item>
          <el-form-item label="电话">
            <el-input v-model="searchFrom.mobile"></el-input>
          </el-form-item>
          <el-form-item label="等级">
            <el-input v-model="searchFrom.level"></el-input>
          </el-form-item>
          <el-form-item>
            <div class="flexRow">
              <el-button type="primary" plain @click="getUser">搜索</el-button>
              <el-button type="success" plain @click="syncUser">同步最新数据</el-button>
            </div>
          </el-form-item>
    </el-form>
    <el-table
      :data="userList"
      style="width: 100%">
      <el-table-column
        className="f-c"
        label="用户">
        <template slot-scope="scope">
          <img class="avatar" :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-column
        prop="created_at"
        label="注册时间" sortable>
      </el-table-column>
      <el-table-column
        prop="last_login_at"
        label="最后登录时间" sortable>
      </el-table-column>
      <el-table-column
        label="操作">
        <template slot-scope="scope">
          <el-button size="mini" plain type="primary" @click="goToDetail(scope.row.user_id)">
            查看详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <page :total="total" :limit="limit" @pageChange="onPageChange" @sizeChange="onSizeChange"/>
    <detail-dialog :dialogObj="dialogDetailObj" v-if="dialogDetailObj.show" @changeShow="changeShow"/>
    <teacher-dialog :dialogObj="dialogObj" v-if="dialogObj.show" @reflash="getUser"></teacher-dialog>
  </div>
</template>

<script>
  import {getUserListApi,getSyncUserApi} from "../../service/api";
  import page from '../framework/page'
  import detailDialog from './detail'
  import teacherDialog from './dialog'
  export default {
    name: "index",
    data(){
      return {
        searchFrom:{
          nickName:'',
          mobile:'',
          level:'',
          userId: ''
        },
        userList:[],
        total:0,
        nowPage:1,
        limit: 10,
        showDetail:false,
        showId:'',
        dialogObj:{
          show:false,
          title:'绑定老师',
          id:0,
          teacher_id: 0
        },
        dialogDetailObj: {
          show: false,
          id: ''
        },
        loading: false
      }
    },
    components:{
      page,
      detailDialog,
      teacherDialog
    },
    mounted(){
      this.getUser()
    },
    methods:{
      onPageChange(val){
        this.nowPage = val
        this.getUser()
      },
      onSizeChange(val){
        this.limit = val;
        this.nowPage = 1;
        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
        }
        if (this.searchFrom.level) {
          json.level = this.searchFrom.level
        }
        getUserListApi(json).then(res=>{
          this.userList = res.list;
          this.total = res.total
        })
      },
      detail(data){
        this.dialogDetailObj = {
          show: true,
          id: data.user_id
        }
      },
      changeShow(data){
        this.dialogDetailObj.show=data
      },
      bindTeacher(data){
        this.dialogObj = {
          show:true,
          title:'绑定老师',
          id:data.user_id,
          teacher_id: data.teacher_id
        }
      },
      goToDetail(id){
        this.$router.push('/userDetail/'+ id);
      },
      syncUser(){
        this.loading = true
        getSyncUserApi().then((data)=>{
          this.loading = false
          this.$message({
            showClose: true,
            message: `已更新${data}条用户数据`,
            type: 'success'
          })
        }).catch(()=>{
          this.loading = false
        })
      }
    }
  }
</script>

<style scoped lang="less">
  @import "../../util/public";
  .avatar {
    width: 50px;
    margin-right: 5px;
    border-radius: 50%;
    height: 50px;
  }
  .user{
    height: 100%;
    overflow: auto;
    padding: 20px 0;
    .btn-content{
      text-align: center;
    }
  }
  .flexRow {
    display: flex;
    flex-flow: row;
    justify-content: flex-start;
    align-items: center;
  }

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