<template>
  <div class="app-container">
    <!-- 搜索区域 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="nickname">
        <el-input clearable v-model="queryParams.nickname" placeholder="根据姓名查询" />
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-select v-model="queryParams.sex" placeholder="请选择性别">
          <el-option label="男" value="0" />
          <el-option label="女" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item label="学历" prop="highestDegree">
        <el-input v-model="queryParams.highestDegree" placeholder="请输入学历" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" 
                  @click="handleExport" v-hasPermi="['system:onboarding:export']">
          导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" />
    </el-row>

    <!-- 人员表格 -->
    <el-table v-loading="loading" :data="peopleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" />
      <el-table-column label="姓名" align="center" prop="nickName" />
      <el-table-column label="性别" align="center" prop="sex">
        <template slot-scope="scope">
          <span>{{ scope.row.sex === '0' ? '男' : '女' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="部门" align="center" prop="dept.deptName" />
      <el-table-column label="入职日期" align="center" prop="joiningDate" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.joiningDate || '未完成审批流程' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-add" 
                    @click="handleFileManagement(scope.row)">
            文件管理
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-detail" 
                    @click="handleDetail(scope.row)">
            详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination v-show="total>0" :total="total" 
               :page.sync="queryParams.pageNum" 
               :limit.sync="queryParams.pageSize" 
               @pagination="getList" />

    <!-- 文件管理抽屉 -->
    <el-drawer :title="`${currentUser.nickName} - 文件管理`" 
              :visible.sync="fileDrawerVisible" 
              size="800px" 
              direction="rtl" 
              append-to-body>
      <UserFileManager 
        :user-id="currentUser.userId" 
        :read-only="!hasEditPermission"
        v-if="fileDrawerVisible" />
    </el-drawer>

    <!-- 人员详情对话框 -->
    <!-- <user-detail-dialog :visible.sync="detailDialogVisible" :user-id="currentUser.userId" /> -->
  </div>
</template>

<script>
import { listUser } from "@/api/system/user";
import UserFileManager from "@/components/FileList";
// import UserDetailDialog from "@/components/UserDetailDialog";

export default {
  name: "Onboarding",
  components: {
    UserFileManager,
    // UserDetailDialog
  },
  data() {
    return {
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nickname: '',
        sex: '',
        highestDegree: '',
        userId: undefined,
      },
      // 页面状态
      loading: true,
      showSearch: true,
      fileDrawerVisible: false,
      detailDialogVisible: false,
      // 数据
      peopleList: [],
      total: 0,
      currentUser: {},
      // 权限状态
      hasEditPermission: false
    };
  },
  created() {
    this.getList();
    this.checkPermissions();
  },
  methods: {
    /** 获取人员列表 */
    getList() {
      this.loading = true;
      listUser(this.queryParams).then(response => {
        this.peopleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 权限检查 */
    checkPermissions() {
      // 根据实际权限系统实现，示例使用固定值
      this.hasEditPermission = this.$store.getters.roles.includes('HR');
    },

    /** 打开文件管理 */
    handleFileManagement(row) {
      this.currentUser = row;
      this.fileDrawerVisible = true;
      this.hasEditPermission = true;
    },

    /** 打开详情 */
    handleDetail(row) {
      this.currentUser = row;
      this.fileDrawerVisible = true;
      this.hasEditPermission = false;
    },

    /** 搜索 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /** 重置查询 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 处理选中 */
    handleSelectionChange(selection) {
      this.selectedUsers = selection;
    },

    /** 导出数据 */
    handleExport() {
      this.download('system/onboarding/export', {
        ...this.queryParams
      }, `onboarding_${new Date().getTime()}.xlsx`);
    }
  }
};
</script>

<style scoped>
/* 可根据需要添加自定义样式 */
.drawer-footer {
  margin-top: 20px;
  text-align: right;
}
</style>