<template>
  <div>
    <treePage title="角色列表" @on-result-change="_refresh">
      <template slot="tree">
        <roleTree ref="roleModal" @on-result-change="_roleData" />
      </template>
      <template slot="cont">
        <el-row>
          <!--查询-->
          <el-col :span="24">
            <Form
              v-show="searchOpen"
              inline
              onsubmit="return false"
              :label-width="70"
            >
              <Form-item class="search-item" label="角色名称:">
                <el-input v-model="formObj.name" placeholder="请输入角色名称" clearable @keyup.enter.native="_formSearch" />
              </Form-item>
              <Form-item class="search-btn">
                <el-button type="primary" @click="_formSearch">
                  搜索
                </el-button>
              </Form-item>
            </Form>
          </el-col>
          <!--操作-->
          <el-col :span="24">
            <btn-list
              :msg="btn"
              :open="searchOpen"
              :show-search-btn="true"
              class="contHide"
              @on-result-change="_btnClick"
            />
          </el-col>
          <!-- 表格 -->
          <el-col :span="24">
            <element-table
              ref="pageTable"
              :table-height="tableHeight"
              :get-page="getPage"
              :icon-msg="iconMsg"
              :opt-col-width="100"
              @on-result-change="_tableResultChange"
            >
              <el-table-column
                v-for="item in pageColumns"
                :key="item.id"
                sortable
                show-overflow-tooltip
                :prop="item.key"
                :label="item.title"
                :min-width="item.width"
                :fixed="item.fixed?item.fixed:undefined"
              >
                <template slot-scope="scope">
                  <div v-if="item.key==='status'">
                    <div v-if="scope.row[item.key]===0" class="red-color">
                      禁用
                    </div>
                    <div v-else class="blue-color">
                      正常
                    </div>
                  </div>
                  <div v-else>
                    {{ scope.row[item.key] }}
                  </div>
                </template>
              </el-table-column>
            </element-table>
          </el-col>
        </el-row>
      </template>
    </treePage>
    <!-- 添加、编辑 -->
    <roleEdit ref="editModal" @on-result-change="_search" />
    <!-- 分配菜单权限 -->
    <assignMenuTree ref="menuTreeModal" />
  </div>
</template>
<script>
import { sysRole } from '../../../api'
import treePage from '../../../components/base/treePage'
import roleEdit from './roleEdit'
import assignMenuTree from './tree/assignMenuTree'
import roleTree from './tree/roleTree'

export default {
  components: {
    roleEdit,
    assignMenuTree,
    roleTree,
    treePage
  },
  data() {
    return {
      searchOpen: false,
      btn: [{ type: 'success', id: 'sys-role-add', name: '添加' }],
      iconMsg: [
        { type: 'pt-edit', id: 'sys-role-edit', name: '编辑' },
        {
          type: 'pt-resource',
          id: 'sys-role-menu-permission',
          name: '分配菜单权限'
        }
      ],
      formObj: {
        id: '', // 左侧树返回的id
        name: ''
      },
      getPage: {},
      pageColumns: [
        { title: '名称', key: 'name', width: 140, detail: true },
        { title: '上级角色', key: 'parentName', width: 140 },
        { title: '状态', key: 'status', width: 100 },
        { title: '备注', key: 'remark', width: 220 }
      ]
    }
  },
  computed: {
    tableHeight: function() {
      if (this.searchOpen) {
        return this.$tableHeight('search')
      } else {
        return this.$tableHeight('noSearch')
      }
    }
  },
  mounted() {
    this._search()
  },
  methods: {
    _search() {
      this._page()
      this.$refs.roleModal._open()
    },
    _iconClick(res, data) {
      switch (res) {
        case '编辑':
          this._editModal(data.id)
          break
        case '分配菜单权限':
          this._menuTreeModal(data.id)
          break
      }
    },
    _refresh() {
      this.formObj.id = ''
      this._page()
    },
    _page: async function() {
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      const result = await sysRole.page(this.$serializeForm(this.formObj))
      if (result) {
        this.getPage = result
        this.$refs.pageTable._initTable()
      }
    },
    _formSearch() {
      this.$refs.pageTable._pageChange(1)
    },
    _detailModal: async function(id) {
      const result = await sysRole.getById(id)
      this.$refs.detailModal._open(result)
    },
    _editModal: async function(id) {
      const result = await sysRole.getById(id)
      this.$refs.editModal._open(result)
    },
    _menuTreeModal(id) {
      this.$refs.menuTreeModal._open(id)
    },
    _roleData(id) {
      this.formObj.id = id
      this._formSearch()
    },
    _btnClick(msg) {
      // 列表返回的数据
      switch (msg) {
        case '添加':
          this.$refs.editModal._open()
          break
        case 'search':
          this.searchOpen = !this.searchOpen
          break
      }
    },
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'page':
          this._page()
          break
        case 'iconClick':
          this._iconClick(data.name, data.rowData)
          break
      }
    }
  }
}
</script>
