<template>
  <div class="divBox">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item label="部门id">
        <el-input v-model="dataForm.deptId" placeholder="请输入部门id" clearable></el-input>
      </el-form-item>
      <el-form-item label="父部门id">
        <el-input v-model="dataForm.parentId" placeholder="请输入父部门id" clearable></el-input>
      </el-form-item>
      <el-form-item label="祖级列表">
        <el-input v-model="dataForm.ancestors" placeholder="请输入祖级列表" clearable></el-input>
      </el-form-item>
      <el-form-item label="部门名称">
        <el-input v-model="dataForm.deptName" placeholder="请输入部门名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="显示顺序">
        <el-input v-model="dataForm.orderNum" placeholder="请输入显示顺序" clearable></el-input>
      </el-form-item>
      <el-form-item label="负责人">
        <el-input v-model="dataForm.leader" placeholder="请输入负责人" clearable></el-input>
      </el-form-item>
      <el-form-item label="联系电话">
        <el-input v-model="dataForm.phone" placeholder="请输入联系电话" clearable></el-input>
      </el-form-item>
      <el-form-item label="邮箱">
        <el-input v-model="dataForm.email" placeholder="请输入邮箱" clearable></el-input>
      </el-form-item>
      <el-form-item label="是否作业区">
        <el-input v-model="dataForm.isWorkArea" placeholder="请输入是否作业区" clearable></el-input>
      </el-form-item>
      <el-form-item label="部门状态（0正常 1停用）">
        <el-input v-model="dataForm.status" placeholder="请输入部门状态（0正常 1停用）" clearable></el-input>
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="dataForm.remark" placeholder="请输入备注" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button @click="resetForm()">重置</el-button>
        <el-button v-hasPermi="['autogencode:sysdept:save']" type="primary" @click="addOrUpdateHandle()">新增数据</el-button>
        <el-button v-hasPermi="['autogencode:sysdept:delete']" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="deptId"
        header-align="center"
        align="center"
        label="部门id">
      </el-table-column>
      <el-table-column
        prop="parentId"
        header-align="center"
        align="center"
        label="父部门id">
      </el-table-column>
      <el-table-column
        prop="ancestors"
        header-align="center"
        align="center"
        label="祖级列表">
      </el-table-column>
      <el-table-column
        prop="deptName"
        header-align="center"
        align="center"
        label="部门名称">
      </el-table-column>
      <el-table-column
        prop="orderNum"
        header-align="center"
        align="center"
        label="显示顺序">
      </el-table-column>
      <el-table-column
        prop="leader"
        header-align="center"
        align="center"
        label="负责人">
      </el-table-column>
      <el-table-column
        prop="phone"
        header-align="center"
        align="center"
        label="联系电话">
      </el-table-column>
      <el-table-column
        prop="email"
        header-align="center"
        align="center"
        label="邮箱">
      </el-table-column>
      <el-table-column
        prop="isWorkArea"
        header-align="center"
        align="center"
        label="是否作业区">
      </el-table-column>
      <el-table-column
        prop="status"
        header-align="center"
        align="center"
        label="部门状态（0正常 1停用）">
      </el-table-column>
      <el-table-column
        prop="createDept"
        header-align="center"
        align="center"
        label="创建部门">
      </el-table-column>
      <el-table-column
        prop="createBy"
        header-align="center"
        align="center"
        label="创建人">
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        label="创建时间">
      </el-table-column>
      <el-table-column
        prop="updateBy"
        header-align="center"
        align="center"
        label="修改人">
      </el-table-column>
      <el-table-column
        prop="updateTime"
        header-align="center"
        align="center"
        label="修改时间">
      </el-table-column>
      <el-table-column
        prop="remark"
        header-align="center"
        align="center"
        label="备注">
      </el-table-column>
      <el-table-column
        prop="delFlag"
        header-align="center"
        align="center"
        label="删除标志（0代表存在 2代表删除）">
      </el-table-column>
      <el-table-column
        prop="tenantId"
        header-align="center"
        align="center"
        label="">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.deptId)">{{ '修改' }}</el-button>
          <el-button v-hasPermi="['autogencode:sysdept:delete']" type="text" size="small" @click="deleteHandle(scope.row.deptId)" style="color: #f56c6c;">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 表单弹窗, 新增数据和修改数据 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './sysdept-add-and-update'
  import * as api from '@/api/sysdept.js'
  export default {
    data () {
      return {
        dataForm: {
          deptId: '',
          parentId: '',
          ancestors: '',
          deptName: '',
          orderNum: '',
          leader: '',
          phone: '',
          email: '',
          isWorkArea: '',
          status: '',
          remark: '',
        },

        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    components: {
      AddOrUpdate
    },
    activated () {
      this.getDataList()
    },
    mounted() {
      // 确保页面加载时自动查询
      this.getDataList()
    },
    methods: {
      // 重置表单
      resetForm() {
        this.dataForm = {
          deptId: '',
          parentId: '',
          ancestors: '',
          deptName: '',
          orderNum: '',
          leader: '',
          phone: '',
          email: '',
          isWorkArea: '',
          status: '',
          remark: '',
        }
        this.getDataList()
      },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        // 根据后端SysDeptController和PageParamRequest的期望格式构建参数
        const params = {
          page: this.pageIndex,
          limit: this.pageSize
        }

        // 将查询条件添加到参数中
        if (this.dataForm.deptId) params.deptId = this.dataForm.deptId;
        if (this.dataForm.parentId) params.parentId = this.dataForm.parentId;
        if (this.dataForm.ancestors) params.ancestors = this.dataForm.ancestors;
        if (this.dataForm.deptName) params.deptName = this.dataForm.deptName;
        if (this.dataForm.orderNum) params.orderNum = this.dataForm.orderNum;
        if (this.dataForm.leader) params.leader = this.dataForm.leader;
        if (this.dataForm.phone) params.phone = this.dataForm.phone;
        if (this.dataForm.email) params.email = this.dataForm.email;
        if (this.dataForm.isWorkArea) params.isWorkArea = this.dataForm.isWorkArea;
        if (this.dataForm.status) params.status = this.dataForm.status;
        if (this.dataForm.remark) params.remark = this.dataForm.remark;

        api.sysdeptListApi(params).then(res => {
          this.dataListLoading = false
          this.dataList = res.list || [];
          this.totalPage = res.total;
        }).catch(e => {
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除处理（支持单个删除和批量删除）
      deleteHandle (id) {
        let ids = []
        let idText = ''

        // 如果传入了单个id参数，则为单个删除
        if (id) {
          ids = [id]
          idText = id
        } else {
          // 否则为批量删除
          ids = this.dataListSelections.map(item => {
            return item.deptId
          })
          idText = ids.join(',')
        }

        this.$confirm(`确定进行删除操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          // 调用批量删除API
          api.sysdeptDeleteApi(ids).then(res => {
            this.$message.success('删除成功')
            this.getDataList()
          })
        })
      },
    }
  }
</script>
