<template>
    <div class="layout-content-wrap background-color-fff border-radius-8">
        <div class="border-b-solid-lighter-1 p-h-24">
            <div class="flex-row justify-content-between height-56 align-items-center">
                <div class="color-text-primary f22">用户列表</div>
                <div class="color-text-regular f14" >
                    <el-button type="primary" @click="handleAddForm" size="small">新增</el-button>
                </div>
            </div>
        </div>
        <div class="layout-tabs-content-box">
            <!-- table -->
            <div class="">
              <el-table ref="monthlyPlanTable"
                :data="tableData"
                :header-cell-style="{background:'#F5F7FA'}"
              >
              <el-table-column type="index" label="序号" width="55"></el-table-column>
              <el-table-column prop="realName" label="姓名"></el-table-column>
              <el-table-column prop="phone" label="手机号码"></el-table-column>
              <el-table-column prop="email" label="电子邮箱"></el-table-column>
              <!-- <el-table-column prop="username" label="用户账号"></el-table-column> -->
              <el-table-column prop="createAt" label="创建时间"></el-table-column>
                <el-table-column prop="platformPermission" label="有效状态">
                  <template slot-scope="scope">
                      <el-switch
                            v-model="scope.row.status"
                            active-color="#535AFF"
                            inactive-color="#dcdfe6"
                            :active-value="1"
                            :inactive-value="0"
                            @change="changeStatus(scope.row)">
                        </el-switch>

                  </template>
                </el-table-column>

                
              <el-table-column  label="操作" width="180">
                <template slot-scope="scope">
                    <div class="flex-row">
                        <div class="f14 color-1960F4 cursor-pointer mr-8" @click="handleAddForm(scope)">编辑</div>
                        <div class="f14 color-1960F4 cursor-pointer" @click="handleReset(scope.row)">重置密码</div>
                    </div>
                  
                </template>
              </el-table-column>
            </el-table>

            <div class="text-center p-v-24">
                  <el-pagination
                      @size-change="getUserList"
                      @current-change="getUserList"
                      :current-page="queryData.current"
                      :page-size="queryData.size"
                      layout="total, prev, pager, next, jumper"
                      :total="total">
                  </el-pagination>
            </div> 
          </div>

        </div>
        <!-- 新增-编辑 -->
        <addOrEditUserDialog v-if="addOrEditUserConfig" :addOrEditUserConfig.sync="addOrEditUserConfig" />
    </div>
</template>
<script>
    import systemManageApi from "@/services/systemManage";
    export default {
        components: {
           addOrEditUserDialog: () => import('./components/addOrEditUserDialog.vue'),
        },
        data() {
            return {
                tableData:[],
                queryData: {
                    current: 1,
                    size: 10,
                    type: 2
                },
                total: 0,
                addOrEditUserConfig:null,//新增弹窗
            };
        },
        created() {
            this.getUserList(1)
        },
        methods: {
            // 新增
            handleAddForm(scope){
                let title = scope.row?'编辑用户':'新增用户'
                let data = scope.row?scope.row:''
                this.addOrEditUserConfig={
                    title:title,
                    data: data
                }
            },
            customerListPagination () {
                this.$store.commit('', this.queryData)
            },
            // 改变状态
            changeStatus(row) {
                systemManageApi.editStatus({
                    id: row.id,
                    status: row.status,
                }).then(res => {
                    this.getUserList()
                })
            },
            
            getUserList(val){
                if(val!=undefined){this.queryData.current = val}
                systemManageApi.getUserList(this.queryData).then(res => {
                    if (!res.code) {
                        this.tableData = res.records;
                        this.total = res.total;
                    }
                })
            },
            // 重置密码
            handleReset(row) {
                this.$confirm(`确定重置用户：${row.realName} 的密码, 是否继续?`, '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    if(!this.$clickThrottle()) { return }//防止重复点击
                    systemManageApi.resetPassWord({id: row.id}).then(res => {
                        this.$message.success('重置成功')
                    })
                })
            },
            // 解除锁定
            handleLock(row) {
                this.$confirm(`确定解除用户：${row.realName} 的锁定状态, 是否继续?`, '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    if(!this.$clickThrottle()) { return }//防止重复点击
                    systemManageApi.unlockUser({id: row.id}).then(res => {
                        this.$message.success('解除锁定成功')
                    })
                })
            },
        }
        
    };
</script>
<style lang="scss" scoped>
.el-table--fit {
    border:1px solid $border-color-lighter !important;
    border-bottom: 0!important;
}
    
</style>