<template>
  <div class="system-user">
    <!-- 头部细节部分 -->
      <div class="title">
        <el-row>
          <el-col :span="24">
            <div class="top">
              <el-input  placeholder="用户" class="input-demo" v-model="userName"></el-input>
              <el-input  placeholder="部门" class="input-demo" v-model="company"></el-input>
               <el-button type="success" style="margin-left: 1%;" @click="queryLook">查询</el-button>
                <el-button type="success" style="margin-left: 1%;" @click="addUserList = true">添加</el-button>
            </div>
          </el-col>
        </el-row>

         <!-- <el-row>
          <el-col :span="24">
            <div class="top">
              <el-button type="success" style="margin-left: 1%;" @click="queryLook">查询</el-button>
              <el-button type="success" style="margin-left: 1%;" @click="addUserList = true">添加</el-button>
            </div>
          </el-col>
        </el-row> -->

      </div>
    <!-- 头部细节部分结束 -->
    <!-- 表格部分 -->
      <template>
        <el-table
          v-loading="loading"
          ref="multipleTable"
          :data="usertable"
           height="370px"
          border
          tooltip-effect="dark">
          <el-table-column
            label="用户名"
            prop="userName"
            show-overflow-tooltip>
          </el-table-column>
           <el-table-column
            prop="company"
            label="部门"
            show-overflow-tooltip>
          </el-table-column>
          <el-table-column
            prop="roleName"
            label="权限"
            show-overflow-tooltip>
             <!-- <template slot-scope="scope">
               <el-popover
              placement="top"
              width="400"
              trigger="click"
              >
              <el-select v-model="tempRole" style="width:400px" @change="function(data){changeRole(data,scope.row)}" multiple placeholder="请选择角色">
              <el-option
                v-for="item in roleList"
                :key="item.id"
                :label="item.roleName"
                :value="item.id">
              </el-option>
              </el-select>
               <el-link :underline="false"  type="primary" slot="reference" @click="showRole(scope.row)"> {{scope.row.roleName}}</el-link>
             </el-popover>
            </template> -->
          </el-table-column>
          <!-- <el-table-column
            prop="phone"
            label="手机号"
            show-overflow-tooltip>
          </el-table-column> -->
          <!-- <el-table-column
            prop="name"
            label="姓名"
            show-overflow-tooltip> -->
          <!-- </el-table-column> -->
          <!-- <el-table-column
            prop="price"
            label="单价(元)"
            show-overflow-tooltip>
            <template slot-scope="scope" show-overflow-tooltip>
              <span>{{scope.row.price / 100}}</span>
            </template>
          </el-table-column> -->
          <!-- <el-table-column
            label="余额(元)"
            show-overflow-tooltip>
             <template slot-scope="scope" show-overflow-tooltip>
                <span>{{scope.row.money / 100}}</span>
              </template>
          </el-table-column> -->
          <!-- <el-table-column
            prop="dutyParagraph"
            label="税号"
            show-overflow-tooltip>
          </el-table-column> -->
         
          <!-- <el-table-column
            prop="companyNum"
            label="企业编号"
            show-overflow-tooltip>
          </el-table-column> -->
          <el-table-column
            label="状态"
            show-overflow-tooltip>
             <template slot-scope="scope">
              <span v-if="scope.row.isLock == 0" style="color:green">正常</span>
              <span v-else-if="scope.row.isLock == 1" style="color: red">锁定</span>
              <span v-else-if="scope.row.isLock == 2" style="color: darkgrey">停用</span>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            width="280">
            <template slot-scope="scope" show-overflow-tooltip>
              <el-link style="cursor:pointer;color: blue;" @click="openChange(scope.row)">修改</el-link>
              <el-link style="margin-left: 10px; cursor:pointer;color: blue;" @click="openPassword(scope.row)">重置密码</el-link>
              <el-link style="margin-left: 10px; cursor:pointer;color: blue;" @click="openisLock(scope.row)">修改状态</el-link>
              <el-link style="margin-left: 10px; cursor:pointer; color: red;" @click="Delete(scope.row.id)">删除</el-link>
            </template>
          </el-table-column>
        </el-table>
      </template>

    <!-- 分页 -->
      <div class="block">
        <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="current"
        :page-sizes="[6, 8, 10]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
        </el-pagination>
      </div>

    <!-- 新增用户 -->
      <el-dialog
        @close="handleEditDialogClose"
        :visible.sync="addUserList"
        style="font-size: 0px;">
        <div class="tou">用户--添加</div>
        <div class="line"></div>
        <el-form
          label-position="right"
          label-width="80px"
          :rules="rules"
          ref="ruleForm"
          :model="formUserList">
          <el-form-item label="用户名" prop="userName">
            <el-input v-model="formUserList.userName" auto-complete="off" placeholder="请输入用户名"></el-input>
          </el-form-item>

          <el-form-item label="密码" prop="password">
            <el-input v-model="formUserList.password" auto-complete="off" placeholder="请输入密码"></el-input>
          </el-form-item>

          <el-form-item label="姓名" prop="name">
            <el-input v-model="formUserList.name" auto-complete="off" placeholder="请输入姓名"></el-input>
          </el-form-item>

          <el-form-item label="角色" prop="roleId">
            <el-select v-model="formUserList.roleId" placeholder="请选择角色" style="width: 100%">
              <el-option
                v-for="item in roleList"
                :key="item.id"
                :label="item.roleName"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>

         

          <el-form-item label="手机号" prop="phone">
            <el-input v-model="formUserList.phone" auto-complete="off" placeholder="请输入手机号"></el-input>
          </el-form-item>

  

          <el-form-item label="企业名称" prop="company">
            <el-input v-model="formUserList.company" auto-complete="off" placeholder="请输入企业名称"></el-input>
          </el-form-item>

          <el-form-item label="状态" prop="isLock">
            <el-select v-model="formUserList.isLock" placeholder="请选择状态" style="width: 100%">
              <el-option
                v-for="item in optionone"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>

        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="addUserList = false" type="info">取 消</el-button>
          <el-button type="primary" @click="addData('ruleForm')">新 增</el-button>
        </div>
      </el-dialog>
    <!-- 修改用户 -->
      <el-dialog
        @close="handleEditDialogClose"
        :visible.sync="changeUser"
        style="font-size: 0px;">
        <div class="tou">用户--修改</div>
        <div class="line"></div>
        <el-form
          label-position="right"
          label-width="80px"
          :model="formUserList">

          <el-form-item label="用户名">
            <el-input :disabled="true" v-model="formUserList.userName" auto-complete="off" placeholder="请输入用户名"></el-input>
          </el-form-item>

          <el-form-item label="姓名">
            <el-input v-model="formUserList.name" auto-complete="off" placeholder="请输入姓名"></el-input>
          </el-form-item>

          <!-- <el-form-item label="角色">
            <el-select v-model="formUserList.roleId" placeholder="请选择角色" style="width: 100%">
              <el-option
                v-for="item in roleList"
                :key="item.id"
                :label="item.roleName"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item> -->

          <el-form-item label="手机号">
            <el-input v-model="formUserList.phone" auto-complete="off" placeholder="请输入手机号"></el-input>
          </el-form-item>


          <el-form-item label="企业名称">
            <el-input v-model="formUserList.company" auto-complete="off" placeholder="请输入企业名称"></el-input>
          </el-form-item>

        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="changeUser = false" type="info">取 消</el-button>
          <el-button type="primary" @click="changeData">修 改</el-button>
        </div>
      </el-dialog>
    <!-- 修改锁定和解锁状态 -->
      <el-dialog
        @close="handleEditDialogClose"
        :visible.sync="changelocks"
        style="font-size: 0px;">
        <div class="tou">用户--修改状态</div>
        <div class="line"></div>
        <el-form
          label-position="right"
          label-width="80px"
          :model="formUserList">
          <el-form-item label="用户名">
            <el-input v-model="formUserList.userName" auto-complete="off" placeholder="请输入用户名"></el-input>
          </el-form-item>
          <el-form-item label="状态">
            <el-select v-model="formUserList.isLock" placeholder="请选择状态" style="width: 100%">
              <el-option
                v-for="item in optionone"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="changelocks = false" type="info">取 消</el-button>
          <el-button type="primary" @click="changeLock">修 改</el-button>
        </div>
      </el-dialog>
      <!-- 重置密码 -->
      <el-dialog
        @close="handleEditDialogClose"
        :visible.sync="changepassword"
        style="font-size: 0px;">
        <div class="tou">用户--重置密码</div>
        <div class="line"></div>
        <el-form
          label-position="right"
          label-width="80px"
          :model="formUserList">
          <el-form-item label="用户名">
            <el-input v-model="formUserList.userName" auto-complete="off" placeholder="请输入用户名"></el-input>
          </el-form-item>
          <el-form-item label="新密码">
            <el-input v-model="formUserList.password" type="password" auto-complete="off" placeholder="请输入新密码"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="changepassword = false" type="info">取 消</el-button>
          <el-button type="primary" @click="resetPassword">修 改</el-button>
        </div>
      </el-dialog>
      <!-- 充值 -->
      <!-- <el-dialog
        @close="handleEditDialogClose"
        :visible.sync="changeMoney"
        style="font-size: 0px;">
        <div class="tou">用户--充值</div>
        <div class="line"></div>
        <el-form
          label-position="right"
          label-width="80px"
          :model="formUserList">
          <el-form-item label="用户名">
            <el-input v-model="formUserList.userName" auto-complete="off" placeholder="请输入用户"></el-input>
          </el-form-item>
          <el-form-item label="充值金额(元)">
            <el-input v-model="formUserList.money" auto-complete="off" placeholder="请输入充值金额(元)"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="changeMoney = false" type="info">取 消</el-button>
          <el-button type="primary" @click="resetMoney">充 值</el-button>
        </div>
      </el-dialog> -->
  </div>
</template>

<script type="text/javascript">
import axios from 'axios';
export default{
  data() {
    return {
      loading: true,
      rules: {
          userName: [
            { required: true, message: '请输入用户名', trigger: 'blur' },
            { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '请输入真实姓名', trigger: 'blur' },
            { min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur' }
          ],
          roleId: [
            { required: true, message: '请选择角色', trigger: 'change' }
          ],
          phone: [
            { required: true, message: '请输入手机号', trigger: 'blur' },
            { min: 11, max: 11, message: '长度在 11 个字符', trigger: 'blur' }
          ],
          
          company: [
            { required: true, message: '请输入企业名称', trigger: 'blur' },
            { min: 2, max: 25, message: '长度在 2 到 25 个字符', trigger: 'blur' }
          ],
          isLock: [
            { required: true, message: '请选择状态', trigger: 'change' }
          ]
        },
      userName: '',
      company: '',
      usertable: [],
      roleList: [],
      tempRole:[],
      formUserList: {
        "userName":"",
        "password": "",
        "id":"",
        "name": "",
        "phone":"",
        "roleId": "",
        "isLock":"",
        "company": ""
      },
      optionone: [{
          value: '0',
          label: '正常'
        }, {
          value: '1',
          label: '锁定'
        },{
           value: '2',
          label: '停用'
        }],
      current: 1,
      pagesize: 8,
      // 总共有多少条数据
      total: 0,
      addUserList: false,
      changeUser: false,
      changeMoney: false,
      changelocks: false,
      changepassword: false,
    }
  },
  created() {
    this.loadRoleList();
    this.loadData();
  },
  methods: {
    // 获取角色状态
    async loadRoleList() {
      const response = await this.$http.post(`role/list`);
      if (response.data.code === 0) {
        this.roleList = response.data.data;
      }
    },
    // showRole(row){
    //     this.tempRole=[];
    //     if(row.roleId == '' || row.roleId == null){
    //       return;
    //     }
    //     var roleids = (row.roleId+'').split(',');
    //     for(var i in roleids){
    //       this.tempRole.push(parseInt(roleids[i]));
    //     }
        
      
    // },
    async changeRole(data,row){
        row.roleId ='';
        row.roleName = '无权限';
        for(var i in data){
          if(i == 0){
            row.roleId = data[i];
            for(var j in this.roleList){
              if(this.roleList[j]['id'] == data[i]){
                row.roleName = '';
                row.roleName = this.roleList[j]['roleName'];
              }
            }
          }else{
            row.roleId +=","+data[i];
            for(var j in this.roleList){
              if(this.roleList[j]['id'] == data[i]){
                row.roleName +=","+ this.roleList[j]['roleName'];
              }
            }
           
          }
        }
         const response = await this.$http.post(`user/updateUser`, row);
          if(response.data.code === 0) {
            
          }else {
            this.$message({
              type: 'error',
              message: '修改权限失败'
            });
          }
    },
    // 列表展示
    async loadData() {
      const formData = new FormData();
      formData.append('current', this.current);
      formData.append('size', this.pagesize);
      const response = await this.$http.post(`user/page`, formData);
      if (response.data.code === 0) {
        this.loading = false;
        this.usertable = response.data.data.records;
        this.total = response.data.data.total;
      }
    },
    //查询
    async queryLook() {
      const formData = new FormData();
      formData.append('current', this.current);
      formData.append('size', this.pagesize);
      formData.append('userName', this.userName);
      formData.append('company', this.company);
      const response = await this.$http.post(`user/page`, formData);
      if (response.data.code === 0) {
        this.loading = false;
        this.usertable = response.data.data.records;
        this.total = response.data.data.total;
      }
    },
    // 新增用户
    addData(formName) {
      this.$refs[formName].validate(async (valid) => {
        if(valid) {
          this.formUserList.price = this.formUserList.price * 100;
          const response = await this.$http.post(`user`, this.formUserList);
          if(response.data.code === 0) {
            this.loadData();
            this.addUserList = false;
            this.$message({
              type: 'success',
              message: '添加成功'
            });
          }else {
            this.$message({
              type: 'error',
              message: '添加失败'
            });
          }
        }else {
          this.$message.error('请查看是否有选项未填写或填错项');
          return false;
        }
      })
    },
    // 打开修改并赋予信息
    openChange(user) {
      this.changeUser = true;
      this.formUserList.userName = user.userName;
      this.formUserList.price = user.price / 100;
      this.formUserList.id = user.id;
      this.formUserList.name = user.name;
      this.formUserList.phone = user.phone;
      this.formUserList.roleId = user.roleId;
      this.formUserList.isLock = user.isLock;
      this.formUserList.dutyParagraph = user.dutyParagraph;
      this.formUserList.company = user.company;
      this.formUserList.money = user.money;
    },
    // 修改用户
    async changeData() {
      this.formUserList.price = this.formUserList.price * 100;
      const response = await this.$http.put(`user`, this.formUserList);
      if(response.data.code === 0) {
        this.loadData();
        this.changeUser = false;
        this.$message({
          type: 'success',
          message: '修改成功'
        });
      }else {
        this.$message({
          type: 'error',
          message: '修改失败'
        });
      }
    },
     // 点击删除按钮
    Delete(id) {
      this.$confirm('是否删除该用户', '提示' ,{
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          const response = await this.$http.delete(`user/${id}`);
          if(response.data.code === 0) {
            this.loadData();
            this.$message({
              type: 'success',
              message: '删除成功'
            });
          }else {
            this.$message({
              type: 'error',
              message: '删除失败'
            });
          }
        }).catch(() => {
           this.$message({ type: 'info', message: '已取消删除'});
      });
    },
    // 打开修改密码
    openPassword(user) {
      this.changepassword = true;
      this.formUserList.userName = user.userName;
      this.formUserList.id = user.id;
    },
    //修改密码
    async resetPassword() {
      const response = await this.$http.put(`user/restPassword`, this.formUserList);
      if(response.data.code === 0) {
        this.loadData();
        this.changepassword = false;
        this.$message({
          type: 'success',
          message: '修改成功'
        });
      }else {
        this.$message({
          type: 'error',
          message: '修改失败'
        });
      }
    },
    // 打开充值弹框
    openMoney(user) {
      this.changeMoney = true;
      this.formUserList.userName = user.userName;
      this.formUserList.id = user.id;
    },
    // 充值
    async resetMoney() {
      this.formUserList.money = this.formUserList.money * 100;
      const response = await this.$http.put(`user/money`, this.formUserList);
      if(response.data.code === 0) {
        this.loadData();
        this.changeMoney = false;
        this.$message({
          type: 'success',
          message: '充值成功'
        });
      }else {
        this.$message({
          type: 'error',
          message: '充值失败'
        });
      }
    },
    // 打开修改状态弹框
    openisLock(user) {
      this.changelocks = true;
      this.formUserList.userName = user.userName;
      this.formUserList.id = user.id;
    },
    // 修改用户状态
    async changeLock() {
      const response = await this.$http.put(`user/lock`, this.formUserList);
      if(response.data.code === 0) {
        this.loadData();
        this.changelocks = false;
        this.$message({
          type: 'success',
          message: '修改成功'
        });
      }else {
        this.$message({
          type: 'error',
          message: '修改失败'
        });
      }
    },

    // 清空表单数据
    handleEditDialogClose() {
      for (var key in this.formUserList) {
        this.formUserList[key] = '';
      };
      this.current = 1;
      this.pagesize = 8;
    },
    // 分页方法
    handleSizeChange(val) {
      this.pagesize = val;
      if(this.userName !== '' || this.company !== '') {
        this.queryLook();
      }else{
        this.loadData();
      };
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.current = val;
      if(this.userName !== '' || this.company !== '') {
        this.queryLook();
      }else{
        this.loadData();
      };
      console.log(`当前页: ${val}`);
    },

  }
};
</script>

<style>
.system-user {
  border: 1px solid #d9d9d9;
  border-radius: 10px;
}
.system-user .title {
  font-size: 5px;
  margin-bottom: 20px;
}
.system-user .top {
  padding-top: 20px;
  padding-left: 20px;
}
.system-user .text {
  display: inline-block;
  color: #000;
  font-size: 16px ;
  margin-left: 1%;
}
.system-user .input-demo {
  display: inline-block;
  width: 20%;
  margin-left: 1%;
}
.system-user .block {
  font-size: 5px;
  text-align: center;
  margin-top: 25px;
  margin-bottom: 25px;
}
.system-user .el-dialog {
  width: 60%;
}
.system-user .el-dialog__header, .el-dialog__body {
  padding: 0 20px;
}
.system-user .tou {
  font-size: 20px;
  height: 30px;
  line-height: 30px;
  padding-top: 15px;
}
.system-user .line {
  margin-top: 15px;
  margin-bottom: 15px;
  width: 100%;
  height: 2px;
  background-color: #d9d9d9;
}
.system-user .xinxi {
  text-align: center;
  margin: 15px auto;
  font-size: 18px;
}
</style>
