<!-- 开通记录 -->
<template>
  <div>
    <div class="row">
      <div class="info_wrap">
        <div class="item">
          <div class="icon">
            <img class="img" src="@/assets/fenxiao/1.png">
          </div>
          <div class="info">
            <div>支付开通人数</div>
            <div>{{ multiplyAndFormat(totalCount || 0) }}</div>
          </div>
        </div>
        <div class="item">
          <div class="icon">
            <img class="img" src="@/assets/fenxiao/2.png">
          </div>
          <div class="info">
            <div>已支付金额（元）</div>
            <div>{{ multiplyAndFormat(totalAmount || 0) }}</div>
          </div>
        </div>
      </div>
    </div>
    <div class="row mt14">
      <el-form inline>
        <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>
          <selectUser v-model="queryForm.key" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" :loading="tableData.loading" @click="searchHandle">搜索</el-button>
          <el-button icon="Refresh" :loading="tableData.loading" @click="resetHandle">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="row">
      <el-table :data="tableData.list" stripe border v-loading="tableData.loading">
        <el-table-column label="订单号" prop="orderNo" min-width="250"></el-table-column>
        <el-table-column label="用户" prop="nickName" min-width="200">
          <template #default="scope">
            <div class="column">
              <div>{{ scope.row.nickName }}</div>
              <div>{{ scope.row.phone }}</div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="支付金额" prop="changeAmount">
          <template #default="scope">
            {{ multiplyAndFormat(scope.row.changeAmount || 0) }}
          </template>
        </el-table-column>
        <el-table-column label="支付时间" prop="createTime"></el-table-column>
      </el-table>
    </div>
    <div class="row mt14">
      <el-pagination v-model:current-page="tableData.page" v-model:page-size="tableData.size"
        :page-sizes="[10, 20, 50, 100, 500]" 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, reactive, onMounted } from 'vue'
import { distributionOpenFlow } from '@/api/coupon'
import selectUser from '../../components/selectUser.vue';
import { multiplyAndFormat } from '@/utils'

const totalCount = ref(0)
const totalAmount = ref(0)

const queryForm = ref({
  key: '',
  startTime: '',
  endTime: ''
})

function searchHandle() {
  tableData.page = 1;
  getTableData()
}

function resetHandle() {
  queryForm.value.key = ''
  queryForm.value.startTime = ''
  queryForm.value.endTime = ''
  times.value = []
  searchHandle()
}

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

// 选择日期
const times = ref([])
function selectTimeChange(e) {
  if (e !== null) {
    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')
  } else {
    queryForm.value.startTime = ''
    queryForm.value.endTime = ''
  }
}

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

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

async function getTableData() {
  try {
    tableData.loading = true
    const res = await distributionOpenFlow({
      ...queryForm.value,
      page: tableData.page,
      size: tableData.size
    })
    tableData.list = res.records
    tableData.total = res.totalRow

    totalCount.value = res.totalCount
    totalAmount.value = res.totalAmount
  } catch (error) {
    console.log(error);
  }
  setTimeout(() => {
    tableData.loading = false
  }, 500);
}

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

<style scoped lang="scss">
.row {
  &.mt14 {
    margin-top: 14px;
  }
}

.info_wrap {
  display: flex;
  gap: 48px;

  .item {
    display: flex;
    align-items: center;
    border: 1px solid #D9D9D9;
    border-radius: 8px;
    padding: 0 10px;

    .icon {
      width: 44px;
      height: 44px;

      .img {
        width: 100%;
        height: 100%;
      }
    }

    .info {
      flex: 1;
      padding: 10px;
    }
  }
}

.column {
  display: flex;
  flex-direction: column;
}
</style>