<template>
  <view class="marketing-live-product">
    <!-- List View -->
    <template v-if="!isAdding">
      <view class="filter-card border-shadow">
        <view class="filter-row">
          <view class="filter-item">
            <text class="label">审核状态：</text>
            <view class="select-mock">
              <text class="select-val">全部</text>
              <text class="arrow">▼</text>
            </view>
          </view>
          <view class="filter-item ml-24">
            <text class="label">搜索：</text>
            <input class="input-mock" placeholder="请输入商品名称/ID" />
          </view>
          <button class="btn-query ml-16">查询</button>
        </view>
      </view>

      <view class="action-bar">
        <button class="btn-add" @click="isAdding = true">添加商品</button>
      </view>

      <view class="table-card border-shadow">
        <view class="table-container">
          <view class="table-head">
            <view class="th cell-id">商品ID</view>
            <view class="th cell-info">商品名称</view>
            <view class="th cell-price">直播价</view>
            <view class="th cell-price">原价</view>
            <view class="th cell-stock">库存</view>
            <view class="th cell-audit">审核状态</view>
            <view class="th cell-status">是否显示</view>
            <view class="th cell-op">操作</view>
          </view>

          <view class="table-body">
            <view v-for="item in productList" :key="item.id" class="table-row">
              <view class="td cell-id"><text class="td-txt">{{ item.id }}</text></view>
              <view class="td cell-info">
                <view class="info-wrap">
                  <image class="thumb" :src="item.image" mode="aspectFill"></image>
                  <text class="p-title line-clamp-2">{{ item.title }}</text>
                </view>
              </view>
              <view class="td cell-price"><text class="td-txt">{{ item.live_price.toFixed(2) }}</text></view>
              <view class="td cell-price"><text class="td-txt">{{ item.price.toFixed(2) }}</text></view>
              <view class="td cell-stock"><text class="td-txt">{{ item.stock }}</text></view>
              <view class="td cell-audit"><text class="td-txt">{{ item.audit_status }}</text></view>
              <view class="td cell-status">
                <view class="switch-mock" :class="{ active: item.is_show }" @click="toggleStatus(item)">
                  <view class="switch-dot"></view>
                  <text class="switch-txt">{{ item.is_show ? '显示' : '隐藏' }}</text>
                </view>
              </view>
              <view class="td cell-op">
                <view class="op-links">
                  <text class="op-link" @click="handleEdit(item)">详情</text>
                  <text class="op-split">|</text>
                  <text class="op-link" @click="handleDelete(item)">删除</text>
                </view>
              </view>
            </view>
          </view>
        </view>

        <view class="pagination-footer">
          <view class="page-total"><text class="total-txt">共 {{ productList.length }} 条</text></view>
          <view class="page-select">
            <view class="select-mock mini">
              <text class="select-val">20条/页</text>
              <text class="arrow">▼</text>
            </view>
          </view>
          <view class="page-btns">
            <text class="p-btn disabled">‹</text>
            <text class="p-btn active">1</text>
            <text class="p-btn disabled">›</text>
          </view>
        </view>
      </view>
    </template>

    <!-- Adding View -->
    <template v-else>
      <view class="breadcrumb">
        <text class="back-link" @click="isAdding = false">‹ 返回</text>
        <text class="current-path">直播商品管理</text>
      </view>
      
      <view class="select-card border-shadow">
        <view class="form-row">
          <text class="f-label">选择商品：</text>
          <view class="selected-list">
             <view class="p-box" v-for="(p, index) in selectedList" :key="index">
                <image class="p-thumb" :src="p.image" mode="aspectFill"></image>
                <view class="remove-btn" @click="removeSelected(index)">×</view>
             </view>
             <view class="add-box" @click="handleAddProduct">
                <text class="add-ic">👜</text>
             </view>
          </view>
        </view>
        <view class="form-row mt-24">
           <button class="btn-generate" @click="handleGenerate">生成直播商品</button>
        </view>
      </view>
    </template>
  </view>
</template>

<script setup lang="uts">
import { ref } from 'vue'

const isAdding = ref(false)
const selectedList = ref([
  { image: 'https://img0.baidu.com/it/u=3033502919,1657850259&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500' }
])

const productList = ref([
  {
    id: 92,
    image: 'https://img0.baidu.com/it/u=3023224345,1529124233&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
    title: '绣球永生花网红干花花束大',
    live_price: 149.00,
    price: 149.00,
    stock: 10617,
    audit_status: '审核通过',
    is_show: false
  },
  {
    id: 89,
    image: 'https://img1.baidu.com/it/u=3175865615,2002599723&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
    title: '家居梵高系列款软版盒袋',
    live_price: 350.00,
    price: 350.00,
    stock: 8625,
    audit_status: '审核通过',
    is_show: false
  },
  {
    id: 93,
    image: 'https://img2.baidu.com/it/u=2719717192,3826027113&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
    title: '【LESHUCANGHU',
    live_price: 300.00,
    price: 300.00,
    stock: 164,
    audit_status: '审核通过',
    is_show: false
  },
  {
    id: 116,
    image: 'https://img0.baidu.com/it/u=2257917711,1359654032&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
    title: '爱奇艺智能 奇遇LT01',
    live_price: 1199.00,
    price: 1199.00,
    stock: 6287,
    audit_status: '审核通过',
    is_show: false
  }
])

const toggleStatus = (item: any) => {
  item.is_show = !item.is_show
  uni.showToast({ title: '状态修改成功', icon: 'success' })
}

const handleEdit = (item: any) => {
  uni.showToast({ title: '详情查看中', icon: 'none' })
}

const handleDelete = (item: any) => {
  uni.showModal({
    title: '提示',
    content: '确定要删除该商品吗？',
    success: (res) => {
      if (res.confirm) {
        productList.value = productList.value.filter(i => i.id !== item.id)
        uni.showToast({ title: '删除成功' })
      }
    }
  })
}

const handleAddProduct = () => {
  uni.showToast({ title: '选择商品功能开发中', icon: 'none' })
}

const removeSelected = (index: number) => {
  selectedList.value.splice(index, 1)
}

const handleGenerate = () => {
  uni.showToast({ title: '生成成功', icon: 'success' })
  isAdding.value = false
}
</script>

<style scoped lang="scss">
.marketing-live-product {
  min-height: 100vh;
  background: #f0f2f5;
  padding: 16px;
}

.border-shadow {
  background: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
}

.ml-16 { margin-left: 16px; }
.ml-24 { margin-left: 24px; }
.mt-24 { margin-top: 24px; }

/* 过滤栏 */
.filter-card {
  padding: 24px;
  margin-bottom: 16px;
}

.filter-row {
  display: flex;
  flex-direction: row;
  align-items: center;
}

.filter-item {
  display: flex;
  flex-direction: row;
  align-items: center;
}

.label {
  font-size: 14px;
  color: #606266;
  white-space: nowrap;
}

.input-mock, .select-mock {
  width: 200px;
  height: 32px;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  display: flex;
  flex-direction: row;
  align-items: center;
  padding: 0 12px;
  font-size: 13px;
}
.input-mock { width: 300px; }
.select-mock { width: 160px; justify-content: space-between; }
.select-mock.mini { width: 100px; height: 28px; }

.select-val { font-size: 13px; color: #606266; }
.arrow { font-size: 10px; color: #c0c4cc; }

.btn-query {
  width: 64px;
  height: 32px;
  background-color: #1890ff;
  color: #fff;
  font-size: 14px;
  border: none;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

/* 操作栏 */
.action-bar {
  margin-bottom: 16px;
}
.btn-add {
  width: auto;
  padding: 0 16px;
  height: 32px;
  background-color: #1890ff;
  color: #fff;
  font-size: 14px;
  border: none;
  border-radius: 4px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

/* 表格区域 */
.table-card {
  padding: 24px;
}

.table-head {
  display: flex;
  flex-direction: row;
  background-color: #f8f8f9;
  border-bottom: 1px solid #e8eaec;
}

.th {
  padding: 12px 8px;
  font-size: 13px;
  color: #515a6e;
  font-weight: bold;
}

.table-row {
  display: flex;
  flex-direction: row;
  border-bottom: 1px solid #e8eaec;
  align-items: center;
}

.td {
  padding: 16px 8px;
}

.td-txt { font-size: 13px; color: #515a6e; }

/* 各列宽度 */
.cell-id { width: 70px; }
.cell-info { flex: 1; min-width: 250px; }
.cell-price { width: 100px; text-align: center; }
.cell-stock { width: 100px; text-align: center; }
.cell-audit { width: 120px; text-align: center; }
.cell-status { width: 100px; text-align: center; }
.cell-op { width: 120px; text-align: right; }

.info-wrap {
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 12px;
}
.thumb {
  width: 40px;
  height: 40px;
  border-radius: 4px;
}
.p-title {
  font-size: 13px;
  color: #515a6e;
  line-height: 1.4;
}
.line-clamp-2 {
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.switch-mock {
  width: 50px;
  height: 24px;
  background-color: #bfbfbf;
  border-radius: 12px;
  display: flex;
  flex-direction: row;
  align-items: center;
  padding: 0 4px;
  position: relative;
  transition: background-color 0.3s;
}
.switch-mock.active { background-color: #1890ff; }
.switch-dot {
  width: 16px;
  height: 16px;
  background-color: #fff;
  border-radius: 50%;
  position: absolute;
  left: 4px;
  transition: left 0.3s;
}
.switch-mock.active .switch-dot { left: 30px; }
.switch-txt { font-size: 11px; color: #fff; margin-left: 20px; }
.switch-mock.active .switch-txt { margin-left: 4px; }

.op-links {
  display: flex;
  flex-direction: row;
  justify-content: flex-end;
  align-items: center;
}
.op-link { color: #1890ff; font-size: 13px; cursor: pointer; }
.op-split { color: #e8eaec; margin: 0 8px; }

/* Adding Template Styles */
.breadcrumb {
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 12px;
  margin-bottom: 16px;
}
.back-link { font-size: 14px; color: #8c8c8c; cursor: pointer; }
.current-path { font-size: 14px; color: #262626; font-weight: 600; }

.select-card {
  padding: 48px;
}
.form-row {
  display: flex;
  flex-direction: row;
}
.f-label {
  width: 100px;
  font-size: 14px;
  color: #262626;
}
.selected-list {
  flex: 1;
  display: flex;
  flex-direction: row;
  gap: 16px;
}
.p-box {
  width: 64px;
  height: 64px;
  position: relative;
}
.p-thumb {
  width: 100%;
  height: 100%;
  border-radius: 4px;
  border: 1px solid #f0f0f0;
}
.remove-btn {
  position: absolute;
  top: -8px;
  right: -8px;
  width: 16px;
  height: 16px;
  background-color: #bfbfbf;
  color: #fff;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
}
.add-box {
  width: 64px;
  height: 64px;
  border: 1px dashed #d9d9d9;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #fafafa;
  cursor: pointer;
}
.add-ic { font-size: 24px; color: #bfbfbf; }

.btn-generate {
  width: auto;
  padding: 0 16px;
  height: 32px;
  background-color: #1890ff;
  color: #fff;
  font-size: 14px;
  border: none;
  border-radius: 4px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  margin-left: 100px;
}

/* 分页 */
.pagination-footer {
  margin-top: 24px;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: flex-end;
  gap: 12px;
}
.total-txt { font-size: 13px; color: #606266; }

</style>


