<template>
  <div class="car-entry-exit-log">
    <el-card class="mb-4">
      <div class="flex justify-between items-center mb-4">
        <h2 class="text-xl font-bold text-blue-400">车辆出入营区记录</h2>
      </div>
      
      <!-- 搜索栏 -->
      <div class="search-bar mb-4 p-3 bg-blue-900/20 rounded">
        <el-row :gutter="20">
          <el-col :span="8">
            <el-input 
              v-model="searchForm.carId" 
              placeholder="车牌号" 
              prefix-icon="el-icon-search"
              size="small"
            />
          </el-col>
          <el-col :span="8">
            <el-select 
              v-model="searchForm.action" 
              placeholder="方向" 
              size="small"
            >
              <el-option 
                v-for="item in actionOptions" 
                :key="item.value" 
                :label="item.label" 
                :value="item.value"
              />
            </el-select>
          </el-col>
          <el-col :span="8" class="text-right">
            <el-button type="primary" size="small" @click="handleSearch">搜索</el-button>
            <el-button size="small" @click="handleReset">重置</el-button>
          </el-col>
        </el-row>
      </div>
      
      <!-- 出入营区记录表格 -->
      <el-table 
        :data="logData" 
        style="width: 100%"
        stripe
        :row-key="row => row.Id"
      >
        <el-table-column 
          v-for="column in tableColumns" 
          :key="column.field"
          :prop="column.field"
          :label="column.title"
          :width="column.width"
          :align="column.align"
          v-if="!column.hidden"
        >
          <template v-if="column.field === 'Action'" slot-scope="scope">
            <el-tag :type="getActionType(scope.row.Action)">{{ getActionLabel(scope.row.Action) }}</el-tag>
          </template>
          <template v-else-if="column.field === 'CreateDate'" slot-scope="scope">
            {{ formatDateTime(scope.row.CreateDate) }}
          </template>
        </el-table-column>
      </el-table>
      
      <!-- 分页 -->
      <div class="mt-4 flex justify-end">
        <el-pagination
          layout="prev, pager, next, jumper, sizes, total"
          :total="total"
          :page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </div>
    </el-card>
    
    <!-- 新增记录弹窗 -->
    <el-dialog
      title="新增出入营区记录"
      :visible.sync="dialogVisible"
      width="450px"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <el-form 
        :model="formData" 
        :rules="rules" 
        ref="formRef"
        label-width="100px"
        class="mt-4"
      >
        <el-form-item label="车牌号" prop="CarId">
          <el-input v-model="formData.CarId" placeholder="请输入车牌号" />
        </el-form-item>
        <el-form-item label="方向" prop="Action">
          <el-select v-model="formData.Action" placeholder="请选择方向">
            <el-option 
              v-for="item in actionOptions" 
              :key="item.value" 
              :label="item.label" 
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="时间" prop="CreateDate">
          <el-date-picker 
            v-model="formData.CreateDate" 
            type="datetime" 
            placeholder="请选择时间" 
            style="width: 100%"
          />
        </el-form-item>
      </el-form>
      
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSubmit">确定</el-button>
      </div>
    </el-dialog>
    
    <!-- 操作按钮 -->
    <div class="fixed bottom-4 right-4">
      <el-button type="primary" icon="el-icon-plus" circle size="medium" @click="handleAdd" />
    </div>
  </div>
</template>

<script>
export default {
  name: 'CarEntryExitLog',
  data() {
    return {
      // 表格数据
      logData: [],
      total: 0,
      pageSize: 10,
      currentPage: 1,
      
      // 搜索表单
      searchForm: {
        carId: '',
        action: ''
      },
      
      // 新增记录对话框
      dialogVisible: false,
      formData: {
        Id: '',
        CarId: '',
        Action: '',
        CreateDate: ''
      },
      
      // 验证规则
      rules: {
        CarId: [
          { required: true, message: '请输入车牌号', trigger: 'blur' }
        ],
        Action: [
          { required: true, message: '请选择方向', trigger: 'change' }
        ],
        CreateDate: [
          { required: true, message: '请选择时间', trigger: 'change' }
        ]
      },
      
      // 出入方向选项
      actionOptions: [
        { label: '出', value: '出' },
        { label: '入', value: '入' }
      ]
    }
  },
  
  computed: {
    tableColumns() {
      return [
        {field:'Id',title:'车辆记录ID',type:'int',width:110,hidden:true,require:true,align:'left'},
        {field:'CarId',title:'车牌号',type:'string',link:true,width:150,align:'left'},
        {field:'Action',title:'方向（出/入）',type:'string',bind:{ key:'出入方向',data:[]},width:150,align:'left'},
        {field:'CreateDate',title:'时间',type:'datetime',width:180,align:'left'}
      ]
    }
  },
  
  mounted() {
    this.loadData()
  },
  
  methods: {
    // 加载数据
    loadData() {
      // 模拟数据加载
      this.logData = [
        {
          Id: 1,
          CarId: '京A12345',
          Action: '出',
          CreateDate: '2024-01-20 08:30:00'
        },
        {
          Id: 2,
          CarId: '京B54321',
          Action: '出',
          CreateDate: '2024-01-20 09:15:00'
        },
        {
          Id: 3,
          CarId: '京A12345',
          Action: '入',
          CreateDate: '2024-01-20 17:45:00'
        },
        {
          Id: 4,
          CarId: '京C67890',
          Action: '入',
          CreateDate: '2024-01-20 18:20:00'
        },
        {
          Id: 5,
          CarId: '京B54321',
          Action: '入',
          CreateDate: '2024-01-20 19:00:00'
        },
        {
          Id: 6,
          CarId: '京D11223',
          Action: '入',
          CreateDate: '2024-01-21 08:00:00'
        },
        {
          Id: 7,
          CarId: '京D11223',
          Action: '出',
          CreateDate: '2024-01-21 16:30:00'
        }
      ]
      this.total = this.logData.length
    },
    
    // 搜索
    handleSearch() {
      // 模拟搜索逻辑
      console.log('搜索条件:', this.searchForm)
      this.loadData()
    },
    
    // 重置
    handleReset() {
      this.searchForm = {
        carId: '',
        action: ''
      }
      this.loadData()
    },
    
    // 新增
    handleAdd() {
      this.formData = {
        Id: '',
        CarId: '',
        Action: '',
        CreateDate: new Date()
      }
      this.dialogVisible = true
    },
    
    // 提交表单
    handleSubmit() {
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          // 模拟提交操作
          console.log('提交数据:', this.formData)
          this.dialogVisible = false
          this.loadData()
          this.$message.success('记录添加成功')
        }
      })
    },
    
    // 分页处理
    handleCurrentChange(val) {
      this.currentPage = val
      this.loadData()
    },
    
    handleSizeChange(val) {
      this.pageSize = val
      this.loadData()
    },
    
    // 格式化日期时间
    formatDateTime(dateTime) {
      if (!dateTime) return ''
      const date = new Date(dateTime)
      return date.toLocaleString('zh-CN')
    },
    
    // 获取动作标签
    getActionLabel(value) {
      const item = this.actionOptions.find(opt => opt.value === value)
      return item ? item.label : value
    },
    
    // 获取动作类型（用于标签颜色）
    getActionType(value) {
      return value === '出' ? 'warning' : 'success'
    }
  }
}
</script>

<style scoped>
.car-entry-exit-log {
  padding: 20px;
}

.search-bar {
  margin-bottom: 20px;
}

/* 表格背景透明 */
:deep(.el-table) {
  background-color: transparent !important;
}

:deep(.el-table__body-wrapper) {
  background-color: transparent !important;
}

:deep(.el-table__row) {
  background-color: transparent !important;
}

/* 表头透明 */
:deep(.el-table__header) {
  background: transparent !important;
}

:deep(.el-table__header) th {
  background: transparent !important;
  background-color: transparent !important;
  color: #68c9ff !important;
  border-color: rgba(104, 201, 255, 0.3) !important;
}

/* 确保表头在各种状态下都保持透明 */
:deep(.el-table th.is-leaf) {
  background: transparent !important;
  background-color: transparent !important;
}

/* 表格边框 */
:deep(.el-table__row td) {
  border-color: rgba(104, 201, 255, 0.2) !important;
  color: #91d5ff !important;
}

/* 表格悬停效果 */
:deep(.el-table__row:hover td) {
  background-color: rgba(104, 201, 255, 0.1) !important;
}

:deep(.el-dialog) {
  background-color: rgba(0, 58, 97, 0.4) !important;
  backdrop-filter: blur(8px);
  border: 1px solid rgba(104, 201, 255, 0.3);
}

:deep(.el-dialog__header) {
  background-color: rgba(0, 58, 97, 0.6) !important;
  border-bottom: 1px solid rgba(104, 201, 255, 0.3);
}

:deep(.el-dialog__title) {
  color: #91d5ff !important;
  font-weight: 600;
}

:deep(.el-dialog__footer) {
  background-color: rgba(0, 58, 97, 0.6) !important;
  border-top: 1px solid rgba(104, 201, 255, 0.3);
  padding-bottom: 20px;
}

:deep(.el-form-label) {
  color: #68c9ff !important;
  font-weight: 500;
}
</style>