<template>
  <el-row>
    <el-row>
      <el-form ref="searchForm" :model="searchForm" label-width="80px">
        <search-white-board :show-more="true" @search="list">
          <div slot="base">
            <el-row style="line-height: 40px; text-align: right;">
              <el-col :span="2" :offset="1"><label class="search-label">部门名称</label></el-col>
              <el-col :span="5">
                <el-input v-model.trim="searchForm.name" placeholder="请输入部门名称" clearable @keyup.enter.native="list" />
              </el-col>
            </el-row>
          </div>

          <div slot="more">
            <el-row style="line-height: 40px; text-align: right;">
              <el-col :span="2"><label class="search-label">是否删除</label></el-col>
              <el-col :span="5" style="text-align: left;">
                <el-switch v-model="searchForm.isDel" active-color="#13ce66" inactive-color="#ff4949" @change="list" />
              </el-col>
            </el-row>
          </div>
        </search-white-board>
      </el-form>
    </el-row>

    <el-row>
      <white-board>
        <el-row>
          <el-col :span="2" :offset="22" style="text-align: right;">
            <el-button icon="el-icon-plus" type="success" plain @click="handleAdd">新增</el-button>
          </el-col>
        </el-row>

        <el-row style="padding: 10px 0px;">
          <el-table :data="tableData" style="width: 100%;" row-key="id" lazy :load="load" :tree-props="{children: 'children', hasChildren: 'hasChildren'}" highlight-current-row stripe>
            <el-table-column prop="id" label="标识" />
            <el-table-column prop="parentId" label="父级" show-overflow-tooltip />
            <el-table-column prop="name" label="部门名称" show-overflow-tooltip />
            <el-table-column prop="leader" label="负责人" show-overflow-tooltip />
            <el-table-column prop="phone" label="电话" show-overflow-tooltip />
            <el-table-column prop="email" label="邮箱" show-overflow-tooltip />
            <el-table-column label="更新时间" width="135">
              <template slot-scope="scope">
                <el-row>{{ scope.row.updateByName }}</el-row>
                <el-row>{{ scope.row.updateAt }}</el-row>
              </template>
            </el-table-column>
            <el-table-column label="操作" width="150" fixed="right">
              <template slot-scope="scope">
                <el-button type="primary" plain @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                <el-button v-if="!scope.row.isDel" type="danger" plain @click="handleIsDel(scope.$index, scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-row>

        <el-row>
          <el-pagination :current-page="searchForm.page" :page-sizes="[10, 20, 50, 100]" :page-size="50" layout="total, sizes, prev, pager, next, jumper" :total="total" small @size-change="changeSizeChange" @current-change="changeCurrentChange" />
        </el-row>
      </white-board>
    </el-row>

    <edit-dept-dialog :visible="dialogVisible" :dialog-title="dialogTitle" :is-top="isTop" :form="form" @cancelDialog="cancelDialog" @refresh="list" />
  </el-row>
</template>

<script>
import deptApi from '@/api/platform/deptApi'
import EditDeptDialog from '@/views/platform/system/dept/editDeptDialog'

export default {
  name: 'DeptIndex',
  components: {
    'edit-dept-dialog': EditDeptDialog
  },
  data() {
    return {
      searchForm: {
        name: '',
        isDel: false,
        page: 1,
        size: 50
      },
      total: 0,
      tableData: [],
      dialogTitle: '',
      isTop: '1',
      dialogVisible: false,
      form: {
        id: '',
        parentId: '',
        name: '',
        leader: '',
        phone: '',
        email: '',
        sort: 0
      }
    }
  },
  mounted() {
    this.list()
  },
  methods: {
    cancelDialog(visible) {
      this.dialogVisible = visible
    },
    handleAdd() {
      this.form = {
        id: '',
        parentId: '',
        name: '',
        leader: '',
        phone: '',
        email: '',
        sort: 0
      }
      this.isTop = '1'
      this.dialogTitle = '新增'
      this.dialogVisible = true
    },
    handleEdit(index, row) {
      this.form = JSON.parse(JSON.stringify(row))
      if (this.form.parentId === null) {
        this.isTop = '1'
      } else {
        this.isTop = '0'
      }
      this.dialogTitle = '编辑'
      this.dialogVisible = true
    },
    handleIsDel(index, row) {
      this.$confirm('确定删除吗？如果存在下级节点则一并删除，此操作不能撤销！', '警告', {
        confirmButtonText: '删除',
        cancelButtonText: '取消',
        type: 'info'
      }).then(() => {
        deptApi.handleIsDel(row.id).then(data => {
          this.list()
        })
      })
    },
    list() {
      this.tableData = []
      deptApi.page(this.searchForm).then(data => {
        this.total = data.total
        this.tableData = data.data
      })
    },
    load(row, treeNode, resolve) {
      const d = {
        parentId: row.id,
        isDel: false
      }
      deptApi.list(d).then(data => {
        resolve(data)
      })
    },
    changeSizeChange(val) {
      this.searchForm.size = val
      this.list()
    },
    changeCurrentChange(val) {
      this.searchForm.page = val
      this.list()
    }
  }
}
</script>

<style scoped>
</style>
