<!-- 充值记录 -->
<template>
  <el-dialog :title="`记录（${tableRow.shopName}）`" width="1200px" v-model="visible">
    <el-form label-width="0" inline>
      <el-form-item>
        <el-date-picker v-model="dateRange" type="datetimerange" range-separator="至" start-placeholder="开始日期时间"
          end-placeholder="结束日期时间" format="YYYY-MM-DD HH:mm:ss" value-format="YYYY-MM-DD HH:mm:ss"
          @change="timeChange" />
      </el-form-item>
      <el-form-item>
        <el-select v-model="queryForm.type" placeholder="类型" style="width: 200px;">
          <el-option :label="item.label" :value="item.value" v-for="item in statusList" :key="item.value"></el-option>
        </el-select>
      </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 class="row">
      <el-table :data="tableData.list" stripe border v-loading="tableData.loading" height="500px">
        <el-table-column label="类型">
          <template #default="scope">
            {{statusList.find(item => item.value === scope.row.type).label}}
          </template>
        </el-table-column>
        <el-table-column label="变动金额" prop="expense">
          <template #default="scope">
            {{ multiplyAndFormat(scope.row.changeAmount || 0) }}
          </template>
        </el-table-column>
        <el-table-column label="变动后金额" prop="balance">
          <template #default="scope">
            {{ multiplyAndFormat(scope.row.amount || 0) }}
          </template>
        </el-table-column>
        <el-table-column label="变动原因" prop="remark"></el-table-column>
        <el-table-column label="关联账单" prop="orderNo"></el-table-column>
        <el-table-column label="创建时间" prop="createTime"></el-table-column>
      </el-table>
    </div>
    <div class="row">
      <el-pagination v-model:current-page="tableData.page" v-model:page-size="tableData.size"
        :page-sizes="[10, 20, 50, 100]" background layout="total, sizes, prev, pager, next, jumper"
        :total="tableData.total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
    </div>
  </el-dialog>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { distributionShopFlow } from '@/api/coupon'
import { multiplyAndFormat } from '@/utils'

const visible = ref(false)

const statusList = ref([
  {
    value: '',
    label: '全部'
  },
  {
    value: 'manual_recharge',
    label: '增加'
  },
  {
    value: 'manual_sub',
    label: '减少'
  },
  {
    value: 'self_recharge',
    label: '自助充值'
  },
  {
    value: 'refund',
    label: '退款'
  },
  {
    value: 'sub',
    label: '系统扣减'
  }
])
const dateRange = ref([])
const resetQueryForm = ref({
  startTime: '',
  endTime: '',
  type: '',
})
const queryForm = ref({ ...resetQueryForm.value })

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

function resetHandle() {
  dateRange.value = []
  queryForm.value = { ...resetQueryForm.value }
  searchHandle()
}

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

// 选择时间
function timeChange(e) {
  if (e == null) {
    queryForm.value.startTime = ''
    queryForm.value.endTime = ''
  } else {
    queryForm.value.startTime = e[0]
    queryForm.value.endTime = e[1]
  }
  tableData.page = 1
  // getTableData()
}

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

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

async function getTableData() {
  try {
    tableData.loading = true
    const res = await distributionShopFlow({
      shopId: tableRow.value.id,
      page: tableData.page,
      size: tableData.size,
      ...queryForm.value
    })
    tableData.list = res.records
    tableData.total = +res.totalRow
  } catch (error) {
    console.log(error);
  }
  setTimeout(() => {
    tableData.loading = false
  }, 500);
}

const tableRow = ref('')
function show(row) {
  dateRange.value = []
  queryForm.value = { ...resetQueryForm.value }
  tableRow.value = { ...row }
  visible.value = true
  getTableData()
}

defineExpose({
  show
})
</script>

<style scoped lang="scss">
.row {
  padding-top: 14px;
}
</style>