<!-- 分销明细 -->
<template>
  <div>
    <div class="row">
      <div class="info_wrap">
        <div class="item">
          <div class="icon">
            <img class="img" src="@/assets/fenxiao/3.png">
          </div>
          <div class="info">
            <div>已入账金额（元）</div>
            <div>{{ multiplyAndFormat(infoObj.successAmount || 0) }}</div>
          </div>
        </div>
        <div class="item">
          <div class="icon">
            <img class="img" src="@/assets/fenxiao/4.png">
          </div>
          <div class="info">
            <div>待入账金额（元）</div>
            <div>{{ multiplyAndFormat(infoObj.pendingAmount || 0) }}</div>
          </div>
        </div>
        <div class="item">
          <div class="icon">
            <img class="img" src="@/assets/fenxiao/5.png">
          </div>
          <div class="info">
            <div>运营余额（元）</div>
            <div>{{ multiplyAndFormat(infoObj.balanceAmount || 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-select v-model="queryForm.status" placeholder="请选择状态" style="width: 200px;">
            <el-option label="已入账" value="success"></el-option>
            <el-option label="待入账" value="pending"></el-option>
            <el-option label="已退款" value="refund"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <selectUser v-model="queryForm.id" placeholder="分销员昵称/手机号" />
        </el-form-item>
        <el-form-item>
          <div class="between">
            <div class="center">
              <el-button type="primary" icon="Search" :loading="tableData.loading" @click="searchHandle">搜索</el-button>
              <el-button icon="Refresh" :loading="tableData.loading" @click="resetHandle">重置</el-button>
            </div>
            <div class="center">
              <el-button type="primary" @click="rechargeDialogRef.show()">充值</el-button>
              <el-button type="primary" plain @click="rechargeRecordDialogRef.show()">查看记录</el-button>
            </div>
          </div>
        </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="sourceNickName" 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="nickName" width="200">
          <template #default="scope">
            <div class="column">
              <div>{{ scope.row.sourceNickName }}</div>
              <div>{{ scope.row.sourcePhone }}</div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="分成来源" prop="level" min-width="80">
          <template #default="scope">
            <span v-if="scope.row.level == 1">直接分成</span>
            <span v-if="scope.row.level == 2">间接分成</span>
          </template>
        </el-table-column>
        <el-table-column label="状态" prop="status" min-width="100">
          <template #default="scope">
            <el-tag disable-transitions type="success" effect="dark" v-if="scope.row.status == 'success'">已入账</el-tag>
            <el-tag disable-transitions type="info" effect="dark" v-if="scope.row.status == 'pending'">待入账</el-tag>
            <el-tag disable-transitions type="danger" effect="dark" v-if="scope.row.status == 'refund'">已退款</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="关联订单号" prop="orderNo" min-width="200"></el-table-column>
        <el-table-column label="收益(元)" prop="rewardAmount" min-width="120">
          <template #default="scope">
            <span v-if="scope.row.status == 'refund'">-</span>
            {{ multiplyAndFormat(scope.row.rewardAmount || 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>
    <recharge_dialog ref="rechargeDialogRef" />
    <recharge_record_dialog ref="rechargeRecordDialogRef" />
  </div>
</template>

<script setup>
import dayjs from 'dayjs'
import { ref, reactive, onMounted } from 'vue'
import recharge_dialog from './recharge_dialog.vue'
import recharge_record_dialog from './recharge_record_dialog.vue'
import selectUser from '../../components/selectUser.vue'
import { distributionFlowGet } from '@/api/coupon'
import { multiplyAndFormat } from '@/utils'

const rechargeDialogRef = ref(null)
const rechargeRecordDialogRef = ref(null)

const infoObj = ref({
  balanceAmount: 0,
  pendingAmount: 0,
  successAmount: 0
})

const resetQuerForm = ref({
  id: '',
  key: '',
  startTime: '',
  endTime: '',
  status: ''
})
const queryForm = ref({ ...resetQuerForm.value })

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

function resetHandle() {
  queryForm.value = { ...resetQuerForm.value }
  times.value = []
  searchHandle()
}

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

// 选择日期
const times = ref([])
function selectTimeChange(e) {
  if (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')
  } 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 distributionFlowGet({
      page: tableData.page,
      size: tableData.size,
      ...queryForm.value
    })
    tableData.list = res.records
    tableData.total = res.totalRow

    infoObj.value.balanceAmount = res.balanceAmount
    infoObj.value.pendingAmount = res.pendingAmount
    infoObj.value.successAmount = res.successAmount
  } catch (error) {
    console.log(error);
  }
  setTimeout(() => {
    tableData.loading = false
  }, 500);
}

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

<style scoped lang="scss">
.column {
  display: flex;
  flex-direction: column;
}

.between {
  display: flex;
  justify-content: space-between;
  gap: 100px;
}

.center {
  display: flex;
}

.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;
    }
  }
}
</style>