<template>
  <div class="dashboard-container">
    <div class="search-content">
      <el-input style="width: 220px;" placeholder="请输入角色名称" v-model="searchData.roleName" clearable></el-input>     
      <el-select style="width: 220px;" clearable placeholder="请选择状态" v-model="searchData.status">
        <el-option v-for="item in statusOptions" :key="item.value" :label="item.label" :value="item.value">
        </el-option>
      </el-select>
      <div class="search-btn">        
        <div class="search" @click="searchBtn()">查询</div>
        <div class="reset" @click="reset()">重置</div>
      </div>
    </div>
    
    <div class="table-content">
      <div style="width: 100%;height: 30px;">
        <div class="btn" style="margin-right: -3px;" @click="addRoleBtn()">
          <span class="icon font_family">&#xe604;</span>
          新增
        </div>
      </div>
      <el-table ref="multipleTable" :data="tableData" tooltip-effect="dark" style="width: 100%">
        <el-table-column type="selection" width="55">
        </el-table-column>
        <el-table-column type="index" label="序号" width="80">
        </el-table-column>
        <el-table-column prop="roleName" label="角色名称">
        </el-table-column>
        <el-table-column prop="roleKey" label="权限字符">
        </el-table-column>
        <el-table-column prop="roleSort" label="显示顺序">
        </el-table-column>
        <el-table-column prop="status" label="状态" width="80">
          <template slot-scope="scope">
            <div class="startUsing" v-if="scope.row.status == 1">启用</div>
            <div class="Deactivate" v-else>停用</div>
          </template>
        </el-table-column>
        <el-table-column prop="updateTime" label="最近更新">
        </el-table-column>
        <el-table-column prop="userName" label="操作" width="200">
          <template slot-scope="scope">
            <div class="operateBtn">
              <div @click="updateRole(scope.row)">修改</div>
              <div @click="delRole(scope.row)">删除</div>
              <div @click="lookDetail(scope.row)">详情查看</div>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <detaileDialog v-if="detaildData.dialogVisible" :detaildData="detaildData"></detaileDialog>
      <addRoleDialog v-if="addRoleData.dialogVisible" :addRoleData="addRoleData" @addRoleBtn1="addRoleBtn1"></addRoleDialog>
      <updataRoleDialog v-if="updateRoleData.dialogVisible" :updateRoleData="updateRoleData"
        @updateRoleBtn="updateRoleBtn"></updataRoleDialog>
      <div class="pagination">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :page-sizes="[10, 15, 20]"
          :page-size="searchData.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { roleList, getOptionSelect, delRole, roleDetaile, addRole, editRole } from '@/api/sysManage'
import detaileDialog from './components/detaileDialog.vue'
import addRoleDialog from './components/addRoleDialog.vue'
import updataRoleDialog from './components/upDateRoleDialog.vue'

export default {
  name: 'home',
  computed: {
    ...mapGetters([
      'name',
    ])
  },
  components: {
    detaileDialog,
    addRoleDialog,
    updataRoleDialog
  },
  data() {
    return {
      tableData: [],
      total: 0,
      searchData: {
        pageNum: 1,
        pageSize: 10,
        roleName: '',
        status: '',
        roleKey: ''
      },
      detaildData: {
        dialogVisible: false,
        data: {}
      },
      addRoleData: {
        dialogVisible: false,
        data: {}
      },
      updateRoleData: {
        dialogVisible: false,
        data: {}
      },
      statusOptions: [
        {
          label: '启用',
          value: '1'
        },
        {
          label: '停用',
          value: '2'
        }
      ],
      roleOptions: [

      ]
    }
  },
  created() {
    this.getRoleList(this.searchData)
    this.getOptionSelect()
  },
  methods: {
    getRoleList(params) {
      roleList(params).then(res => {
        if (res.code == 200) {
          this.total = res.data.total
          this.tableData = res.data.records
        } else {
          this.$message({
            type: 'error',
            message: res.msg
          });
        }
      })
    },
    updateRole(scope) {
      this.updateRoleData.data = { ...scope }      
      roleDetaile(scope.roleId).then(res => {
        if (res.code == 200) {                
          this.updateRoleData.data.menuIds = res.data.menuIds          
        }
      })
      this.updateRoleData.dialogVisible = true
    },
    handleSizeChange(val) {
      this.searchData.pageSize = val
      this.getRoleList(this.searchData)
    },
    handleCurrentChange(val) {
      this.searchData.pageNum = val
      this.getRoleList(this.searchData)
    },
    searchBtn() {
      this.getRoleList(this.searchData)
    },
    reset() {
      this.searchData = {
        pageNum: 1,
        pageSize: 10,
        roleName: '',
        status: '',
        roleId: ''
      }
      this.getRoleList(this.searchData)
    },   
    updateRoleBtn(payload) {
      editRole(payload).then(res => {
        if (res.code == 200) {
          this.$message({
            type: 'success',
            message: '修改成功!'
          });
          this.updateRoleData.dialogVisible = false
          this.getRoleList(this.searchData)
        } else {
          this.$message({
            type: 'error',
            message: res.msg
          });
        }
      }
      )
    },
    //删除角色
    delRole(scope) {
      this.$confirm('确定要删除吗?', '信息提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delRole(scope.roleId).then(res => {
          if (res.code == 200) {
            this.getRoleList(this.searchData)
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
          } else {
            this.$message({
              type: 'error',
              message: res.msg
            });
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    //添加角色
    addRoleBtn1(payload) {
      addRole(payload).then(res => {
        if (res.code == 200) {
          this.$message({
            type: 'success',
            message: '添加成功！'
          });
          this.getRoleList(this.searchData)
          this.addRoleData.dialogVisible = false
        }
      })
    },
    addRoleBtn() {
      this.addRoleData.dialogVisible = true
    },
    //获取角色选择框列表
    getOptionSelect() {
      this.roleOptions = []
      getOptionSelect().then(res => {
        if (res.code == 200) {
          res.data.forEach(element => {
            this.roleOptions.push({
              value: element.roleId,
              label: element.roleName
            })
          });
        }
      })
    },
    //查看详情
    lookDetail(scope) {
      roleDetaile(scope.roleId).then(res => {
        if (res.code == 200) {
          this.detaildData.data = res.data
          this.detaildData.dialogVisible = true
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.dashboard {
  &-container {
    margin: 30px;
  }

  &-text {
    font-size: 30px;
    line-height: 46px;
  }
}

.dashboard-container {
  .search-content {
    width: 100%;
    height: 74px;
    background-color: #fff;
    display: flex;
    align-items: center;
    padding-left: 10px;

    .el-input {
      margin: 0 10px;
    }

    .el-select {
      margin: 0 10px;
    }

    .search-btn {
      display: flex;
      align-items: center;
      height: 100%;      
      margin-left: 10px;

      div {
        // margin: 0 6px;
        display: inline-block;
        width: 80px;
        height: 40px;
        border-radius: 2px;
        font-size: 14px;
        line-height: 40px;
        text-align: center;
      }

      div:hover {
        cursor: pointer;
      }

      .search {
        margin-right: 10px;
        background-color: #2766DD;
        color: #F7F8FB;
      }

      .search:hover {
        background-color: #4D85F4;
      }

      .reset {
        color: #333334;
        border: 1px solid #D7D7D7;
      }

      .reset:hover {
        color: #1F9FFF;
        border: 1px solid #1F9FFF;
      }
    }
  }

  .table-content {
    margin-top: 15px;
    width: 100%;
    background-color: #fff;
    padding: 0 20px 20px;

    .btn {
      margin: 15px 20px 15px 0;
      cursor: pointer;
      float: left;
      text-align: center;
      width: 98px;
      height: 34px;
      border-radius: 2px;
      border: 1px solid #ABC7FD;
      line-height: 34px;
      font-weight: 400;
      font-size: 14px;
      color: #2250C8;
      background-color: #E7EEFF;
    }

    .btn:hover {
      color: #FFFFFF;
      background-color: #2250C8;
      border: 1px solid #2250C8;
    }

    .btn:active {
      color: #FFFFFF;
      background-color: #194DA4;
      border: 1px solid #194DA4;
    }

    .startUsing {
        width: 54px;
        height: 24px;
        margin: 14.5px auto 0;
        line-height: 24px;
        text-align: center;
        font-size: 12px;
        color: #00974D;
        border-radius: 4px;
        background-color: #E7FAF0;
        border: 1px solid #BEFDDD;
      }

      .Deactivate {
        width: 54px;
        height: 24px;
        margin: 14.5px auto 0;
        line-height: 24px;
        text-align: center;
        font-size: 12px;
        color: #FF9933;
        border-radius: 4px;
        background-color: #FFF3E8;
        border: 1px solid #FDE6CF;
      }

    .operateBtn {
      display: flex;
      justify-content: center;
      color: #2866DD;

      div {
        font-size: 14px;
        margin: 0 5px;
        cursor: pointer;
      }
    }

    .pagination {
      margin: 10px 0;
      width: 100%;
      height: 20px;

      .el-pagination {
        float: right;
        margin: 10px 0;
      }
    }
  }

}
</style>
