<template>
  <div class="app-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>安全人员管理</span>
      </div>
      <!-- 搜索 -->
      <div class="head-container">
        <el-form :inline="true" :model="query" class="demo-form-inline">
          <el-form-item label="请输入人员名称">
            <el-input v-model="query.name" placeholder="请输入人员名称" />
          </el-form-item>
          <el-form-item label="入职时间">
            <el-date-picker
              v-model="query.dateRange"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleQuery">查询</el-button>
            <el-button @click="handleReset">重置</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="success" @click="handleAdd">新增安全人员</el-button>
          </el-form-item>
        </el-form>
      </div>

      <!-- 表格 -->
      <el-table :data="tableData" :loading="loading" style="width: 100%">
        <el-table-column type="index" label="序号" width="60" />
        <el-table-column prop="name" label="姓名" />
        <el-table-column prop="sex" label="性别" />
        <el-table-column prop="phone" label="联系方式" />
        <el-table-column prop="deptId" label="部门" />
        <el-table-column prop="job" label="职务" />
        <el-table-column prop="workType" label="工种" />
        <el-table-column prop="entryDate" label="入职时间" />
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="text" @click="handleEdit(scope.row)">修改</el-button>
            <el-button type="text" style="color: red" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        :current-page="pagination.currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pagination.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pagination.total"
        style="margin-top: 20px; text-align: right"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
  </div>
</template>

<script>
import { getSafePeopleInfo } from '@/api/aqrf/safePersonnel'

export default {
  name: 'SafePersonnel',
  data() {
    return {
      query: {
        name: '',
        dateRange: []
      },
      tableData: [],
      pagination: {
        currentPage: 1,
        pageSize: 10,
        total: 0
      },
      loading: false
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    async fetchData() {
      this.loading = true
      try {
        const params = {
          page: this.pagination.currentPage - 1, // 接口页码从0开始
          size: this.pagination.pageSize,
          name: this.query.name || '',
          startDate: this.query.dateRange && this.query.dateRange[0] ? this.formatDate(this.query.dateRange[0]) : '',
          endDate: this.query.dateRange && this.query.dateRange[1] ? this.formatDate(this.query.dateRange[1]) : ''
        }

        const response = await getSafePeopleInfo(params)

        if (response && response.data) {
          this.tableData = response.body.list || []
          this.pagination.total = response.body.total || 0
        } else {
          this.tableData = []
          this.pagination.total = 0
        }
      } catch (error) {
        console.error('获取安全人员信息失败:', error)
        this.$message.error('获取安全人员信息失败')
        this.tableData = []
        this.pagination.total = 0
      } finally {
        this.loading = false
      }
    },

    // 格式化日期为 YYYY-MM-DD 格式
    formatDate(date) {
      if (!date) return ''
      const d = new Date(date)
      const year = d.getFullYear()
      const month = String(d.getMonth() + 1).padStart(2, '0')
      const day = String(d.getDate()).padStart(2, '0')
      return `${year}-${month}-${day}`
    },
    handleQuery() {
      this.pagination.currentPage = 1
      this.fetchData()
    },
    handleReset() {
      this.query = {
        name: '',
        dateRange: []
      }
      this.handleQuery()
    },
    handleAdd() {
      this.$router.push('/aqrf/add-personnel')
    },
    handleEdit(row) {
      console.log('Edit:', row)
    },
    handleDelete(row) {
      this.$confirm('此操作将永久删除该条目, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          console.log('Delete:', row)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    handleSizeChange(val) {
      this.pagination.pageSize = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.pagination.currentPage = val
      this.fetchData()
    }
  }
}
</script>

<style scoped>
.app-container {
  padding: 20px;
}
.head-container {
  margin-bottom: 20px;
}
.clearfix:before,
.clearfix:after {
  display: table;
  content: '';
}
.clearfix:after {
  clear: both;
}
</style>
