<template>
    <div class="main-content__div">
      <div class="table-box__div" >
      <el-form  class="demo-form-inline">
        <el-form-item label="会员名称">
          <el-input
            placeholder="会员名称"
            v-model="keyWord"
            style="width:150px;"
            size="small"
            >
          </el-input>
        </el-form-item>
        
        <!-- 搜索按钮 -->
        <el-form-item>
          <el-button type="primary" size="small" @click="searchFrom()">搜索</el-button>
        </el-form-item>
       
      </el-form>
      </div>
      <!-- 列表 -->
      <div class="table-box__div" v-cloak v-loading="loadingFlag">
        <!-- 查询表单 -->
        <el-form :inline="true" class="demo-form-inline">
          <el-form-item>
            <el-button type="primary" size="small" @click="addClick">添加会员</el-button>
          </el-form-item>
        
        </el-form>
        <el-table
          size="mini"
          :data="tableData"
          :empty-text="tableDataLoadingText"
          style="width: 100%;font-size: 15px;"
          :header-cell-style="{background:'#FAFAFA'}"
        >
          <el-table-column label="编号" width="100" prop="id"></el-table-column>
          <el-table-column label="头像"  width="80">
            <template v-slot="scope">
              <el-image
              style="width: 50px; height: 50px"
              :src="scope.row.avatar"
              ></el-image>
          </template>
          </el-table-column>
          <el-table-column label="会员名" prop="user_name" width="100"></el-table-column>
          <el-table-column label="会员类型" prop="type_name" width="100"></el-table-column>
          <el-table-column label="状态" prop="status_name" width="100"></el-table-column>
          <el-table-column label="会员等级" prop="level_name" width="100"></el-table-column>
          <el-table-column label="性别" prop="gender_name" width="100"></el-table-column>
          <el-table-column label="真实姓名" prop="real_name" width="100"></el-table-column>
          <el-table-column label="手机" prop="mobile" width="150"></el-table-column>
          <el-table-column label="职务" prop="job" width="150"></el-table-column>
          <el-table-column label="邮箱" prop="email" width="150"></el-table-column>
          <el-table-column label="更新时间" prop="updated_at" width="180"></el-table-column>

          <el-table-column   prop="" label="操作" align="center"  fixed="right" width="200">
                  <template v-slot="scope">
                    <div style="display: flex;flex-direction: row;justify-content: center;align-items: center;">
                      <span class="btn"  @click="updateClick(scope.row, scope.$index)">编辑</span>
                      <span class="btn"  @click="deleteAuthority(scope.row,scope.$index)">删除</span>
                    </div>
                  </template>
                </el-table-column>
        </el-table>
        <!-- 分页 -->
        <div class="block" v-show="pagination" style="margin-top: 10px;float: right;">
          <el-pagination
            background
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-size="pageSize"
            layout="total, prev, pager, next, jumper"
            :total="parseInt(tableCount)">
          </el-pagination>
        </div>
      </div>
    </div>
  <Dialog title="温馨提示" content="您确认要删除该用户吗？"
          @close="DialogShow = false"
          @submitSureClick="submitSureClickDel"
          @DialogFalse="DialogShow = false"
          :innerVisible="DialogShow">
  </Dialog>

  <DialogSlot
      title="添加/编辑"
      @close="memberDialogShow = false"
      @DialogClose="memberDialogShow = false"
      :innerVisible="memberDialogShow"
      @colesClick="memberDialogShow  = false"
  >
    <addEdit
        @colesClick="memberDialogShow = false"
        :memberDataForm="memberDataForm"
         :isloading="isloading"
        @memberSubmitClick="memberSubmitClick"
        @closeDlg="closeDlg"
        >
    </addEdit>
  </DialogSlot>
  </template>
  <script>
  import Dialog from "../../components/dialog";
  import addEdit from "./components/addEdit";
  import DialogSlot from "../../components/DialogSlot";
  export default {
    components: {
      Dialog,
      addEdit,
      DialogSlot
    },
    data() {
      return {
        value:"1",
        //  站点数据
        tableData: [],
        //  加载中数据提示
        tableDataLoadingText:"拼命加载中······",
        //  是否显示分页
        pagination:false,
        //  每页显示的条数
        pageSize:10,
        //  当前页
        currentPage:1,
        //  菜单列表数据总条数
        tableCount:0,
        //关键字
        keyWord:'',
        loadingFlag: false,
      
    
        RowIndex:'',
        DataForm: {},
  
        isloading:false, //提交按钮状态
        addDialogShow: false, // 控制保存弹窗



        DialogShow: false, //控制删除弹窗
        memberDialogShow: false, // 控制保存弹窗
      }
    },
    mounted() {},
    methods: {
      // 获取用户信息列表
      getList:function(){
        let _t = this;
        _t.loadingFlag = true;
        let parames = {
            'page':this.currentPage,
            'pageSize':this.pageSize,
            'keyWord':this.keyWord
        }
        this.$api.user.getUserList(parames).then(res=>{
                _t.loadingFlag = false;
                if(res.code==200){
                  if(res.data.rows.length==0){
                    this.$message.error("没有数据了")
                    return
                  }
                    _t.tableData = res.data.rows;
                    _t.tableCount = res.data.count;
                     //  总条数大于每页显示的条数时显示分页
                    if( _t.tableCount > _t.pageSize ){
                     _t.pagination = true;
                    }
                    if( _t.tableData.length==0 ){
                     _t.tableDataLoadingText = "暂无数据";
                    }
                }else{
                    this.$message.error("查询失败")
                }
            })
      },
      // 点击翻页
      handleCurrentChange(val) {
        this.currentPage = val;
        this.getList();
      },

      addClick(){
        this.memberDataForm = {}
        this.memberDialogShow = true
      },

      //更新菜单
      updateClick(row,index){
        this.memberDataForm = row
        this.rowIndex = index
        this.memberDialogShow = true
      },
      closeDlg(val){
        this.memberDialogShow = false
        this.DialogShow = false
        this.isloading = false
  },
  memberSubmitClick(data){
        let _t = this
        console.log("提交数据",data)
        let parames = data
        _t.isloading = true
        //有id更新，没有新增
        if(parames.id){
          this.$api.user.updateUser(parames).then(res=>{
                _t.loadingFlag = false;
                if(res.code==200){
                  _t.tableData[_t.rowIndex] = parames
                     console.log("修改成功")
                    //  location.reload();
                    this.getList();
                }else{
                    this.$message.error("修改失败")
                }
                this.memberDialogShow = false
                this.DialogShow = false
                this.isloading = false
            })
        }else{
          this.$api.user.createUser(parames).then(res=>{
                _t.loadingFlag = false;
                if(res.code==200){
                     console.log("创建成功")
                     this.getList();
                }else{
                    this.$message.error(res.message)
                }
                this.memberDialogShow = false
                this.DialogShow = false
                this.isloading = false
            })
        }
       
      },
      //删除菜单
      deleteAuthority(row, index) {
        this.Id = row.id
        this.RowIndex = index
        this.DialogShow = true
      },
      submitSureClickDel(row,index){
        let parames = {
          'id':this.Id
        }
        this.$api.user.delUser(parames).then(res=>{
          if(res.code==200){
            this.DialogShow = false
            this.tableData.splice(index, 1);
            this.getList()
          }else{
            this.$message.error("删除失败")
          }
        })
      },
      //审核
      toExamine(){

      },
      searchFrom(){
        this.currentPage = 1
        this.pageSize = 10
        this.getList()
      }

    },
    created() {
        this.getList();
    }
  }
  </script>
  <style >
   .demo-form-inline{
      display: flex;
   }
   .demo-form-inline .el-form-item{
    margin: 0 20px;
   }
  </style>
  
  