<template>
  <div>
    <el-form :model="queryForm" inline>
      <el-form-item>
        <selectBranchs all :multiple="false" v-model="queryForm.shopId" />
      </el-form-item>
      <el-form-item>
        <el-date-picker style="width: 300px" v-model="times" type="daterange" range-separator="至"
          start-placeholder="开始日期" end-placeholder="结束日期" value-format="YYYY-MM-DD"
          @change="selectTimeChange"></el-date-picker>
      </el-form-item>
      <el-form-item label="" prop="">
        <el-input placeholder="请输入关键词" v-model="queryForm.key">
          <template #prepend>名称</template>
        </el-input>
      </el-form-item>
      <el-form-item label="" prop="">
        <el-button type="primary" @click="searchHandle">搜索</el-button>
        <el-button @click="resetSearch">重置</el-button>
      </el-form-item>
    </el-form>
    <div class="data_show">
      <div class="data_list">
        <div class="item">
          <div class="title">
            返现总金额
          </div>
          <div class="num">
            ￥{{ tableData.totalAmount }}
          </div>
        </div>
      </div>
    </div>
    <div class="table" style="width: 100%;">
      <el-table :data="tableData.list" stripe border v-loading="tableData.loading" height="500px">
        <el-table-column label="ID" prop="id"></el-table-column>
        <el-table-column label="关联订单号" prop="orderNo"></el-table-column>
        <el-table-column label="消费门店" prop="shopName"></el-table-column>
        <el-table-column label="用户信息" prop="nickName">
          <template #default="scope">
            <el-link type="primary" @click="router.push({
              path: '/user/index',
              query: {
                phone: scope.row.phone
              }
            })">
              <div class="column">
                <span>{{ scope.row.nickName }}</span>
                <span>{{ scope.row.phone }}</span>
              </div>
            </el-link>
          </template>
        </el-table-column>
        <el-table-column label="支付金额（元）" prop="amount"></el-table-column>
        <el-table-column label="返现金额（元）" prop="cashbackAmount"></el-table-column>
        <el-table-column label="创建时间" prop="createTime"></el-table-column>
      </el-table>
    </div>
    <div class="row" style="margin-top: 14px;">
      <el-pagination v-model:current-page="tableData.page" v-model:page-size="tableData.size"
        :page-sizes="[10, 30, 50, 100]" background layout="total, sizes, prev, pager, next, jumper"
        :total="tableData.total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
    </div>
  </div>
</template>

<script setup>
import dayjs from 'dayjs'
import { ref, onMounted } from 'vue'
import selectBranchs from '../components/selectBranchs.vue'
import { consumeCashbackRecord } from '@/api/coupon/index.js'
import { useRouter } from "vue-router";

const router = useRouter();
const times = ref([])
const queryForm = ref({
  shopId: '',
  key: '',
  startTime: '',
  endTime: ''
})

const tableData = reactive({
  totalAmount: 0,
  loading: false,
  page: 1,
  size: 10,
  list: []
})

// 搜索
function searchHandle() {
  tableData.page = 1;
  consumeCashbackRecordAjax()
}

// 重置搜索
function resetSearch() {
  queryForm.value.shopId = ''
  queryForm.value.key = ''
  queryForm.value.startTime = ''
  queryForm.value.endTime = ''

  times.value = []

  searchHandle()
}

// 选择日期
function selectTimeChange(e) {
  console.log(e);
  queryForm.value.startTime = dayjs(e[0]).format('YYYY-MM-DD 00:00:00')
  queryForm.value.endTime = dayjs(e[1]).format('YYYY-MM-DD 23:59:59')
}

// 分页大小发生变化
function handleSizeChange(e) {
  tableData.pageSize = e;
  consumeCashbackRecordAjax();
}

// 分页发生变化
function handleCurrentChange(e) {
  tableData.page = e;
  consumeCashbackRecordAjax();
}

// 获取消费返现记录
async function consumeCashbackRecordAjax(params) {
  try {
    tableData.loading = true
    const res = await consumeCashbackRecord({
      ...queryForm.value,
      page: 1,
      size: 10,
      total: 0
    })
    tableData.totalAmount = res.totalAmount
    tableData.list = res.records
    tableData.total = +res.totalRow
  } catch (error) {
    console.log(error);
  }
  setTimeout(() => {
    tableData.loading = false
  }, 500);
}

onMounted(() => {
  consumeCashbackRecordAjax()
})
</script>

<style scoped lang="scss">
.data_show {
  padding: 0 0 14px;

  .data_list {
    display: flex;

    .item {
      width: 200px;
      border: 1px solid #ddd;
      border-radius: 4px;
      padding: 14px;

      .title {
        color: #666;
      }

      .num {
        font-size: 24px;
        color: #333;
        font-weight: bold;
      }
    }
  }
}

.column {
  display: flex;
  flex-direction: column;
  line-height: 16px;
}
</style>