<!-- 变动记录弹窗 -->
<template>
  <el-dialog title="变动记录" width="1000px" v-model="visible">
    <div class="row">
      <el-form :model="queryForm" inline>
        <el-form-item>
          <el-select v-model="queryForm.type" placeholder="请选择状态" style="width: 200px;">
            <el-option v-for="item in statusList" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <selectUser v-model="queryForm.key" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="searchHandle" icon="Search" :loading="tableData.loading">搜索</el-button>
          <el-button icon="Refresh" @click="resetHandle" :loading="tableData.loading">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="row">
      <div class="info_wrap">
        <div class="item">
          <div class="icon">
            <img class="img" src="@/assets/fenxiao/5.png">
          </div>
          <div class="info">
            <div>已充值金额（元）</div>
            <div>{{ multiplyAndFormat(info.totalRecharge || 0) }}</div>
          </div>
        </div>
        <div class="item">
          <div class="icon">
            <img class="img" src="@/assets/fenxiao/6.png">
          </div>
          <div class="info">
            <div>已扣减金额（元）</div>
            <div>{{ multiplyAndFormat(info.totalSub || 0) }}</div>
          </div>
        </div>
      </div>
    </div>
    <div class="row mt14">
      <el-table :data="tableData.list" border stripe height="400" v-loading="tableData.loading">
        <el-table-column label="类型" prop="type">
          <template #default="scope">
            {{statusList.find(item => item.value == scope.row.type).label}}
          </template>
        </el-table-column>
        <el-table-column label="变动金额（元）" prop="changeAmount"></el-table-column>
        <el-table-column label="变动后金额（元）" prop="amount"></el-table-column>
        <el-table-column label="变动原因" prop="remark">
          <template #default="scope">
            <div class="column" v-if="scope.row.type == 'manual_recharge' || scope.row.type == 'manual_sub'">
              <span>{{ scope.row.remark }}</span>
              <span class="info">操作员：{{ scope.row.opAccount }}</span>
            </div>
            <span v-else>{{ scope.row.remark }}</span>
          </template>
        </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 mt14">
      <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>
  </el-dialog>
</template>

<script setup>
import { ref } from 'vue'
import { multiplyAndFormat } from '@/utils'
import { distributionFlow } from '@/api/coupon'
import selectUser from '../../components/selectUser.vue'

const visible = ref(false)

const info = ref({
  totalRecharge: 0,
  totalSub: 0
})

const statusList = ref([
  {
    value: 'manual_recharge',
    label: '手动充值'
  },
  {
    value: 'self_recharge',
    label: '自助充值'
  },
  {
    value: 'refund',
    label: '退款'
  },
  {
    value: 'manual_sub',
    label: '手动扣减'
  },
  {
    value: 'sub',
    label: '系统扣减'
  },
])

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

function resetHandle() {
  queryForm.value.type = ''
  queryForm.value.key = ''
  tableData.size = 10

  searchHandle()
}

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

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

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

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

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

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

function show() {
  visible.value = true
  getTableData()
}

defineExpose({
  show
})
</script>

<style scoped lang="scss">
.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;

  .info {
    font-size: 14px;
    color: #999;
  }
}
</style>