<template>
  <view class="chapter-container">
    <view class="header">
      <view class="nav-bar" style="position: relative; height: 44px;">
        <view class="back">
          <BackArrow :size="56" color="black" @back="goBack" />
        </view>
        <view class="title">兽王进化：从被小萝莉...</view>
        <view class="right-icons"></view>
      </view>
      <view class="tabs">
        <view class="tab" :class="{ active: activeTab === 'draft' }" @click="switchTab('draft')">草稿箱</view>
        <view class="tab" :class="{ active: activeTab === 'published' }" @click="switchTab('published')">已发布</view>
      </view>
    </view>
    
    <view class="chapter-list">
      <view class="chapter-item" v-for="(chapter, index) in chapters" :key="index" @click="editChapter(chapter)">
        <view class="chapter-info">
          <text class="chapter-title">章节名</text>
          <text class="chapter-number">第{{index + 1}}章</text>
        </view>
        <uv-icon name="arrow-right" color="#999" size="28"></uv-icon>
      </view>
    </view>
    
    <view class="bottom-actions">
      <button class="btn-settings" @click="handleSettings">设置作品</button>
      <button class="btn-new" @click="addNewChapter">新建章节</button>
    </view>
  </view>
</template>

<script>
import BackArrow from './components/BackArrow.vue';

export default {
  components: {
    BackArrow,
  },
  data() {
    return {
      activeTab: 'draft',
      chapters: [] // 真实章节数据
    }
  },
  onLoad(options) {
    // 根据tab参数决定初始tab
    if (options && options.tab === 'published') {
      this.activeTab = 'published';
    } else {
      this.activeTab = 'draft';
    }
    this.loadChapters();
    // 判断是否需要弹窗
    if (options && options.fromSave === '1') {
      uni.showToast({
        title: '保存成功',
        icon: 'success'
      });
    }
    if (options && options.fromPublish === '1') {
      uni.showToast({
        title: '发布成功',
        icon: 'success'
      });
    }
  },
  methods: {
    loadChapters() {
      if (this.activeTab === 'published') {
        this.chapters = uni.getStorageSync('publishedChapters') || [];
      } else {
        this.chapters = uni.getStorageSync('chapters') || [];
      }
    },
    goBack() {
      uni.navigateBack()
    },
    switchTab(tab) {
      this.activeTab = tab;
      this.loadChapters();
    },
    addNewChapter() {
      uni.navigateTo({
        url: '/pages_order/author/editor'
      })
    },
    editChapter(chapter) {
      uni.navigateTo({
        url: '/pages_order/author/editor?id=' + chapter.id
      })
    },
    handleSettings() {
      uni.navigateTo({
        url: '/pages_order/novel/createNovel?type=edit'
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.chapter-container {
  min-height: 100vh;
  background-color: #fff;
  padding-bottom: 70px;
  
  .header {
    background: #fff;
    .nav-bar {
      position: relative;
      height: 44px;
      .back {
        position: absolute;
        left: 0;
        top: 0;
        bottom: 0;
        width: 48px;
        display: flex;
        align-items: center;
        justify-content: center;
        z-index: 2;
      }
      .right-icons {
        position: absolute;
        right: 0;
        top: 0;
        bottom: 0;
        width: 48px;
        display: flex;
        align-items: center;
        justify-content: center;
        z-index: 2;
      }
      .title {
        position: absolute;
        left: 0;
        right: 0;
        top: 0;
        bottom: 0;
        margin: auto;
        height: 44px;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 16px;
        font-weight: 500;
        color: #222;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        text-align: center;
        z-index: 1;
        pointer-events: none;
      }
    }
    .tabs {
      display: flex;
      margin-top: 4px;
      margin-left: 120rpx;
      justify-content: space-between;
    
      .tab {
        font-size: 15px;
        color: #888;
        margin-right: 120rpx;
        position: relative;
        &.active {
          color: #223a7a;
          font-weight: 600;
          &::after {
            content: '';
            display: block;
            width: 24px;
            height: 3px;
            background: #223a7a;
            border-radius: 2px;
            position: absolute;
            left: 50%;
            transform: translateX(-50%);
            bottom: -6px;
          }
        }
      }
    }
  }
  
  .chapter-list {
    padding: 20rpx 40rpx;
    background-color: rgb(255, 254, 254);
    margin: 50rpx 40rpx;
    border-radius: 3%;
    .chapter-item {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 15px 0;
      border-bottom: 1px solid #eee;
      
      .chapter-info {
        .chapter-title {
          font-size: 14px;
          color: #999;
          margin-bottom: 5px;
          display: block;
        }
        
        .chapter-number {
          font-size: 16px;
          color: #333;
          display: block;
        }
      }
      
      .icon-arrow {
        color: #999;
        font-size: 16px;
      }
    }
  }
  
  .bottom-actions {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    display: flex;
    padding: 10px 15px;
    background: #fff;
    box-shadow: 0 -2px 6px rgba(0,0,0,0.05);
    
    button {
      flex: 1;
      height: 40px;
      border-radius: 20px;
      font-size: 16px;
      margin: 0 5px;
      
      &.btn-settings {
        background: #fff;
        border: 1px solid #ddd;
        color: #333;
      }
      
      &.btn-new {
        background: #2b4acb;
        color: #fff;
        border: none;
      }
    }
  }
}
</style> 