<template>
  <view class="page">
    <!-- 顶部标题栏 -->
    <view class="header">
      <text class="title">耗材种类管理</text>
    </view>

    <!-- 搜索栏 -->
    <view class="search-bar">
      <view class="search-box">
        <text class="search-icon">🔍</text>
        <input class="search-input" v-model="searchKeyword" placeholder="搜索物料名称或规格" @confirm="loadList(true)" />
      </view>
      <button class="btn-add" @tap="showAddModal">+ 新增</button>
    </view>

    <!-- 列表 -->
    <scroll-view scroll-y class="list-container" @scrolltolower="loadMore">
      <view v-if="list.length === 0 && !loading" class="empty">
        <text class="empty-text">暂无耗材种类</text>
      </view>
      
      <view v-for="item in list" :key="item.id" class="catalog-card">
        <view class="card-main">
          <view class="catalog-info">
            <text class="catalog-name">{{ item.name }}</text>
            <view class="catalog-meta">
              <text v-if="item.spec" class="meta-item">规格: {{ item.spec }}</text>
              <text v-if="item.unit" class="meta-item">单位: {{ item.unit }}</text>
            </view>
          </view>
          <view class="catalog-status" :class="item.isActive ? 'active' : 'inactive'">
            {{ item.isActive ? '启用' : '停用' }}
          </view>
        </view>
        <view class="card-footer">
          <text class="time">{{ formatTime(item.updatedAt) }}</text>
          <view class="actions">
            <button class="btn-edit" @tap="handleEdit(item)">编辑</button>
            <button v-if="item.isActive" class="btn-disable" @tap="handleDisable(item.id)">停用</button>
            <button v-else class="btn-enable" @tap="handleEnable(item.id)">启用</button>
          </view>
        </view>
      </view>

      <view v-if="loading" class="loading">
        <text>加载中...</text>
      </view>

      <view v-if="!hasMore && list.length > 0" class="no-more">
        <text>没有更多了</text>
      </view>
    </scroll-view>

    <!-- 编辑/新增弹窗 -->
    <view v-if="showModal" class="modal-mask" @tap="closeModal">
      <view class="modal-content" @tap.stop>
        <view class="modal-header">
          <text class="modal-title">{{ isEdit ? '编辑种类' : '新增种类' }}</text>
          <text class="modal-close" @tap="closeModal">✕</text>
        </view>
        <view class="modal-body">
          <view class="form-item">
            <text class="form-label required">物料名称</text>
            <input class="form-input" v-model="formData.name" placeholder="请输入物料名称" />
          </view>
          <view class="form-item">
            <text class="form-label">规格</text>
            <input class="form-input" v-model="formData.spec" placeholder="请输入规格（可选）" />
          </view>
          <view class="form-item">
            <text class="form-label">单位</text>
            <input class="form-input" v-model="formData.unit" placeholder="请输入单位（可选）" />
          </view>
          <view class="form-item">
            <text class="form-label">状态</text>
            <view class="switch-box">
              <switch :checked="formData.isActive" @change="onSwitchChange" color="#667eea" />
              <text class="switch-label">{{ formData.isActive ? '启用' : '停用' }}</text>
            </view>
          </view>
        </view>
        <view class="modal-footer">
          <button class="btn-cancel" @tap="closeModal">取消</button>
          <button class="btn-confirm" @tap="handleSubmit" :disabled="submitting">
            {{ submitting ? '提交中...' : '确认' }}
          </button>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import consumableTempAPI, { type CatalogItem } from '@/pages/api/consumable-temp'

// 数据状态
const list = ref<CatalogItem[]>([])
const loading = ref(false)
const searchKeyword = ref('')
const page = ref(1)
const pageSize = ref(20)
const hasMore = ref(true)

// 弹窗相关
const showModal = ref(false)
const isEdit = ref(false)
const formData = ref<CatalogItem>({
  name: '',
  spec: '',
  unit: '',
  isActive: true
})
const submitting = ref(false)

// 加载列表
async function loadList(reset = false) {
  if (loading.value) return
  
  if (reset) {
    page.value = 1
    hasMore.value = true
    list.value = []
  }
  
  if (!hasMore.value) return
  
  loading.value = true
  
  try {
    const res = await consumableTempAPI.getCatalogList({
      page: page.value,
      pageSize: pageSize.value,
      q: searchKeyword.value || undefined
    })
    
    if (res.statusCode === 200 && (res.data as any)?.data) {
      const newData = (res.data as any).data
      list.value = reset ? newData : [...list.value, ...newData]
      hasMore.value = newData.length >= pageSize.value
    } else {
      uni.showToast({ title: '加载失败', icon: 'none' })
    }
  } catch (err) {
    console.error('加载列表失败：', err)
    uni.showToast({ title: '加载失败', icon: 'none' })
  } finally {
    loading.value = false
  }
}

// 加载更多
function loadMore() {
  if (!loading.value && hasMore.value) {
    page.value++
    loadList()
  }
}

// 格式化时间
function formatTime(time?: string) {
  if (!time) return '-'
  const date = new Date(time)
  return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`
}

// 显示新增弹窗
function showAddModal() {
  isEdit.value = false
  formData.value = {
    name: '',
    spec: '',
    unit: '',
    isActive: true
  }
  showModal.value = true
}

// 编辑
function handleEdit(item: CatalogItem) {
  isEdit.value = true
  formData.value = { ...item }
  showModal.value = true
}

// 关闭弹窗
function closeModal() {
  showModal.value = false
}

// 开关改变
function onSwitchChange(e: any) {
  formData.value.isActive = e.detail.value
}

// 提交表单
async function handleSubmit() {
  if (!formData.value.name) {
    uni.showToast({ title: '请输入物料名称', icon: 'none' })
    return
  }
  
  if (submitting.value) return
  
  submitting.value = true
  
  try {
    let res
    if (isEdit.value && formData.value.id) {
      res = await consumableTempAPI.updateCatalog(formData.value.id, formData.value)
    } else {
      res = await consumableTempAPI.createCatalog(formData.value)
    }
    
    if (res.statusCode === 200 || res.statusCode === 201) {
      uni.showToast({ 
        title: isEdit.value ? '更新成功' : '创建成功', 
        icon: 'success'
      })
      closeModal()
      loadList(true)
    } else {
      uni.showToast({ title: '操作失败', icon: 'none' })
    }
  } catch (err) {
    console.error('提交失败：', err)
    uni.showToast({ title: '操作失败', icon: 'none' })
  } finally {
    submitting.value = false
  }
}

// 停用
function handleDisable(id?: string) {
  if (!id) return
  
  uni.showModal({
    title: '确认停用',
    content: '停用后该种类将不会出现在申请选择列表中',
    success: async (res) => {
      if (res.confirm) {
        try {
          const result = await consumableTempAPI.deleteCatalog(id)
          if (result.statusCode === 204) {
            uni.showToast({ title: '已停用', icon: 'success' })
            loadList(true)
          } else {
            uni.showToast({ title: '操作失败', icon: 'none' })
          }
        } catch (err) {
          console.error('停用失败：', err)
          uni.showToast({ title: '操作失败', icon: 'none' })
        }
      }
    }
  })
}

// 启用
async function handleEnable(id?: string) {
  if (!id) return
  
  try {
    const res = await consumableTempAPI.updateCatalog(id, { isActive: true })
    if (res.statusCode === 200) {
      uni.showToast({ title: '已启用', icon: 'success' })
      loadList(true)
    } else {
      uni.showToast({ title: '操作失败', icon: 'none' })
    }
  } catch (err) {
    console.error('启用失败：', err)
    uni.showToast({ title: '操作失败', icon: 'none' })
  }
}

onMounted(() => {
  loadList(true)
})
</script>

<style scoped>
.page {
  min-height: 100vh;
  background: #f5f7fb;
  display: flex;
  flex-direction: column;
  box-sizing: border-box;
}

.header {
  background: #fff;
  padding: 32rpx;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.06);
}

.title {
  font-size: 36rpx;
  font-weight: 600;
  color: #111;
}

.search-bar {
  background: #fff;
  padding: 24rpx 32rpx;
  margin-bottom: 12rpx;
  display: flex;
  gap: 12rpx;
}

.search-box {
  flex: 1;
  display: flex;
  align-items: center;
  padding: 0 20rpx;
  background: #f5f7fb;
  border-radius: 12rpx;
  height: 72rpx;
}

.search-icon {
  font-size: 28rpx;
  margin-right: 12rpx;
}

.search-input {
  flex: 1;
  height: 72rpx;
  line-height: 72rpx;
  font-size: 28rpx;
}

.btn-add {
  padding: 0 24rpx;
  height: 72rpx;
  line-height: 72rpx;
  background: #3a5ddd;
  color: #fff;
  border-radius: 12rpx;
  font-size: 28rpx;
  white-space: nowrap;
}

.list-container {
  flex: 1;
  padding: 0 32rpx 24rpx;
}

.empty {
  padding: 120rpx 0;
  text-align: center;
}

.empty-text {
  font-size: 28rpx;
  color: #999;
}

.catalog-card {
  background: #fff;
  border-radius: 20rpx;
  padding: 24rpx;
  margin-bottom: 16rpx;
  box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.06);
}

.card-main {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 16rpx;
}

.catalog-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 8rpx;
}

.catalog-name {
  font-size: 30rpx;
  font-weight: 600;
  color: #111;
}

.catalog-meta {
  display: flex;
  gap: 16rpx;
  flex-wrap: wrap;
}

.meta-item {
  font-size: 24rpx;
  color: #666;
  padding: 4rpx 12rpx;
  background: #f5f7fb;
  border-radius: 6rpx;
}

.catalog-status {
  padding: 8rpx 16rpx;
  border-radius: 8rpx;
  font-size: 24rpx;
  font-weight: 500;
}

.catalog-status.active {
  background: #e8f5e9;
  color: #2e7d32;
}

.catalog-status.inactive {
  background: #f5f5f5;
  color: #757575;
}

.card-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-top: 16rpx;
  border-top: 1rpx solid #f0f0f0;
}

.time {
  font-size: 24rpx;
  color: #999;
}

.actions {
  display: flex;
  gap: 8rpx;
}

.btn-edit,
.btn-disable,
.btn-enable {
  padding: 8rpx 20rpx;
  border-radius: 8rpx;
  font-size: 24rpx;
}

.btn-edit {
  background: #e3f2fd;
  color: #1976d2;
}

.btn-disable {
  background: #fff3e0;
  color: #f57c00;
}

.btn-enable {
  background: #e8f5e9;
  color: #2e7d32;
}

.loading,
.no-more {
  padding: 32rpx;
  text-align: center;
  font-size: 26rpx;
  color: #999;
}

/* 弹窗样式 */
.modal-mask {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal-content {
  width: 600rpx;
  background: #fff;
  border-radius: 24rpx;
}

.modal-header {
  padding: 32rpx;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1rpx solid #f0f0f0;
}

.modal-title {
  font-size: 32rpx;
  font-weight: 600;
  color: #111;
}

.modal-close {
  font-size: 40rpx;
  color: #999;
  width: 60rpx;
  height: 60rpx;
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal-body {
  padding: 32rpx;
}

.form-item {
  margin-bottom: 24rpx;
}

.form-item:last-child {
  margin-bottom: 0;
}

.form-label {
  display: block;
  font-size: 28rpx;
  color: #333;
  margin-bottom: 12rpx;
}

.form-label.required::before {
  content: '*';
  color: #ff4d4f;
  margin-right: 4rpx;
}

.form-input {
  width: 100%;
  height: 72rpx;
  line-height: 72rpx;
  padding: 0 20rpx;
  background: #f5f7fb;
  border-radius: 12rpx;
  font-size: 28rpx;
}

.switch-box {
  display: flex;
  align-items: center;
  gap: 16rpx;
}

.switch-label {
  font-size: 28rpx;
  color: #333;
}

.modal-footer {
  padding: 24rpx 32rpx;
  border-top: 1rpx solid #f0f0f0;
  display: flex;
  gap: 16rpx;
}

.btn-cancel,
.btn-confirm {
  flex: 1;
  height: 80rpx;
  line-height: 80rpx;
  border-radius: 12rpx;
  font-size: 30rpx;
  font-weight: 500;
}

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

.btn-confirm {
  background: #3a5ddd;
  color: #fff;
}

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