<template>
  <view class="reservation-page">
    <view class="fixed-nav">
      <NavBar title="船只管理">
      </NavBar>
    </view>
    <view class="content-area">
      <view v-if="shipList.length > 0">
        <view v-for="item in shipList" :key="item.id" class="ship-cell" @click="goDetail(item)">
          <view class="ship-title">
            船号: <text class="ship-no">{{ item.ship_number }}</text>
          </view>
          <view class="ship-row">所有人: {{ item.owner_name }}</view>
          <view class="ship-row">联系电话: {{ item.phone }}</view>
          <view class="ship-row">总吨位: {{ item.total_tonnage }} 吨</view>
        </view>
      </view>
      <view v-else class="empty-tip">
        <image src="/static/images/empty.png" mode="aspectFit" class="empty-img" />
        <text class="empty-text">暂无船只信息</text>
      </view>
    </view>
    <view class="fixed-bottom">
      <button class="add-cell-btn" @click="goAdd">添加</button>
    </view>
  </view>
</template>

<script>
import NavBar from '@/components/NavBar.vue'
import { API } from '@/config/index.js'
export default {
  name: 'ShipManagePage',
  components: { NavBar },
  data() {
    return {
      shipList: []
    }
  },
  onLoad() {
    // 页面加载时的初始化
  },
  onShow() {
    // 页面渲染完成后获取数据
    this.fetchShipList();
  },
  methods: {
    async fetchShipList() {
      const token = uni.getStorageSync('token');
      if (!token) {
        uni.showToast({ title: '请先登录', icon: 'none' });
        return;
      }
      uni.showLoading({ title: '加载中...' });
      try {
        console.log(API.SHIP_INDEX)
        const res = await new Promise((resolve, reject) => {
          uni.request({
            url: API.SHIP_INDEX,
            method: 'GET',
            data: { token },
            success: resolve,
            fail: reject
          });
        });
        uni.hideLoading();
        if (res.data && res.data.errcode === 0) {
          this.shipList = res.data.data || [];
        } else {
          uni.showToast({ title: res.data.errmsg || '获取失败', icon: 'none' });
        }
      } catch (error) {
        uni.hideLoading();
        uni.showToast({ title: error.message || '网络错误', icon: 'none' });
      }
    },
    goAdd() {
      uni.navigateTo({ url: '/pages/index/ship_add' })
    },
    goDetail(item) {
      const itemStr = encodeURIComponent(JSON.stringify(item));
      uni.navigateTo({
        url: `/pages/index/ship_detail?id=${item.id}&item=${itemStr}`
      })
    }
  }
}
</script>

<style lang="scss" scoped>
@import '@/styles/common.scss';

.reservation-page {
  background: linear-gradient(180deg, #eaf3ff 0%, #f6faff 100%);
  min-height: 100vh;
  padding-bottom: 140rpx; // 为底部按钮留出空间
}
.fixed-nav {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 100;
  background: linear-gradient(180deg, #cbe6ff 0%, #f6faff 100%);
  box-shadow: 0 2px 8px rgba(0,0,0,0.04);
}
.content-area {
  padding: 220rpx 24rpx 24rpx 24rpx;
  min-height: calc(100vh - 220rpx - 140rpx); // 减去顶部导航和底部按钮的高度
}
.ship-cell {
  background: #fff;
  border-radius: 16rpx;
  box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.04);
  padding: 32rpx 28rpx;
  margin-bottom: 32rpx;
  border: 1rpx solid #e5e5e5;
}
.ship-title {
  @include font-primary;
  font-weight: $font-weight-bold;
  margin-bottom: 18rpx;
  color: $font-color-primary;
}
.ship-no {
  color: $font-color-primary;
  font-weight: $font-weight-bold;
  @include font-primary;
}
.ship-row {
  @include font-secondary;
  color: $font-color-primary;
  margin-bottom: 10rpx;
}
.fixed-bottom {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  background: #fcfcfc;
  box-shadow: 0 -2rpx 16rpx rgba(59,124,255,0.08);
  padding: 24rpx 24rpx 32rpx 24rpx;
  z-index: 999;
  display: flex;
  justify-content: center;
}
.add-cell-btn {
  min-width: 320rpx;
  height: 80rpx;
  border-radius: 40rpx;
  background: #217aff;
  color: #fff;
  @include font-primary;
  font-weight: $font-weight-medium;
  border: none;
  outline: none;
  box-shadow: 0 4rpx 16rpx rgba(33,122,255,0.08);
  transition: background 0.2s;
}
.empty-tip {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 120rpx 0;
}
.empty-img {
  width: 240rpx;
  height: 240rpx;
  margin-bottom: 24rpx;
}
.empty-text {
  @include font-secondary;
  color: $font-color-secondary;
}
</style> 