<template>
  <div class="main-container">
    <el-tabs v-model="activeName" type="border-card">
      <el-tab-pane label="角色列表" name="1">
        <el-table stripe ref="table" :height="tableHeight" v-loading="tableLoading" size="medium"
                  :data="authGroupList" border row-key="id">
          <el-table-column :index="indexMethod" label="序号" type="index" width="80" align="center"/>
          <el-table-column show-overflow-tooltip prop="name" label="角色名称" min-width="100" align="center"/>
          <el-table-column min-width="190" show-overflow-tooltip prop="authData" label="数据权限" align="center">
            <template slot-scope="{row}">
              <el-tag v-if="row.authData === 1" type="" size="small">查看本人负责河湖</el-tag>
              <el-tag v-if="row.authData === 2" type="success" size="small">查看本人组织及下属组织内河湖</el-tag>
              <el-tag v-if="row.authData === 3" type="warning" size="small">查看指定组织下河湖</el-tag>
            </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip prop="remark" label="角色描述" align="center"/>
          <el-table-column show-overflow-tooltip prop="sort" label="排序" align="center" min-width="100"/>
          <el-table-column show-overflow-tooltip prop="status" label="状态" align="center" width="120">
            <template slot-scope="{row}">
              {{ row.status === 1 ? '正常' : '禁用' }}
              <el-switch v-if="btnRule.userauthgroup_setStatus" v-model="row.status" :active-value="1"
                         :inactive-value="2" size="small"
                         @change="setStatus(row)"/>
            </template>
          </el-table-column>
          <el-table-column label="操作"
                           width="120" align="center" fixed="right">
            <template slot-scope="scoped">
              <el-row>
                <el-tooltip v-if="btnRule.userauthgroup_info" class="item" effect="dark" content="详情"
                            placement="bottom">
                  <el-button @click="authGroupInfo(scoped.row,1)" type="primary" size="mini" icon="el-icon-tickets"/>
                </el-tooltip>
                <el-tooltip v-if="btnRule.userauthgroup_edit&&btnRule.userauthgroup_info" class="item" effect="dark" content="编辑"
                            placement="bottom">
                  <el-button type="primary" size="mini" icon="el-icon-edit" @click="authGroupInfo(scoped.row)"/>
                </el-tooltip>
                <el-tooltip v-if="btnRule.userauthgroup_drop" class="item" effect="dark" content="删除"
                            placement="bottom">
                  <el-button :loading="delBtnLoading" type="danger" size="mini" class="el-icon-delete"
                             @click="authGroupDel(scoped.row)"/>
                </el-tooltip>
              </el-row>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <div class="page-current">
          <el-pagination
            background
            :current-page="currentPage"
            :page-size="pageSize"
            prev-text="上一页"
            next-text="下一页"
            layout="total, prev, pager, next, jumper"
            style="display: inline-block;"
            :total="count"
            @current-change="handlePageChange"/>
        </div>
      </el-tab-pane>
      <el-tab-pane v-if="btnRule.userauthgroup_add" label="添加角色" name="2">
        <editForm :cityList="filterCityList" ref="editForm" :infoForm="infoForm" @closeWin="closeWin"
                  style="width: 50%" v-if="activeName==2" @submit="submit"></editForm>
      </el-tab-pane>
    </el-tabs>

    <!-- 编辑表单 -->
    <el-dialog :close-on-press-escape="false" :close-on-click-modal="false" center :visible.sync="addDialogVisible"
               :title="dialogTitle+'角色'">
      <editForm :cityList="filterCityList" ref="editForm" :infoForm="infoForm" style="width: 100%" @submit="submit"
                @closeWin="closeWin"></editForm>
    </el-dialog>

  </div>
</template>

<script>
  import authGroupApi from '@/api/permission/authgroup'
  import adminApi from '@/api/permission/admin'
  import editForm from './edit'
  import {mapGetters} from "vuex";

  export default {
    name: "authGroup",
    data() {

      return {
        activeName: '1',
        tableLoading: false,
        // 当前页
        currentPage: 1,
        // 总条数
        count: 0,
        // 每页条数
        pageSize: parseInt((document.body.clientHeight - 240 - 43) / 44.5),
        // 显示编辑角色弹框
        addBtnLoading: false,
        addDialogVisible: false,
        delBtnLoading: false,
        // 搜索
        status: [{
          value: 1,
          label: '正常'
        }, {
          value: 2,
          label: '禁用'
        }],
        authGroupList: [],
        infoForm: {
          name: '',
          remark: '',
          sort: 1,
          authData:'',
          driverArea:[]
        },
        filterCityList:[],
        cityList:[],
        loading: false,
        tableHeight: document.body.clientHeight - 240,
        dialogTitle: "添加",
      }
    },
    components: {editForm},
    computed: {
      ...mapGetters(["btnRule"]),
    },
    created() {
    },
    mounted() {
      // 开启加载动画
      this.tableLoading = true
      // 获取数据列表
      this.getList()
      this.getBaseData()
    },
    watch: {
      activeName() {
        this.initRuleForm()
      }
    },
    methods: {
      getBaseData(call) {
        adminApi.getBaseData().then(res => {
          const {area_list, level_list} = res.data
          this.cityList=area_list?area_list:[]
          let filterCityList = this.cityList.filter(item => item.pid == 3201)
          filterCityList.map(item => {
            let childList = this.filterCity(item.id)
            if(childList.length>0){
              item.children =childList
            }
          })
          this.filterCityList = filterCityList
        }).catch(e => {
          this.filterCityList =[]
        })
      },
      filterCity(pid) {
        let list = this.cityList.filter(item => item.pid == pid)
        list.map(item => {
          let childList = this.filterCity(item.id)
          if(childList.length>0){
            item.children = childList
          }
        })
        return list
      },
      indexMethod(index) {
        return (index + 1) + ((this.currentPage - 1) * this.pageSize);
      },
      // 获取数据列表
      getList() {
        let searchParam ={}
        searchParam.page = this.currentPage
        searchParam.pageSize = this.pageSize
        authGroupApi.roleList(searchParam).then(res => {
          this.authGroupList = res.data.records
          this.count = res.data.total
          this.tableLoading = false
        }).catch(e => {
          this.authGroupList = []
          this.tableLoading = false
        })
      },
      // 获取编辑数据
      authGroupInfo(data,type) {
        // 初始化表单
        this.initRuleForm()
        // 显示弹窗
        // 设置标题
        if (type) {
          this.dialogTitle = "查看";
        } else {
          this.dialogTitle = "编辑";
        }
        this.tableLoading=true
        authGroupApi.authGroupInfo(data.id).then(res => {
          let data=res.data
          if(type){
            data.disabled=true
          }
          if(data.driverArea){
            data.driverArea=data.driverArea.split(',')
          }else{
            data.driverArea=[]
          }
          this.infoForm = Object.assign({},data)
          console.log(this.infoForm)
          this.tableLoading=false
          this.addDialogVisible = true
        }).catch(e => {
          this.tableLoading=false
        })
      },
      // 初始化表单
      initRuleForm() {
        this.infoForm = {
          name: '',
          remark: '',
          sort: 1,
          authData:'',
          driverArea:[]
        }
      },
      // 删除记录
      authGroupDel(data) {
        this.$confirm('您确定要删除角色吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.delBtnLoading = true
          authGroupApi.authGroupDel({id: data.id}).then(res => {
            this.$message({
              type: 'success',
              message: '删除成功！'
            })
            this.delBtnLoading = false
            this.tableLoading = true
            this.getList()
          }).catch(e => {
            this.delBtnLoading = false
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      },
      // 设置状态
      setStatus(data) {
        var content = ''
        if (data.status === 1) {
          content = '确定要启用吗？'
        } else {
          content = '确定要禁用吗？'
        }
        this.$confirm(content, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let params = {
            status: data.status,
            id: data.id
          }
          this.tableLoading = true
          authGroupApi.setStatus(params).then(res => {
            this.$message({
              type: 'success',
              message: '操作成功！'
            })
            this.getList()
          }).catch(e => {
            let authGroupList = JSON.parse(JSON.stringify(this.authGroupList))
            for (let i = 0; i < authGroupList.length; i++) {
              if (authGroupList[i].id == data.id) {
                authGroupList[i].status = data.status == 1 ? 2 : 1
              }
            }
            this.authGroupList = Object.assign([], authGroupList)
            this.tableLoading = false
          })
        }).catch(() => {
          let authGroupList = JSON.parse(JSON.stringify(this.authGroupList))
          for (let i = 0; i < authGroupList.length; i++) {
            if (authGroupList[i].id == data.id) {
              authGroupList[i].status = data.status == 1 ? 2 : 1
            }
          }
          this.authGroupList = Object.assign([], authGroupList)
          this.$message({
            type: 'info',
            message: '已取消该操作！'
          })
        })
      },
      handlePageChange(val) {
        this.currentPage = val
        this.tableLoading = true
        this.$refs.table.bodyWrapper.scrollTop = 0;
        this.getList()
      },
      // 提交表单后逻辑处理
      submit(type) {
        if (type == 1) {
          this.activeName='1'
          this.currentPage = 1
          this.tableLoading = true
          this.$refs.table.bodyWrapper.scrollTop = 0;
          this.getList()
        } else {
          this.tableLoading=true
          this.$refs.table.bodyWrapper.scrollTop = 0;
          this.getList()
          this.addDialogVisible = false
        }
      },
      // 关闭窗体
      closeWin() {
        console.log("关闭")
        // 调用初始化表单方法
        this.initRuleForm()
        // 设置弹窗不可见
        this.addDialogVisible = false
      }
    }
  }
</script>

<style lang="less">
  .main-container {
    width: 100%;

    .page-current {
      text-align: center;
      padding: 15px 5px;
      padding-bottom: 0px;

      .sure-page-btn {
        display: inline-block;
        height: 28px;
        line-height: 0;
        width: 46px;
        padding: 0;
        vertical-align: top;
      }
    }
  }

</style>
