<template>
  <div class="resource-auth-container">
    <vab-card>
      <vab-query-form>
        <vab-query-form-left-panel>
          <el-form :inline="true" :model="queryForm" @submit.prevent>
            <el-form-item label="学生信息">
              <el-input v-model="queryForm.keyword" placeholder="姓名/学号" clearable prefix-icon="user-line" />
            </el-form-item>
            <el-form-item label="资源类型">
              <el-select v-model="queryForm.type" placeholder="全部" clearable style="width: 120px">
                <el-option label="门禁" value="access" />
                <el-option label="用电" value="electric" />
                <el-option label="网络" value="network" />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="handleQuery">
                <vab-icon icon="search-line" class="icon-btn" /> 查询
              </el-button>
            </el-form-item>
          </el-form>
        </vab-query-form-left-panel>
        <vab-query-form-right-panel>
          <el-button type="primary" @click="handleBatchAuth">
            <vab-icon icon="shield-check-line" class="icon-btn" /> 批量授权
          </el-button>
        </vab-query-form-right-panel>
      </vab-query-form>

      <el-table v-loading="loading" :data="tableData" border stripe>
        <el-table-column prop="studentName" label="姓名" width="100" />
        <el-table-column prop="studentId" label="学号" width="120" />
        <el-table-column prop="college" label="学院" width="120" />
        <el-table-column prop="room" label="当前宿舍" width="120" />
        <el-table-column prop="accessStatus" label="门禁权限" width="120">
          <template #default="{ row }">
            <el-switch 
              v-model="row.accessStatus" 
              active-value="active" 
              inactive-value="inactive"
              active-text="启用"
              inactive-text="停用"
              inline-prompt
              @change="handleAuthChange(row, 'access')"
            />
          </template>
        </el-table-column>
        <el-table-column prop="electricStatus" label="智能控电" width="120">
          <template #default="{ row }">
            <el-switch 
              v-model="row.electricStatus" 
              active-value="active" 
              inactive-value="inactive"
              active-text="启用"
              inactive-text="停用"
              inline-prompt
              @change="handleAuthChange(row, 'electric')"
            />
          </template>
        </el-table-column>
        <el-table-column prop="networkStatus" label="校园网" width="120">
          <template #default="{ row }">
            <el-switch 
              v-model="row.networkStatus" 
              active-value="active" 
              inactive-value="inactive"
              active-text="启用"
              inactive-text="停用"
              inline-prompt
              @change="handleAuthChange(row, 'network')"
            />
          </template>
        </el-table-column>
        <el-table-column prop="expireTime" label="授权有效期" min-width="180" />
        <el-table-column label="操作" width="120" fixed="right">
          <template #default="{ row }">
            <el-button type="primary" text size="small" @click="handleEdit(row)">
              <vab-icon icon="edit-time-line" class="icon-sm" /> 延期
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <vab-pagination
        :current-page="queryForm.pageNo"
        :page-size="queryForm.pageSize"
        :total="total"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
      />
    </vab-card>

    <batch-auth-dialog ref="batchAuthDialogRef" @success="handleQuery" />
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import BatchAuthDialog from './components/BatchAuthDialog.vue'

defineOptions({
  name: 'ResourceAuth',
})

const loading = ref(false)
const total = ref(100)
const tableData = ref([])
const batchAuthDialogRef = ref()

const queryForm = reactive({
  keyword: '',
  type: '',
  pageNo: 1,
  pageSize: 10,
})

const generateMockData = () => {
  const list = []
  for (let i = 0; i < 10; i++) {
    list.push({
      id: i + 1,
      studentName: `学生${i + 1}`,
      studentId: `319010${i + 1}`,
      college: 'ZJUI',
      room: `北区1-${Math.floor(Math.random() * 6 + 1)}0${i + 1}`,
      accessStatus: 'active',
      electricStatus: 'active',
      networkStatus: Math.random() > 0.8 ? 'inactive' : 'active',
      expireTime: '2026-07-01 00:00:00'
    })
  }
  return list
}

const handleQuery = () => {
  loading.value = true
  setTimeout(() => {
    tableData.value = generateMockData()
    loading.value = false
  }, 500)
}

const handleAuthChange = (row, type) => {
  const status = row[`${type}Status`]
  const typeName = { access: '门禁', electric: '控电', network: '网络' }[type]
  ElMessage.success(`${row.studentName} 的${typeName}权限已${status === 'active' ? '启用' : '停用'}`)
}

const handleBatchAuth = () => {
  batchAuthDialogRef.value.open()
}

const handleEdit = (row) => {
  ElMessage.success('有效期已延长至下一学年')
}

const handleCurrentChange = (val) => {
  queryForm.pageNo = val
  handleQuery()
}
const handleSizeChange = (val) => {
  queryForm.pageSize = val
  handleQuery()
}

handleQuery()
</script>

<style lang="scss" scoped>
.resource-auth-container {
  .text-xs { font-size: 12px; }
  .text-gray { color: #909399; }
  
  .icon-btn {
    margin-right: 6px;
    font-size: 16px;
    vertical-align: middle;
  }
  
  .icon-sm {
    margin-right: 4px;
    font-size: 14px;
    vertical-align: middle;
  }
}
</style>