<template>
  <div>
    <treePage title="组织列表" @on-result-change="_refresh">
      <template slot="tree">
        <relOrgZtree ref="orgTreeModal" @on-result-change="_orgData" />
      </template>
      <template slot="cont">
        <el-row>
          <!--查询-->
          <el-col :span="24">
            <Form
              v-show="searchOpen"
              id="search-form"
              inline
              onsubmit="return false"
              :label-width="40"
            >
              <label class="label-sign" />
              <Form-item class="search-item" label="名称">
                <el-input v-model="formObj.name" placeholder="请输入名称" @keyup.enter.native="_formSearch" clearable/>
              </Form-item>
              <Form-item class="search-item" label="状态">
                <el-select v-model="formObj.status" placeholder="请选择状态" style="width: 100%" clearable>
                  <el-option
                    v-for="item in statusList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select>
              </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="70"
              @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">
                  <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] === undefined" />
                    <div v-else-if="scope.row[item.key].value!=='NORMAL'" style="color: red">
                      禁用
                    </div>
                    <div v-else style="color: #00a0e9">
                      正常
                    </div>
                  </div>
                  <div v-else>
                    {{ scope.row[item.key] }}
                  </div>
                </template>
              </el-table-column>
            </element-table>
          </el-col>
        </el-row>
      </template>
    </treePage>
    <!-- 添加、编辑 -->
    <sysOrgEdit ref="editModal" @on-result-change="_search" />
    <!--导入-->
    <downloadTemplateImport ref="importModal" @on-result-change="_search" />
    <!-- 查看详情 -->
    <orgDetail ref="detailModal" />
  </div>
</template>
<script>
import { sysOrg } from '../../../api'
import treePage from '../../../components/base/treePage'
import downloadTemplateImport from '../../../components/import/downloadTemplateImport'
import sysOrgEdit from './sysOrgEdit'
import orgDetail from './orgDetail'
import relOrgZtree from './relOrgZtree'

export default {
  components: {
    treePage,
    sysOrgEdit,
    orgDetail,
    relOrgZtree,
    downloadTemplateImport
  },
  data() {
    return {
      searchOpen: false,
      btn: [
        { type: 'success', id: 'sys-org-add', name: '添加' },
        { id: 'sys-org-add', name: '导入' }
      ],
      iconMsg: [{ type: 'pt-edit', id: 'sys-org-edit', name: '编辑' }],
      statusList: [
        {
          value: 'DISABLED',
          label: '禁用'
        },
        {
          value: 'NORMAL',
          label: '正常'
        }
      ],
      pageColumns: [
        { title: '名称', key: 'name', width: 150, detail: true },
        { title: '上级组织', key: 'parentName', width: 150 },
        { title: '状态', key: 'status', width: 100 },
        { title: '排序', key: 'sort', width: 80 },
        { title: '备注', key: 'remark', width: 100 }
      ],
      getPage: {},
      formObj: {
        name: '',
        status: '',
        id: ''
      }
    }
  },
  computed: {
    tableHeight: function() {
      if (this.searchOpen) {
        return this.$tableHeight('search')
      } else {
        return this.$tableHeight('noSearch')
      }
    }
  },
  mounted() {
    this._search()
  },
  methods: {
    _search() {
      this._orgTree()
      this._page()
    },
    _iconClick(res, data) {
      switch (res) {
        case '编辑':
          this._editModal(data.id)
          break
      }
    },
    _refresh() {
      this.formObj.id = ''
      this._page()
    },
    _page: async function() {
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      const result = await sysOrg.page(this.$serializeForm(this.formObj))
      if (result) {
        this.getPage = result
        this.$refs.pageTable._initTable()
      }
    },
    _formSearch() {
      this.$refs.pageTable._pageChange(1)
    },
    _detailModal(id) {
      this.$refs.detailModal._open(id)
    },
    _editModal: async function(id) {
      const result = await sysOrg.getById(id)
      this.$refs.editModal._open(result)
    },
    _orgTree() {
      this.$refs.orgTreeModal._open()
    },
    _orgData(id) {
      this.formObj.id = id
      this._formSearch()
    },
    _btnClick(msg) {
      switch (msg) {
        case '添加':
          this.$refs.editModal._open()
          break
        case '导入':
          this._import()
          break
        case 'search':
          this.searchOpen = !this.searchOpen
          break
      }
    },
    _import() {
      const data = {
        importUrl: '/base/v1/org/import',
        downloadUrl: '/base/v1/excel/template/SysOrg',
        title: '导入组织信息'
      }
      this.$refs.importModal._open(data)
    },
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'page':
          this._page()
          break
        case 'iconClick':
          this._iconClick(data.name, data.rowData)
          break
      }
    }
  }
}
</script>
