<template>
  <div class="app-container order-detail-page">
    <div class="detail-container">
      <!-- 顶部信息块 -->
      <div class="info-blocks">
        <!-- 订单信息 -->
        <div class="info-block order-info">
          <div class="block-title">订单信息</div>
          <div class="info-item">
            <span class="label">订单编号：</span>
            <span class="value">{{ orderInfo.orderSn }}</span>
            <el-link type="primary" :underline="false" class="ml10" @click="copyText(orderInfo.orderSn)">复制</el-link>
          </div>
          <div class="info-item">
            <span class="label">下单时间：</span>
            <span class="value">{{ orderInfo.createTime }}</span>
          </div>
          <div class="info-item" v-if="orderInfo.payTime">
            <span class="label">支付时间：</span>
            <span class="value">{{ orderInfo.payTime }}</span>
          </div>
          <div class="info-item status-item">
            <span class="label">订单状态：</span>
            <template v-if="orderInfo.status === '1'">
              <span class="status-tag success">已完成</span>
            </template>
            <template v-else-if="orderInfo.status === '0'">
              <span class="status-tag warning">待付款</span>
              <span class="status-desc">（剩余23分钟自动关闭订单）</span>
              <el-link type="primary" :underline="false" class="close-link" @click="openCancelDialog">取消订单</el-link>
            </template>
            <template v-else>
              <span class="status-tag info">{{ orderInfo.statusLabel }}</span>
            </template>
          </div>
          <div class="info-item">
            <span class="label">支付方式：</span>
            <span class="value">{{ orderInfo.payMethod }}</span>
          </div>
          <div class="info-item">
            <span class="label">订单类型：</span>
            <span class="value">{{ orderInfo.orderType }}</span>
          </div>
          <div class="info-item">
            <span class="label">商品类型：</span>
            <span class="value">{{ orderInfo.productType }}</span>
          </div>
          <div class="info-item">
            <span class="label">订单来源：</span>
            <span class="value">{{ orderInfo.source }}</span>
          </div>
          <div class="info-item">
            <span class="label">订单备注：</span>
            <span class="value">{{ orderInfo.remark || '无' }}</span>
            <el-link type="primary" :underline="false" class="ml10">编辑备注</el-link>
          </div>
        </div>

        <!-- 买家信息 -->
        <div class="info-block buyer-info">
          <div class="block-title">
            买家信息
            <el-link type="primary" :underline="false" class="ml10">查看资料</el-link>
          </div>
          <div class="info-item">
            <span class="label">微信昵称：</span>
            <span class="value">{{ buyerInfo.wechatNickname }}</span>
          </div>
          <div class="info-item">
            <span class="label">用户姓名：</span>
            <span class="value">{{ buyerInfo.userName }}</span>
          </div>
          <div class="info-item">
            <span class="label">用户性别：</span>
            <span class="value">{{ buyerInfo.gender }}</span>
          </div>
          <div class="info-item">
            <span class="label">用户年龄：</span>
            <span class="value">{{ buyerInfo.age }}</span>
          </div>
          <div class="info-item">
            <span class="label">手机号码：</span>
            <span class="value">{{ buyerInfo.phone }}</span>
          </div>
          <div class="info-item">
            <span class="label">用户ID：</span>
            <span class="value">{{ buyerInfo.userId }}</span>
          </div>
          <div class="info-item">
            <span class="label">注册时间：</span>
            <span class="value">{{ buyerInfo.registerTime }}</span>
          </div>
          <div class="info-item">
            <span class="label">最后登录时间：</span>
            <span class="value">{{ buyerInfo.lastLoginTime }}</span>
          </div>
        </div>
      </div>

      <!-- 商品信息 -->
      <div class="product-block">
        <div class="block-title">商品信息</div>
        <el-table :data="productList" border style="width: 100%" header-cell-class-name="custom-header">
          <el-table-column label="商品" min-width="250">
            <template #default="scope">
              <div class="product-cell">
                <img :src="scope.row.productImg" class="p-img" />
                <el-link type="primary" :underline="false" class="p-name">{{ scope.row.productName }}</el-link>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="单价（元）" width="150">
            <template #default="scope">
              <div class="price-cell">
                <div>总价：{{ scope.row.totalPrice }}</div>
                <div>定金：{{ scope.row.deposit }}</div>
                <div>尾款：{{ scope.row.balance }}</div>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="quantity" label="数量" width="100" />
          <el-table-column prop="productType" label="商品类型" width="120" align="center" />
          <el-table-column label="订单状态" width="120" align="center">
            <template #default="scope">
              <span :class="getStatusClass(scope.row.status)">{{ scope.row.statusLabel }}</span>
            </template>
          </el-table-column>
          <el-table-column label="订单类型" width="150" align="center">
            <template #default="scope">
              {{ scope.row.orderType }} ({{ scope.row.deposit }})
            </template>
          </el-table-column>
          <el-table-column prop="postage" label="邮费" width="100" align="center" />
          <el-table-column prop="actualPayment" label="合计付款（元）" width="150" align="center" />
        </el-table>
      </div>
    </div>

    <!-- 取消订单确认弹窗 -->
    <el-dialog title="取消订单" v-model="cancelDialogVisible" width="500px">
      <el-form :model="cancelForm" label-width="120px" class="cancel-form mt20">
        <el-form-item label="取消订单原因：">
          <el-input 
            type="textarea" 
            v-model="cancelForm.reason" 
            placeholder="请输入取消订单原因" 
            :rows="4" 
            style="width: 250px" 
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div style="text-align: center;">
          <el-button type="primary" @click="confirmCancel">确定</el-button>
          <el-button @click="cancelDialogVisible = false">取消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { useRoute } from 'vue-router';
import { ElMessage } from 'element-plus';

const route = useRoute();

// 模拟订单数据
const orderInfo = ref({
  orderSn: '2020033016190804520',
  createTime: '2019-09-09 10:40',
  payTime: '2019-09-09 10:40', // 如果是待付款可能是空的
  status: '1', // 1: 已完成, 0: 待付款
  statusLabel: '已完成',
  payMethod: '微信支付',
  orderType: '定金',
  productType: '测评',
  source: '小程序',
  remark: ''
});

// 模拟买家数据
const buyerInfo = ref({
  wechatNickname: '小丸子',
  userName: '向微微',
  gender: '女',
  age: 23,
  phone: '18890900900',
  userId: '18890900900',
  registerTime: '2019-09-09 10:40',
  lastLoginTime: '2019-09-09 10:40'
});

// 模拟商品数据
const productList = ref([
  {
    productImg: 'https://via.placeholder.com/60',
    productName: '单计势0-B2高级直播班',
    totalPrice: '300.00',
    deposit: '30.00',
    balance: '270.00',
    quantity: 1,
    productType: '岗位',
    status: '1',
    statusLabel: '已完成',
    orderType: '定金',
    postage: '30.00',
    actualPayment: '30.00'
  }
]);

onMounted(() => {
  const status = route.query.status as string;
  if (status === '0') {
    // 模拟待付款状态
    orderInfo.value.status = '0';
    orderInfo.value.statusLabel = '待付款';
    orderInfo.value.payTime = '';
    productList.value[0].status = '0';
    productList.value[0].statusLabel = '待付款';
  }
});

const copyText = (text: string) => {
  navigator.clipboard.writeText(text).then(() => {
    ElMessage.success('复制成功');
  });
};

const getStatusClass = (status: string) => {
  if (status === '1') return 'status-success';
  if (status === '0') return 'status-warning';
  return 'status-info';
};

const cancelDialogVisible = ref(false);
const cancelForm = ref({ reason: '' });

const openCancelDialog = () => {
  cancelForm.value.reason = '';
  cancelDialogVisible.value = true;
};

const confirmCancel = () => {
  ElMessage.success('取消订单成功');
  cancelDialogVisible.value = false;
};
</script>

<style scoped lang="scss">
.order-detail-page {
  background-color: #f0f2f5;
  min-height: calc(100vh - 84px);
  padding: 20px;
}

.detail-container {
  background-color: #fff;
  padding: 30px;
  border-radius: 4px;
}

.info-blocks {
  display: flex;
  margin-bottom: 40px;
  border-bottom: 1px solid #f0f0f0;
  padding-bottom: 40px;
}

.info-block {
  flex: 1;
  padding-right: 20px;

  &.buyer-info {
    padding-left: 200px;
  }
}

.block-title {
  font-size: 16px;
  font-weight: bold;
  color: #1d2129;
  margin-bottom: 24px;
  display: flex;
  align-items: center;
}

.info-item {
  display: flex;
  margin-bottom: 16px;
  font-size: 14px;
  line-height: 22px;
  
  .label {
    width: 100px;
    color: #4e5969;
  }
  
  .value {
    color: #1d2129;
  }
}

.status-item {
  align-items: center;

  .status-tag {
    padding: 2px 8px;
    border-radius: 2px;
    font-size: 13px;
    
    &.success {
      background-color: #e8f3e8;
      color: #52c41a;
      border: 1px solid #b7eb8f;
    }
    
    &.warning {
      background-color: #fffbe6;
      color: #faad14;
      border: 1px solid #ffe58f;
    }

    &.info {
      background-color: #f2f3f5;
      color: #86909c;
      border: 1px solid #e5e6eb;
    }
  }

  .status-desc {
    color: #86909c;
    margin-left: 10px;
    font-size: 13px;
  }

  .close-link {
    margin-left: 10px;
    font-size: 13px;
  }
}

.product-block {
  margin-top: 20px;
  
  .block-title {
    margin-bottom: 16px;
  }
}

.product-cell {
  display: flex;
  align-items: center;
  gap: 12px;
  
  .p-img {
    width: 60px;
    height: 60px;
    border-radius: 4px;
    object-fit: cover;
  }
  
  .p-name {
    font-weight: 500;
  }
}

.price-cell {
  font-size: 13px;
  color: #1d2129;
  line-height: 20px;
}

:deep(.custom-header th) {
  background-color: #f7f8fa !important;
  color: #4e5969;
  font-weight: 500;
}

.status-success { color: #52c41a; font-weight: 600; }
.status-warning { color: #faad14; font-weight: 600; }
.status-info { color: #86909c; }

.ml10 { margin-left: 10px; }
.mt20 { margin-top: 20px; }

.cancel-form {
  :deep(.el-form-item__label) {
    font-weight: 600;
    color: #1d2129;
  }
}
</style>
