<template>
  <div>
    <el-row :gutter="16" class="summary-row">
      <el-col :span="6">
        <el-statistic title="今日收款总额" :value="summary.total_amount" prefix="¥" :precision="2" />
      </el-col>
      <el-col :span="6">
        <el-statistic title="现金收款" :value="summary.cash_amount" prefix="¥" :precision="2" />
      </el-col>
      <el-col :span="6">
        <el-statistic title="免密放行" :value="summary.free_amount" prefix="¥" :precision="2" />
      </el-col>
      <el-col :span="6">
        <el-statistic title="收款笔数" :value="summary.total_count" />
      </el-col>
    </el-row>

    <el-form inline style="margin-top:16px">
      <el-form-item :label="$t.value.LicensePlateNo">
        <el-input v-model="queryForm.plate_number" :placeholder="$t.value.carPlateInput" clearable />
      </el-form-item>
      <el-form-item label="停车场">
        <el-select v-model="queryForm.parking_lot_id" clearable placeholder="全部" style="width:160px">
          <el-option v-for="lot in lotList" :key="lot.ID" :label="lot.lot_name" :value="lot.ID" />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t.value.paymentMethod">
        <el-select v-model="queryForm.payment_method" clearable :placeholder="$t.value.pleaseSelect" style="width:140px">
          <el-option label="现金" value="cash" />
          <el-option label="免密" value="free" />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t.value.EntryTime">
        <el-date-picker v-model="dateRange" type="daterange" start-placeholder="开始" end-placeholder="结束" value-format="YYYY-MM-DD" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="fetchList">{{ $t.value.Search }}</el-button>
        <el-button @click="resetQuery">{{ $t.value.Reset }}</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="list" height="605" border>
      <el-table-column label="停车场" prop="parking_lot" align="center" width="120" />
      <el-table-column :label="$t.value.licensePlate" prop="plate_number" align="center" width="120" />
      <el-table-column :label="$t.value.VehicleType" prop="vehicle_type" align="center" width="100" />
      <el-table-column :label="$t.value.paymentMethod" align="center" width="100">
        <template #default="{ row }">
          <el-tag :type="row.payment_method === 'cash' ? '' : 'success'">{{ row.payment_method === 'cash' ? '现金' : '免密' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column :label="$t.value.Fee" prop="amount" align="center" width="100">
        <template #default="{ row }">¥{{ row.amount.toFixed(2) }}</template>
      </el-table-column>
      <el-table-column label="实收" align="center" width="100">
        <template #default="{ row }">¥{{ (row.paid_amount || 0).toFixed(2) }}</template>
      </el-table-column>
      <el-table-column label="找零" align="center" width="100">
        <template #default="{ row }">¥{{ (row.change_amount || 0).toFixed(2) }}</template>
      </el-table-column>
      <el-table-column :label="$t.value.EntryTime" align="center" width="170">
        <template #default="{ row }">{{ row.entry_time }}</template>
      </el-table-column>
      <el-table-column :label="$t.value.ExitTime" align="center" width="170">
        <template #default="{ row }">{{ row.exit_time }}</template>
      </el-table-column>
      <el-table-column label="操作员" prop="operator_name" align="center" width="100" />
      <el-table-column label="收款时间" align="center" width="170">
        <template #default="{ row }">{{ row.paid_at }}</template>
      </el-table-column>
    </el-table>

    <el-pagination
      v-model:current-page="queryForm.page"
      :page-size="queryForm.page_size"
      :total="total"
      layout="total, prev, pager, next"
      style="margin-top:12px; justify-content:flex-end"
      @current-change="fetchList"
    />
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { getPaymentList, getTodaySummary } from '@/api/payment'
import { obtainPullOverList } from '@/api/pullOver'

const queryForm = reactive({ plate_number: '', payment_method: '', parking_lot_id: null, page: 1, page_size: 10, start_date: '', end_date: '' })
const dateRange = ref([])
const list = ref([])
const total = ref(0)
const lotList = ref([])
const summary = reactive({ total_amount: 0, cash_amount: 0, free_amount: 0, total_count: 0 })

onMounted(() => { fetchList(); fetchSummary(); loadLotList() })

function loadLotList() {
  obtainPullOverList({ page: 1, pageSize: 100 }).then(res => {
    if (res.code === 0) lotList.value = res.data?.list || []
  })
}

function fetchSummary() {
  getTodaySummary().then(res => {
    if (res.code === 0) Object.assign(summary, res.data)
  })
}

function fetchList() {
  if (dateRange.value && dateRange.value.length === 2) {
    queryForm.start_date = dateRange.value[0]
    queryForm.end_date = dateRange.value[1]
  } else {
    queryForm.start_date = ''
    queryForm.end_date = ''
  }
  getPaymentList(queryForm).then(res => {
    if (res.code === 0) {
      list.value = res.data.list || []
      total.value = res.data.total || 0
    }
  })
}

function resetQuery() {
  queryForm.plate_number = ''
  queryForm.payment_method = ''
  queryForm.parking_lot_id = null
  queryForm.page = 1
  dateRange.value = []
  fetchList()
}
</script>

<style scoped>
.summary-row { margin-bottom: 16px; }
.summary-row .el-statistic { background: #f5f7fa; padding: 16px; border-radius: 8px; }
</style>
