<template>
  <div class="invoice-manage-container">
    <PageTitle title="开票管理" />
    
    <!-- 搜索栏 -->
    <div class="search-bar">
      <el-input v-model="searchForm.keyword" placeholder="搜索" clearable style="width: 180px">
        <template #prefix>
          <el-icon><Search /></el-icon>
        </template>
      </el-input>
      <div class="date-filter">
        <span class="label">订单下单时间</span>
        <el-date-picker
          v-model="searchForm.orderDateRange"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="YYYY-MM-DD"
          style="width: 240px"
        />
      </div>
      <div class="date-filter">
        <span class="label">订单完成时间</span>
        <el-date-picker
          v-model="searchForm.completeDateRange"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="YYYY-MM-DD"
          style="width: 240px"
        />
      </div>
    </div>

    <!-- 操作按钮 -->
    <div class="action-bar">
      <el-button>订单导出</el-button>
      <el-button>Excel批量导入开票</el-button>
    </div>

    <!-- 未开发票订单列表 -->
    <div class="order-list-title">未开发票订单列表</div>
    
    <div class="order-list">
      <div v-for="order in orderList" :key="order.id" class="order-card">
        <div class="order-header">
          <el-checkbox v-model="order.checked" />
          <span class="order-time">{{ order.orderTime }}</span>
          <span class="order-no">订单号：{{ order.orderNo }}</span>
        </div>
        <div class="order-content">
          <el-checkbox v-model="order.checked" class="item-checkbox" />
          <img :src="order.productImg" class="product-img" />
          <div class="product-info">
            <div class="product-name">{{ order.productName }}</div>
            <div class="product-spec">
              <span>{{ order.spec1 }}</span>
              <span>{{ order.spec2 }}</span>
              <span class="quantity">x{{ order.quantity }}</span>
            </div>
            <div class="product-price">¥{{ order.price }}</div>
          </div>
          <div class="pay-info">
            <div class="pay-amount">实付款 <span class="amount">¥{{ order.payAmount }}</span></div>
            <div class="pay-method">{{ order.payMethod }}</div>
          </div>
          <div class="order-status">
            <span class="status-text">{{ order.status }}</span>
          </div>
        </div>
      </div>
    </div>

    <!-- 底部操作栏 -->
    <div class="bottom-bar">
      <div class="select-all">
        <el-checkbox v-model="selectAll" @change="handleSelectAll">全选</el-checkbox>
      </div>
      <div class="summary">
        <span>已勾选 <em>{{ selectedCount }}/{{ orderList.length }}</em> 个订单</span>
        <span class="total">共计 <em>¥{{ totalAmount.toFixed(2) }}</em></span>
        <el-button type="danger" :disabled="selectedCount === 0">申请开票</el-button>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref, computed } from 'vue'
import { Search } from '@element-plus/icons-vue'
import { PageTitle } from '@/components'
import type { CheckboxValueType } from 'element-plus'

// 搜索表单
const searchForm = reactive({
  keyword: '',
  orderDateRange: [],
  completeDateRange: []
})

// 全选
const selectAll = ref(false)

// 模拟订单数据
const orderList = ref([
  {
    id: 1,
    checked: true,
    orderTime: '2025/12/05 16:15:06',
    orderNo: '489283929283298392',
    productImg: 'https://via.placeholder.com/80',
    productName: '清华同方超越E500台式机电脑超越E500台式机电脑(i3-6100/4G/1T/19.5寸)',
    spec1: '规格02',
    spec2: '规格01',
    quantity: 1,
    price: 181,
    payAmount: 181,
    payMethod: '微信支付',
    status: '已完成'
  },
  {
    id: 2,
    checked: false,
    orderTime: '2025/12/05 16:15:06',
    orderNo: '489283929283298392',
    productImg: 'https://via.placeholder.com/80',
    productName: '清华同方超越E500台式机电脑超越E500台式机电脑(i3-6100/4G/1T/19.5寸)',
    spec1: '规格02',
    spec2: '规格01',
    quantity: 1,
    price: 181,
    payAmount: 181,
    payMethod: '微信支付',
    status: '已完成'
  },
  {
    id: 3,
    checked: false,
    orderTime: '2025/12/05 16:15:06',
    orderNo: '489283929283298392',
    productImg: 'https://via.placeholder.com/80',
    productName: '清华同方超越E500台式机电脑超越E500台式机电脑(i3-6100/4G/1T/19.5寸)',
    spec1: '规格02',
    spec2: '规格01',
    quantity: 1,
    price: 181,
    payAmount: 181,
    payMethod: '微信支付',
    status: '已完成'
  }
])

// 计算选中数量
const selectedCount = computed(() => orderList.value.filter(item => item.checked).length)

// 计算总金额
const totalAmount = computed(() => {
  return orderList.value.filter(item => item.checked).reduce((sum, item) => sum + item.payAmount, 0)
})

// 全选处理
const handleSelectAll = (val: CheckboxValueType) => {
  orderList.value.forEach(item => {
    item.checked = !!val
  })
}
</script>

<style scoped lang="scss">
.invoice-manage-container {
  padding: 20px;
  background: #fff;
  min-height: 100%;
}

.search-bar {
  display: flex;
  align-items: center;
  gap: 20px;
  margin-bottom: 16px;
  
  .date-filter {
    display: flex;
    align-items: center;
    gap: 8px;
    
    .label {
      color: #666;
      font-size: 14px;
      white-space: nowrap;
    }
  }
}

.action-bar {
  margin-bottom: 20px;
}

.order-list-title {
  font-size: 14px;
  color: #333;
  margin-bottom: 16px;
}

.order-list {
  .order-card {
    border: 1px solid #eee;
    margin-bottom: 16px;
    
    .order-header {
      display: flex;
      align-items: center;
      gap: 16px;
      padding: 12px 16px;
      background: #fafafa;
      border-bottom: 1px solid #eee;
      
      .order-time {
        color: #666;
        font-size: 14px;
      }
      
      .order-no {
        color: #666;
        font-size: 14px;
      }
    }
    
    .order-content {
      display: flex;
      align-items: center;
      padding: 16px;
      gap: 16px;
      
      .item-checkbox {
        display: none;
      }
      
      .product-img {
        width: 80px;
        height: 80px;
        object-fit: cover;
        border: 1px solid #eee;
      }
      
      .product-info {
        flex: 1;
        
        .product-name {
          font-size: 14px;
          color: #333;
          margin-bottom: 8px;
          line-height: 1.4;
        }
        
        .product-spec {
          font-size: 12px;
          color: #999;
          margin-bottom: 8px;
          display: flex;
          gap: 12px;
          
          .quantity {
            color: #666;
          }
        }
        
        .product-price {
          font-size: 14px;
          color: #e60012;
          font-weight: bold;
        }
      }
      
      .pay-info {
        width: 120px;
        text-align: center;
        
        .pay-amount {
          font-size: 14px;
          color: #333;
          margin-bottom: 4px;
          
          .amount {
            color: #e60012;
            font-weight: bold;
          }
        }
        
        .pay-method {
          font-size: 12px;
          color: #999;
        }
      }
      
      .order-status {
        width: 80px;
        text-align: center;
        
        .status-text {
          color: #e60012;
          font-size: 14px;
        }
      }
    }
  }
}

.bottom-bar {
  margin-top: 20px;
  padding: 16px;
  background: #fafafa;
  border: 1px solid #eee;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  
  .select-all {
    display: flex;
    align-items: center;
  }
  
  .summary {
    display: flex;
    align-items: center;
    gap: 20px;
    
    span {
      font-size: 14px;
      color: #666;
      
      em {
        color: #e60012;
        font-style: normal;
      }
    }
    
    .total em {
      font-size: 16px;
      font-weight: bold;
    }
  }
}
</style>
