<template>
  <div class="department-management-container">
    <vab-query-form>
      <vab-query-form-top-panel>
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input v-model.trim="queryForm.deptName" clearable placeholder="请输入名称" />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">查询</el-button>
            <!--            <el-button type="text" @click="handleFold">-->
            <!--              <span v-if="fold">展开</span>-->
            <!--              <span v-else>合并</span>-->
            <!--              <vab-icon-->
            <!--                class="vab-dropdown"-->
            <!--                :class="{ 'vab-dropdown-active': fold }"-->
            <!--                icon="arrow-up-s-line" />-->
            <!--            </el-button>-->
          </el-form-item>
        </el-form>
      </vab-query-form-top-panel>
      <vab-query-form-left-panel :span="12">
        <el-button icon="el-icon-plus" type="primary" @click="handleEdit($event)">添加</el-button>
        <el-button icon="el-icon-delete" type="danger" @click="handleDelete($event)">删除</el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="12">
        <table-tool :check-list.sync="checkList" :columns="columns" />
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      default-expand-all
      :height="height"
      row-key="id"
      :tree-props="{ children: 'children' }"
      @selection-change="setSelectRows">
      <el-table-column show-overflow-tooltip type="selection" />
      <el-table-column
        v-for="(item, index) in finallyColumns"
        :key="index"
        align="center"
        :label="item.label"
        :prop="item.prop"
        show-overflow-tooltip
        :sortable="item.sortable"
        :width="item.width">
        <template #default="{ row }">
          <span v-if="item.label === '状态'">
            {{ row.status === '10' ? '正常' : '停用' }}
          </span>
          <span v-else-if="item.label === '创建时间'">
            {{ parseTime(row[item.prop]) }}
          </span>
          <span v-else>{{ row[item.prop] }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="85">
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">编辑</el-button>
          <el-button :disabled="!row.parentId" type="text" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-image class="vab-data-empty" :src="require('@/assets/empty_images/data_empty.png')" />
      </template>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNo"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange" />
    <edit ref="edit" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import deptApi from '@/api/system/dept'
  import Edit from './components/DeptEdit'
  import VabQueryFormTopPanel from '@/vab/components/VabQueryForm/components/VabQueryFormTopPanel'
  import TableTool from '@/components/table/TableTool'

  export default {
    name: 'Dept',
    components: { VabQueryFormTopPanel, Edit, TableTool },
    data() {
      return {
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        fold: false,
        height: this.$baseTableHeight(2),
        queryForm: {
          pageNum: 1,
          pageSize: 10,
          title: '',
        },
        checkList: [],
        columns: [
          {
            label: '部门名称',
            width: 'auto',
            prop: 'deptName',
            sortable: true,
            disableCheck: true,
          },
          {
            label: '显示排序',
            width: 'auto',
            prop: 'sort',
            sortable: true,
          },
          {
            label: '状态',
            width: 'auto',
            prop: 'status',
            sortable: true,
          },
          {
            label: '创建时间',
            width: 'auto',
            prop: 'createdTime',
            sortable: true,
          },
        ],
      }
    },
    computed: {
      finallyColumns() {
        return this.columns.filter((item) => this.checkList.includes(item.label))
      },
    },
    created() {
      this.fetchData()
    },
    beforeMount() {
      window.addEventListener('resize', this.handleHeight)
    },
    beforeDestroy() {
      window.removeEventListener('resize', this.handleHeight)
    },
    methods: {
      handleFold() {
        this.fold = !this.fold
        this.handleHeight()
      },
      handleHeight() {
        if (this.fold) this.height = this.$baseTableHeight(2)
        else this.height = this.$baseTableHeight(2)
      },
      setSelectRows(val) {
        this.selectRows = val
      },
      handleEdit(row) {
        if (row.id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      handleDelete(row) {
        console.log(row)
        if (row.id) {
          this.$baseConfirm('你确定要删除当前项吗', null, async () => {
            const { msg } = await deptApi.doDelete({ ids: [row.id] })
            this.$baseMessage(msg, 'success', 'vab-hey-message-success')
            await this.fetchData()
          })
        } else {
          if (this.selectRows.length > 0) {
            const ids = this.selectRows.map((item) => item.id)
            this.$baseConfirm('你确定要删除选中项吗', null, async () => {
              const { msg } = await deptApi.doDelete({ ids })
              this.$baseMessage(msg, 'success', 'vab-hey-message-success')
              await this.fetchData()
            })
          } else {
            this.$baseMessage('未选中任何行', 'error', 'vab-hey-message-error')
          }
        }
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data: data } = await deptApi.getList(this.queryForm)
        if (data) {
          this.list = this.translateDataToTree(data)
        }
        if (this.list.length === 0) {
          this.list = data
        }
        this.listLoading = false
      },
    },
  }
</script>
