<template>
  <el-dialog title="对账单详情" v-model="visible" width="800px" append-to-body destroy-on-close>
    <div v-loading="loading">
      <el-descriptions title="基本信息" :column="2" border class="margin-bottom">
        <el-descriptions-item label="对账编号">{{ form.statementOrderNo }}</el-descriptions-item>
        <el-descriptions-item label="客户名称">{{ form.customerName || '-' }}</el-descriptions-item>
        <el-descriptions-item label="对账金额(元)">¥{{ form.amount != null ? Number(form.amount).toFixed(2) : '-' }}</el-descriptions-item>
        <el-descriptions-item label="对账日期">{{ parseTime(form.statementDate, '{y}-{m}-{d}') || '-' }}</el-descriptions-item>
        <el-descriptions-item label="对账人">{{ form.statementSelf || '-' }}</el-descriptions-item>
        <el-descriptions-item label="联系电话">{{ form.statementSelfPhone || '-' }}</el-descriptions-item>
        <el-descriptions-item label="对账状态">
          <dict-tag :options="statement_status" :value="form.statementStatus" />
        </el-descriptions-item>
        <el-descriptions-item label="开票状态">
          <dict-tag :options="invoice_issuance_status" :value="form.isInvoiceStatus" />
        </el-descriptions-item>
        <el-descriptions-item label="支付状态">
          <dict-tag :options="payment_status" :value="form.isPaymentStatus" />
        </el-descriptions-item>
        <el-descriptions-item label="备注" :span="2">{{ form.remark || '-' }}</el-descriptions-item>
      </el-descriptions>

      <!-- 订单明细列表 -->
      <div v-if="form.detailList && form.detailList.length > 0">
        <el-divider content-position="left">对账明细</el-divider>
        <el-table :data="form.detailList" border style="width: 100%; margin-bottom: 20px">
          <el-table-column prop="orderNo" label="订单编号" min-width="150" align="center" />
          <el-table-column prop="deliverCode" label="发货单号" min-width="150" align="center" />
          <!-- <el-table-column prop="type" label="明细类型" min-width="100" align="center" /> -->
          <el-table-column prop="amount" label="金额(元)" min-width="100" align="center">
            <template #default="{ row }"> ¥{{ row.amount != null ? Number(row.amount).toFixed(2) : '-' }} </template>
          </el-table-column>
          <el-table-column prop="orderTime" label="订单时间" min-width="150" align="center">
            <template #default="{ row }">
              <span>{{ parseTime(row.orderTime) || '-' }}</span>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <!-- 商品明细列表 -->
      <div v-if="form.productList && form.productList.length > 0">
        <el-divider content-position="left">商品清单</el-divider>
        <el-table :data="form.productList" border style="width: 100%">
          <el-table-column prop="itemName" label="商品名称" min-width="150" align="center" show-overflow-tooltip />
          <el-table-column prop="productNo" label="商品编号" min-width="120" align="center" />
          <el-table-column prop="unitPrice" label="单价" min-width="90" align="center" />
          <el-table-column prop="quantity" label="数量" min-width="90" align="center" />
          <el-table-column prop="unitName" label="单位" min-width="60" align="center" />
        </el-table>
      </div>
    </div>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="visible = false">关 闭</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, getCurrentInstance, toRefs } from 'vue';
import { ElMessage } from 'element-plus';
import { getStatementInfo } from '@/api/pc/enterprise/statement';
import type { StatementOrder, StatementDetail, StatementProduct } from '@/api/pc/enterprise/statementTypes';

const { proxy } = getCurrentInstance() as any;
const { statement_status, invoice_issuance_status, payment_status } = toRefs<any>(
  proxy?.useDict('statement_status', 'invoice_issuance_status', 'payment_status')
);

const visible = ref(false);
const loading = ref(false);
const form = ref<Partial<StatementOrder>>({});

const open = async (id: number | string) => {
  visible.value = true;
  loading.value = true;
  try {
    const res = await getStatementInfo(id);
    if (res.code === 200) {
      form.value = res.data || {};
    } else {
      ElMessage.error(res.msg || '获取详情失败');
    }
  } catch (error) {
    console.error('获取对账单详情失败:', error);
    ElMessage.error('获取对账单详情失败');
  } finally {
    loading.value = false;
  }
};

defineExpose({
  open
});
</script>

<style scoped>
.margin-bottom {
  margin-bottom: 20px;
}
.dialog-footer {
  text-align: right;
}
</style>
