<template>
  <a-card :bordered="false">
    <a-tabs default-active-key="1" @change="tabsCallback">
      <a-tab-pane key="1" tab="单位部门信息">
        <div class="table-operator">
          <a-button @click="$refs.orgForm.add(1)" icon="plus" type="primary" v-if="hasPerm('sys:org:add')">新增机构</a-button>
        </div>

        <s-table
          ref="table1"
          :columns="columns"
          :data="loadData"
          :rowKey="(record) => record.id"
          :showPagination="false"
          :defaultExpandedRowKeys="expandedRowKeys"
        >

          <span slot="action" slot-scope="text, record">
            <a v-if="hasPerm('sys:org:edit')" @click="$refs.orgForm.edit(record)">编辑</a>
            <a-divider type="vertical" v-if="hasPerm('sys:org:edit') & hasPerm('sys:org:delete')"/>
            <a-popconfirm v-if="hasPerm('sys:org:delete')" placement="topRight" title="确认删除？" @confirm="() => sysOrgDelete(record)">
              <a>删除</a>
            </a-popconfirm>
          </span>
        </s-table>
        <org-form ref="orgForm" @ok="handleOk" />
      </a-tab-pane>
      <a-tab-pane key="2" tab="项目工程信息" >
        <div class="table-operator">
          <a-button @click="$refs.orgForm.add(2)" icon="plus" type="primary" v-if="hasPerm('sys:org:add')">新增项目</a-button>
        </div>

        <s-table
          ref="table2"
          :columns="columns"
          :data="loadData"
          :rowKey="(record) => record.id"
          :showPagination="false"
          :defaultExpandedRowKeys="expandedRowKeys"
        >

          <span slot="action" slot-scope="text, record">
            <a v-if="hasPerm('sys:org:edit')" @click="$refs.orgForm.edit(record)">编辑</a>
            <a-divider type="vertical" v-if="hasPerm('sys:org:edit') & hasPerm('sys:org:delete')"/>
            <a-popconfirm v-if="hasPerm('sys:org:delete')" placement="topRight" title="确认删除？" @confirm="() => sysOrgDelete(record)">
              <a>删除</a>
            </a-popconfirm>
          </span>
        </s-table>
        <org-form ref="orgForm" @ok="handleOk" />
      </a-tab-pane>
    </a-tabs>
  </a-card>
</template>
<script>
  import { STable } from '@/components'
  import { orgList, orgDel } from '@/api/org/org'
  import OrgForm from './OrgForm'
  import { listToTree } from '@/utils/util'
  const rootParentId = 0

  export default {
    components: {
      STable,
      OrgForm
    },
    data () {
      return {
        // 查询参数
        queryParam: { orgType: 1 },
        expandedRowKeys: [],
        // 表头
        columns: [
          {
            title: '机构名称',
            dataIndex: 'name'
          },
          {
            title: '唯一编码',
            dataIndex: 'code'
          },
          {
            title: '排序',
            dataIndex: 'sort'
          },
          {
            title: '备注',
            dataIndex: 'remark'
          }
        ],
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return orgList(Object.assign(parameter, this.queryParam)).then((res) => {
            const orgList = listToTree(res.data, [], rootParentId)
            // 默认展开目录级
            orgList.forEach(item => {
              this.expandedRowKeys.push(item.id)
            })
            return orgList
          })
        }
      }
    },
    created () {
      if (this.hasPerm('sys:org:edit') || this.hasPerm('sys:org:del')) {
        this.columns.push({
          title: '操作',
          width: '150px',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' }
        })
      }
    },
    methods: {
      tabsCallback (key) {
        if (key === '1') {
          this.queryParam.orgType = 1
          this.$refs.table1.refresh(true)
        }
        if (key === '2') {
          this.queryParam.orgType = 2
          this.$refs.table2.refresh(true)
        }
      },
      /**
       * 删除
       */
      sysOrgDelete (record) {
        orgDel({ id: record.id, deleteReason: '' }).then((res) => {
          if (res.code === 200) {
            this.$message.success('删除成功')
            this.$refs.table.refresh(true)
          } else {
            this.$message.error('删除失败：' + res.msg)
          }
        })
      },
      handleOk () {
        this.$refs.table.refresh()
      }
    }
  }
</script>
<style lang="less">
  .table-operator {
    margin-bottom: 18px;
  }
  button {
    margin-right: 8px;
  }
</style>
