<template>
  <div class="shift-manage-container">
    <el-row :gutter="20">
      <!-- 左侧排班日历 -->
      <el-col :span="16">
        <vab-card title="排班管理">
          <template #header>
            <div class="header-actions">
              <span>排班表 (2026年1月)</span>
              <el-button type="primary" size="small" @click="handleEditShift">排班设置</el-button>
            </div>
          </template>
          <el-calendar v-model="currentDate">
            <template #date-cell="{ data }">
              <div class="calendar-cell" :class="{ 'is-selected': data.isSelected }">
                <div class="day">{{ data.day.split('-').slice(1).join('-') }}</div>
                <div class="shifts">
                  <div v-for="(shift, index) in getShifts(data.day)" :key="index" class="shift-tag" :class="shift.type">
                    {{ shift.name }} ({{ shift.staff }})
                  </div>
                </div>
              </div>
            </template>
          </el-calendar>
        </vab-card>
      </el-col>

      <!-- 右侧交接记录 -->
      <el-col :span="8">
        <vab-card title="交接班日志" class="log-card">
          <div class="log-list">
            <el-timeline>
              <el-timeline-item
                v-for="(log, index) in logList"
                :key="index"
                :timestamp="log.time"
                :type="log.status === 'normal' ? 'primary' : 'warning'"
                placement="top"
              >
                <el-card shadow="hover" class="log-item">
                  <div class="log-header">
                    <span class="font-bold">{{ log.shiftName }}</span>
                    <el-tag size="small" effect="plain">{{ log.handoverStaff }} → {{ log.takeoverStaff }}</el-tag>
                  </div>
                  <div class="log-content mt-10">
                    <div class="item">
                      <span class="label">物品核对:</span>
                      <span :class="log.itemCheck ? 'text-success' : 'text-danger'">{{ log.itemCheck ? '无误' : '异常' }}</span>
                    </div>
                    <div class="item">
                      <span class="label">遗留事项:</span>
                      <span>{{ log.todo || '无' }}</span>
                    </div>
                    <div class="item" v-if="log.remark">
                      <span class="label">备注:</span>
                      <span class="text-gray">{{ log.remark }}</span>
                    </div>
                  </div>
                </el-card>
              </el-timeline-item>
            </el-timeline>
          </div>
        </vab-card>
      </el-col>
    </el-row>

    <!-- 排班设置弹窗 -->
    <shift-setting-dialog ref="shiftSettingRef" />
  </div>
</template>

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

defineOptions({
  name: 'ShiftManage',
})

const currentDate = ref(new Date())
const shiftSettingRef = ref()

// --- Shift Logic ---
const getShifts = (day) => {
  // Mock shifts based on date
  const date = new Date(day).getDate()
  if (date % 3 === 0) {
    return [
      { type: 'morning', name: '早班', staff: '张三' },
      { type: 'evening', name: '晚班', staff: '李四' }
    ]
  } else if (date % 3 === 1) {
    return [
      { type: 'morning', name: '早班', staff: '王五' },
      { type: 'evening', name: '晚班', staff: '赵六' }
    ]
  } else {
    return [
      { type: 'morning', name: '早班', staff: '钱七' },
      { type: 'evening', name: '晚班', staff: '孙八' }
    ]
  }
}

const handleEditShift = () => {
  shiftSettingRef.value.open()
}

// --- Log Logic ---
const logList = ref([
  {
    time: '2026-01-24 08:00',
    shiftName: '早班交接',
    handoverStaff: '赵六',
    takeoverStaff: '钱七',
    status: 'normal',
    itemCheck: true,
    todo: '无',
    remark: '夜间巡查正常'
  },
  {
    time: '2026-01-23 20:00',
    shiftName: '晚班交接',
    handoverStaff: '王五',
    takeoverStaff: '赵六',
    status: 'warning',
    itemCheck: true,
    todo: '302室报修门锁待跟进',
    remark: '对讲机电量不足，已充电'
  },
  {
    time: '2026-01-23 08:00',
    shiftName: '早班交接',
    handoverStaff: '李四',
    takeoverStaff: '王五',
    status: 'normal',
    itemCheck: true,
    todo: '无',
    remark: ''
  }
])
</script>

<style lang="scss" scoped>
.shift-manage-container {
  .header-actions {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
  }

  .calendar-cell {
    height: 100%;
    .day { text-align: right; margin-bottom: 5px; }
    .shifts {
      display: flex;
      flex-direction: column;
      gap: 2px;
      .shift-tag {
        font-size: 12px;
        padding: 2px 4px;
        border-radius: 2px;
        color: #fff;
        &.morning { background-color: #67c23a; }
        &.evening { background-color: #409eff; }
        &.night { background-color: #909399; }
      }
    }
  }

  .log-card {
    height: 100%;
    overflow-y: auto;
    
    .log-list {
      padding-right: 10px;
    }

    .log-item {
      .log-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 10px;
      }
      .log-content {
        font-size: 13px;
        .item {
          margin-bottom: 5px;
          .label { color: #909399; margin-right: 5px; }
        }
      }
    }
  }

  .mt-10 { margin-top: 10px; }
  .font-bold { font-weight: bold; }
  .text-success { color: var(--el-color-success); }
  .text-danger { color: var(--el-color-danger); }
  .text-gray { color: #909399; }
}
</style>
