<template>
  <div>
    <treePage title="菜单列表" @on-result-change="_refresh">
      <template slot="tree">
        <relResourceTree ref="treeModal" @on-result-change="_treeData" />
      </template>
      <template slot="cont">
        <el-row>
          <!--查询-->
          <el-col :span="24">
            <Form
              v-show="searchOpen"
              onsubmit="return false"
              :label-width="70"
              inline
            >
              <label class="label-sign" />
              <Form-item class="search-item" label="按钮名称:">
                <el-input v-model="formObj.name" placeholder="请输入按钮名称" clearable @keyup.enter.native="_formSearch" />
              </Form-item>
              <Form-item class="search-item margin-left--20" label="状态:">
                <el-select v-model="formObj.status" placeholder="请选择状态" style="width: 100%" clearable>
                  <el-option
                    v-for="item in statusList"
                    :key="item.value"
                    :label="item.name"
                    :value="item.value"
                  />
                </el-select>
              </Form-item>
              <Form-item class="search-item margin-left--20" label="编码:">
                <el-input v-model="formObj.code" placeholder="请输入编码" clearable @keyup.enter.native="_formSearch" />
              </Form-item>
              <Form-item class="search-item margin-left--20" label="备注:">
                <el-input v-model="formObj.remark" 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" @on-result-change="_btnClick" />
          </el-col>
          <!--table-->
          <el-col :span="24">
            <element-table
              ref="pageTable"
              :page-columns="pageColumns"
              :table-height="tableHeight"
              :get-page="getPage"
              :icon-msg="iconMsg"
              :opt-col-width="70"
              @on-result-change="_tableResultChange"
            >
              <el-table-column
                v-for="item in pageColumns"
                :key="item.key"
                show-overflow-tooltip
                sortable
                :prop="item.key"
                :label="item.title"
                :width="item.width"
                :min-width="200"
                :fixed="item.fixed?item.fixed:undefined"
              >
                <template slot-scope="scope">
                  <a v-if="item.detail" @click.stop="_detailModal(scope.row.id)">
                    {{ scope.row[item.key] }}
                  </a>
                  <div v-else-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-if="item.key==='uri'">
                    <div v-if="scope.row[item.key]==='reportBtn'" class="blue-color">
                      报表
                    </div>
                    <div v-else-if="scope.row[item.key]==='formBtn'" class="green-color">
                      表单
                    </div>
                    <div v-else>
                      普通
                    </div>
                  </div>
                  <div v-else>
                    {{ scope.row[item.key] }}
                  </div>
                </template>
              </el-table-column>
            </element-table>
          </el-col>
        </el-row>
      </template>
    </treePage>
    <!--添加编辑-->
    <btnResourceEdit ref="editModal" @on-result-change="_search" />
    <!--详情-->
    <btnResourceDetail ref="detailModal" />
  </div>
</template>
<script>
import { sysResource } from '../../../api'
import treePage from '../../../components/base/treePage'
import relResourceTree from '../resource/tree/relResourceTree'
import btnResourceEdit from './btnResourceEdit'
import btnResourceDetail from './btnResourceDetail'
export default {
  components: {
    treePage,
    relResourceTree,
    btnResourceEdit,
    btnResourceDetail
  },
  data() {
    return {
      searchOpen: false,
      btn: [{ type: 'success', id: 'sys-btn-resource-add', name: '添加' }],
      iconMsg: [
        { type: 'pt-edit', id: 'sys-btn-resource-edit', name: '编辑' },
        { type: 'pt-copy', id: '', name: '复制' }
      ],
      pageColumns: [
        {
          title: '按钮名称',
          key: 'name',
          width: 180,
          detail: true,
          fixed: true
        },
        { title: '编码', key: 'code' },
        { title: '所属菜单', key: 'parentName', width: 180 },
        { title: '状态', key: 'status', width: 100 },
        { title: '类型', key: 'uri', width: 100 },
        { title: '排序', key: 'sort', width: 100 },
        { title: '备注', key: 'remark' }
      ],
      formObj: {
        code: '',
        name: '',
        status: '',
        remark: '',
        pid: ''
      },
      getPage: {},
      statusList: [
        {
          value: '0',
          name: '禁用'
        },
        {
          value: '1',
          name: '正常'
        }
      ],
      treeObj: {}
    }
  },
  computed: {
    tableHeight: function() {
      if (this.searchOpen) {
        return this.$tableHeight('search')
      } else {
        return this.$tableHeight('noSearch')
      }
    }
  },
  mounted() {
    this._search()
  },
  methods: {
    // 刷新
    _refresh() {
      this.formObj.pid = ''
      this.treeObj = {}
      this._formSearch()
    },
    _search() {
      this._formSearch()
      this.$refs.treeModal._open()
    },
    _formSearch() {
      this.$refs.pageTable._pageChange(1)
    },
    _page: async function() {
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      const result = await sysResource.btnPage(
        this.$serializeForm(this.formObj)
      )
      if (result) {
        this.getPage = result
        this.$refs.pageTable._initTable()
      }
    },
    _btnClick(msg, data) {
      switch (msg) {
        case '添加':
          this.$refs.editModal._open(this.treeObj, '')
          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
      }
    },
    _iconClick(res, data) {
      switch (res) {
        case '编辑':
          this._editModal(data)
          break
        case '复制':
          this._copy(data)
          break
      }
    },
    _editModal: async function(data) {
      const result = await sysResource.getById(data.id)
      this.$refs.editModal._open('', result)
    },
    _copy: async function(data) {
      const result = await sysResource.getById(data.id)
      this.$refs.editModal._openCopy('', result)
    },
    // 详情
    _detailModal(id) {
      this.$refs.detailModal._open(id)
    },
    _treeData(data) {
      this.formObj.pid = data.id
      this.treeObj = data
      this._formSearch()
    }
  }
}
</script>
