<template>
  <BasicLayout>
    <scroll-view scroll-y class="push-list-scroll-view">
      <view class="push-list-container">
        <!-- 页面标题 -->
        <view class="page-header">
          <text class="page-title">推送清单</text>
          <text class="teacher-count">共 {{ teacherList.length }} 名教师</text>
        </view>

        <!-- 教师信息卡片列表 -->
        <view class="teacher-cards">
          <view 
            v-for="(teacher, index) in teacherList" 
            :key="teacher.id || teacher.jsId || index"
            class="teacher-card"
          >
            <view class="teacher-main-info">
              <text class="teacher-name">{{ teacher.jsxm || '未知姓名' }}</text>
              <text class="teacher-position">{{ teacher.dzzw || '' }} {{ teacher.qtzw || '' }}</text>
            </view>
            <view class="teacher-status">
              <text class="status-label">状态：</text>
              <text class="status-value" :class="getStatusClass(teacher.qdStatus)">
                {{ getStatusText(teacher.qdStatus) }}
              </text>
            </view>
          </view>
        </view>

        <!-- 空状态 -->
        <view v-if="teacherList.length === 0" class="empty-state">
          <uni-icons type="info" size="60" color="#ccc"></uni-icons>
          <text class="empty-text">暂无教师信息</text>
        </view>
      </view>
    </scroll-view>

    <!-- Bottom slot -->
    <template #bottom>
      <view class="bottom-actions">
        <button class="action-btn cancel-btn" @click="handleCancel">
          取消
        </button>
        <button class="action-btn confirm-btn" @click="handleConfirmPush" :disabled="isPushing">
          {{ isPushing ? '推送中...' : '确认推送' }}
        </button>
      </view>
    </template>
  </BasicLayout>
</template>

<script lang="ts" setup>
import { ref } from "vue";
import { onLoad } from "@dcloudio/uni-app";
import { qdzxFindByQdParamsApi, xxtsSaveByQdzxParamsApi } from "@/api/base/server";

interface TeacherInfo {
  id: string;
  jsId: string;
  jsxm: string;
  dzzw: string;
  qtzw: string;
  qdStatus: string;
  qdId: string;
  qdmc: string;
}

const qdId = ref<string>('');
const teacherList = ref<TeacherInfo[]>([]);
const isPushing = ref(false);

onLoad((options) => {
  if (options && options.qdId && options.qdId !== 'null' && options.qdId !== 'undefined') {
    qdId.value = options.qdId;
    loadTeacherList();
  } else {
    uni.showToast({ title: "缺少签到ID参数", icon: "error" });
    setTimeout(() => {
      uni.navigateBack();
    }, 1500);
  }
});

const loadTeacherList = async () => {
  try {
    uni.showLoading({ title: "加载教师信息中..." });
    
    console.log('开始调用API，qdId:', qdId.value);
    const result = await qdzxFindByQdParamsApi({ qdId: qdId.value });
    
    uni.hideLoading();
    
    console.log('API返回结果:', result);
    console.log('result.rows:', result?.rows);
    console.log('result.rows类型:', typeof result?.rows);
    console.log('result.rows是否为数组:', Array.isArray(result?.rows));
    console.log('result.rows长度:', result?.rows?.length);
    
    // 直接处理Page对象，不依赖resultCode
    if (result && result.rows && Array.isArray(result.rows)) {
      teacherList.value = result.rows;
      console.log('成功从rows字段获取到教师数据:', result.rows.length, '条');
      console.log('教师数据详情:', result.rows);
    } else {
      console.warn('未找到有效的教师数据，API返回结构:', result);
      console.warn('result的所有字段:', Object.keys(result || {}));
      teacherList.value = [];
      uni.showToast({ title: "未找到教师数据", icon: "none" });
    }
    
    console.log('最终teacherList.value:', teacherList.value);
    console.log('teacherList.value长度:', teacherList.value.length);
    
  } catch (error) {
    uni.hideLoading();
    console.error('加载教师列表失败:', error);
    uni.showToast({ title: "加载教师列表失败", icon: "none" });
  }
};

const getStatusClass = (status: string) => {
  switch (status) {
    case '1':
      return 'status-signed';
    case '0':
    default:
      return 'status-unsigned';
  }
};

const getStatusText = (status: string) => {
  switch (status) {
    case '1':
      return '已推送';
    case '0':
    default:
      return '未推送';
  }
};

const handleCancel = () => {
  uni.navigateBack();
};

const handleConfirmPush = async () => {
  isPushing.value = true;
  
  try {
    const result = await xxtsSaveByQdzxParamsApi({ qdId: qdId.value });
    
    if (result.resultCode === 1) {
      uni.showToast({ title: '推送成功', icon: 'success' });
      setTimeout(() => {
        // 跳转回签到发布列表页面
        uni.redirectTo({
          url: '/pages/view/routine/qd/index'
        });
      }, 1500);
    } else {
      uni.showToast({ title: '推送失败', icon: 'none' });
    }
  } catch (error) {
    console.error('推送失败:', error);
    uni.showToast({ title: '推送失败，请重试', icon: 'none' });
  } finally {
    isPushing.value = false;
  }
};
</script>

<style lang="scss" scoped>
.push-list-scroll-view {
  height: calc(100vh - 120px);
}

.push-list-container {
  padding: 15px;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  padding: 15px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 12px;
  color: white;
}

.page-title {
  font-size: 18px;
  font-weight: 600;
}

.teacher-count {
  font-size: 14px;
  opacity: 0.9;
}

.teacher-cards {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.teacher-card {
  background: white;
  border-radius: 12px;
  padding: 15px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.teacher-main-info {
  margin-bottom: 10px;
}

.teacher-name {
  display: block;
  font-size: 16px;
  font-weight: 600;
  color: #333;
  margin-bottom: 4px;
}

.teacher-position {
  font-size: 14px;
  color: #666;
}

.teacher-status {
  display: flex;
  align-items: center;
  gap: 8px;
}

.status-label {
  font-size: 14px;
  color: #666;
}

.status-value {
  font-size: 14px;
  font-weight: 500;
  padding: 2px 8px;
  border-radius: 8px;
}

.status-signed {
  background: #d4edda;
  color: #155724;
}

.status-unsigned {
  background: #f8d7da;
  color: #721c24;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 20px;
  color: #999;
}

.empty-text {
  margin-top: 15px;
  font-size: 16px;
}

.bottom-actions {
  display: flex;
  gap: 15px;
  padding: 15px;
  background: white;
  border-top: 1px solid #f0f0f0;
}

.action-btn {
  flex: 1;
  padding: 12px;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 500;
  border: none;
}

.cancel-btn {
  background: #f5f5f5;
  color: #666;
}

.confirm-btn {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
}

.confirm-btn:disabled {
  opacity: 0.6;
}
</style> 