<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter"
      :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="search">
        <el-form ref="queryFormRef" :model="queryParams" :inline="true" label-width="85px">
          <el-form-item label="订单单号" prop="code">
            <el-input v-model="queryParams.code" placeholder="请输入订单单号" clearable @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="下单人" prop="placer">
            <el-input v-model="queryParams.placer" placeholder="请输入下单人" clearable @keyup.enter="handleQuery" />
          </el-form-item>

          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <div class="status-tabs">
        <div v-for="(tab, index) in statusTabs" :key="index" class="tab-item"
          :class="{ active: currentTab === tab.value }" @click="handleTabClick(tab.value)">
          {{ tab.label }}
        </div>
      </div>

      <el-table v-loading="loading" :data="orderList" border>
        <el-table-column label="订单单号" align="center" prop="code" />
        <el-table-column label="下单人" align="center" prop="placerName" />
        <el-table-column label="下单时间" align="center" prop="placeTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.placeTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="总支数" align="center" prop="totalCount" />
        <el-table-column label="状态" align="center" prop="status">
          <template #default="scope">
            <el-tag v-if="scope.row.status === 0" type="warning">待审核</el-tag>
            <el-tag v-else-if="scope.row.status === 2" type="primary">待签批</el-tag>
            <el-tag v-else-if="scope.row.status === 3" type="info">生产中</el-tag>
            <el-tag v-else-if="scope.row.status === 4" type="success">已完成</el-tag>
            <el-tag v-else type="info">未知</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="100" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button v-hasPermi="['order:order:query']" link type="warning" icon="View"
              @click="handleViewOrder(scope.row)">查看</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>

    <!-- 订单详情对话框 -->
    <el-dialog title="订单详情" v-model="detailOpen" width="800px" append-to-body>
      <template v-if="detailOrder">
        <div class="section-title">订单信息</div>
        <el-descriptions :column="2" border size="small">
          <el-descriptions-item label="订单单号" :span="2">{{ detailOrder.code }}</el-descriptions-item>
          <el-descriptions-item label="下单人">{{ detailOrder.placerName }}</el-descriptions-item>
          <el-descriptions-item label="订单状态">
            <el-tag v-if="detailOrder.status === 0" type="warning">待审核</el-tag>
            <el-tag v-else-if="detailOrder.status === 2" type="primary">待签批</el-tag>
            <el-tag v-else-if="detailOrder.status === 3" type="info">生产中</el-tag>
            <el-tag v-else-if="detailOrder.status === 4" type="success">已完成</el-tag>
            <el-tag v-else type="info">未知</el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="下单时间">{{ parseTime(detailOrder.placeTime) }}</el-descriptions-item>
          <el-descriptions-item label="创建时间">{{ parseTime(detailOrder.createTime) }}</el-descriptions-item>
          <el-descriptions-item label="总支数">{{ detailOrder.totalCount }}</el-descriptions-item>
        </el-descriptions>

        <div class="section-title" style="margin-top: 20px">订单明细</div>
        <div class="detail-scroll" v-if="detailOrder.details && detailOrder.details.length > 0">
          <div class="detail-card" v-for="(detail, dIdx) in detailOrder.details" :key="dIdx">
            <div class="detail-card-head">规格清单 #{{ dIdx + 1 }}</div>
            <el-descriptions :column="2" border size="small">
              <el-descriptions-item label="产品型号" :span="2">{{ detail.modelNum || '未知型号' }}</el-descriptions-item>
              <el-descriptions-item label="型号名称" :span="2">{{ detail.modelName || '铝型材主料' }}</el-descriptions-item>
              <el-descriptions-item label="型材材质">{{ detail.material || '6063-T5' }}</el-descriptions-item>
              <el-descriptions-item label="需求支数">
                <span class="count-red">{{ detail.count || 0 }} 支</span>
              </el-descriptions-item>
              <el-descriptions-item label="表面处理">{{ detail.surfaceName || '无' }}</el-descriptions-item>
              <el-descriptions-item label="包装方式">{{ detail.packName || '普通包装' }}</el-descriptions-item>
              <el-descriptions-item label="订单长度">{{ formatNum(detail.length) }} mm</el-descriptions-item>
              <el-descriptions-item label="型材壁厚">{{ formatNum(detail.wallThickness, '1.2') }} mm</el-descriptions-item>
            </el-descriptions>
            <div class="detail-remark" v-if="detail.remark">
              <span class="remark-label">备注：</span>{{ detail.remark }}
            </div>
          </div>
        </div>
        <el-empty v-else description="暂无明细" :image-size="60" />
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Order">
import { listOrder, getOrder } from "@/api/erp/order";

/** @Author: Antigravity */

const { proxy } = getCurrentInstance();

const orderList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const detailOpen = ref(false);
const detailOrder = ref(null);
const currentTab = ref(undefined);

const statusTabs = [
  { label: '全部', value: undefined },
  { label: '待审核', value: 0 },
  { label: '待签批', value: 2 },
  { label: '生产中', value: 3 },
  { label: '已完成', value: 4 }
];

const queryFormRef = ref();

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    code: undefined,
    placer: undefined,
    status: undefined
  }
});

const { queryParams } = toRefs(data);

/** 查询订单列表 */
function getList() {
  loading.value = true;
  queryParams.value.status = currentTab.value;
  listOrder(queryParams.value).then(response => {
    orderList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  queryParams.value.status = currentTab.value;
  getList();
}

/** 状态标签点击 */
function handleTabClick(value) {
  currentTab.value = value;
  handleQuery();
}

/** 重置按钮操作 */
function resetQuery() {
  queryFormRef.value?.resetFields();
  currentTab.value = undefined;
  handleQuery();
}

/** 查看订单详情 */
function handleViewOrder(row) {
  detailOpen.value = true;
  detailOrder.value = null;
  getOrder(row.rowId).then(res => {
    detailOrder.value = res.data;
  }).catch(() => {
    proxy.$modal.msgError("获取订单详情失败");
  });
}

/** 格式化数值，匹配小程序端的 toFixed(4) 显示 */
function formatNum(val, fallback = '0') {
  if (val === null || val === undefined || val === '') return fallback;
  const num = Number(val);
  return isNaN(num) ? fallback : num.toFixed(4);
}

getList();
</script>

<style scoped>
/* ========== 状态筛选标签栏 ========== */
.status-tabs {
  display: flex;
  gap: 2px;
  margin-bottom: 16px;
  border-bottom: 1px solid #ebeef5;
  padding-bottom: 0;
}

.tab-item {
  position: relative;
  padding: 10px 20px;
  font-size: 14px;
  color: #606266;
  cursor: pointer;
  transition: color 0.25s ease;
  user-select: none;
}

.tab-item::after {
  content: '';
  position: absolute;
  bottom: -1px;
  left: 50%;
  transform: translateX(-50%) scaleX(0);
  width: 100%;
  height: 2px;
  background: #409eff;
  transition: transform 0.25s ease;
}

.tab-item:hover {
  color: #409eff;
}

.tab-item.active {
  color: #409eff;
  font-weight: 600;
}

.tab-item.active::after {
  transform: translateX(-50%) scaleX(1);
}

/* ========== 详情弹框样式 ========== */
.section-title {
  font-size: 14px;
  font-weight: 600;
  color: #303133;
  margin-bottom: 12px;
  padding-left: 10px;
  border-left: 3px solid #C1001C;
}

.detail-scroll {
  max-height: 420px;
  overflow-y: auto;
  padding-right: 2px;
}

.detail-card {
  background: #fff;
  border: 1px solid #ebeef5;
  border-radius: 6px;
  padding: 14px;
  margin-bottom: 12px;
}

.detail-card-head {
  font-size: 13px;
  font-weight: 600;
  color: #303133;
  margin-bottom: 10px;
}

.count-red {
  color: #ff3b30;
  font-weight: 600;
}

.detail-remark {
  margin-top: 8px;
  padding: 6px 10px;
  background: #fafafa;
  border-radius: 4px;
  font-size: 12px;
  color: #606266;
}

.remark-label {
  color: #909399;
}
</style>