<!-- 分销员明细 -->
<template>
  <el-dialog title="分销员明细" width="1200px" v-model="visible">
    <div class="top_info">
      <div class="user_info">
        <el-avatar :src="rowInfo.headImg" :size="80" shape="square"></el-avatar>
        <div class="info">
          <div class="name">{{ rowInfo.shopUserName }}</div>
          <div class="phone">{{ rowInfo.shopUserPhone }}</div>
        </div>
      </div>
      <div class="info_wrap">
        <div class="item">
          <div class="icon">
            <img class="img" src="@/assets/fenxiao/6.png">
          </div>
          <div class="info">
            <div>总收益</div>
            <div>￥{{ multiplyAndFormat(rowInfo.totalIncome || 0) }}</div>
          </div>
        </div>
        <div class="item">
          <div class="icon">
            <img class="img" src="@/assets/fenxiao/7.png">
          </div>
          <div class="info">
            <div>待入账</div>
            <div>￥{{ multiplyAndFormat(rowInfo.pendingIncome || 0) }}</div>
          </div>
        </div>
        <div class="item">
          <div class="icon">
            <img class="img" src="@/assets/fenxiao/8.png">
          </div>
          <div class="info">
            <div>已入账</div>
            <div>￥{{ multiplyAndFormat(rowInfo.receivedIncome || 0) }}</div>
          </div>
        </div>
      </div>
    </div>
    <div class="row mt14">
      <tabHeader v-model="tabIndex" :list="[{ label: '邀请用户', value: 0 }, { label: '收入明细', value: 1 }]"
        @change="tabChange" />
    </div>
    <div class="row mt14">
      <el-form :model="querForm" inline>
        <el-form-item>
          <selectUser v-model="querForm.userId" />
        </el-form-item>
        <el-form-item v-if="tabIndex == 0">
          <el-select v-model="querForm.distributionLevelId" placeholder="请选择等级" style="width: 200px;">
            <el-option v-for="item in distributionLevelIdList" :key="item.id" :label="item.name"
              :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="tabIndex == 1">
          <el-select v-model="querForm.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>
          <el-button type="primary" @click="searchHandle" :loading="tableData.loading" icon="Search">搜索</el-button>
          <el-button @click="resetHandle" :loading="tableData.loading" icon="Refresh">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="row mt14">
      <!-- 邀请用户 -->
      <el-table :data="tableData.list" stripe border v-loading="tableData.loading" v-if="tabIndex == 0">
        <el-table-column label="ID" prop="shopUserId" width="80"></el-table-column>
        <el-table-column label="用户" prop="shopUserName">
          <template #default="scope">
            <div class="column">
              <div>{{ scope.row.shopUserName }}</div>
              <div>{{ scope.row.shopUserPhone }}</div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="等级" prop="levelName"></el-table-column>
        <!-- <el-table-column label="总消费金额(元)" prop="oneIncome">
          <template #default="scope">
            {{ multiplyAndFormat(scope.row.oneIncome || 0) }}
          </template>
        </el-table-column> -->
        <el-table-column label="累计收益(元)" prop="totalIncome">
          <template #default="scope">
            {{ multiplyAndFormat(scope.row.totalIncome || 0) }}
          </template>
        </el-table-column>
        <el-table-column label="邀请时间" prop="inviteTime"></el-table-column>
      </el-table>
      <!-- 收入明细 -->
      <el-table :data="tableData.list" stripe border v-loading="tableData.loading" v-if="tabIndex == 1">
        <el-table-column label="用户" prop="nickName">
          <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="status">
          <template #default="scope">
            <el-tag disable-transitions type="success" v-if="scope.row.status == 'success'">
              已入账
            </el-tag>
            <el-tag disable-transitions type="info" v-if="scope.row.status == 'pending'">
              待入账
            </el-tag>
            <el-tag disable-transitions type="danger" v-if="scope.row.status == 'refund'">
              已退款
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="关联订单号" prop="orderNo"></el-table-column>
        <el-table-column label="收益(元)" prop="rewardAmount">
          <template #default="scope">
            {{ multiplyAndFormat(scope.row.rewardAmount || 0) }}
          </template>
        </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>
  </el-dialog>
</template>

<script setup>
import { ref } from 'vue'
import tabHeader from '../../components/tabHeader.vue'
import selectUser from '../../components/selectUser.vue'
import { multiplyAndFormat } from '@/utils'
import { distributionUserInviteUser, distributionFlowGet, distributionGet } from '@/api/coupon'

const tabIndex = ref(0)

const visible = ref(false)

const rowInfo = ref('')

const resetQuerForm = ref({
  status: '',
  userId: '',
  distributionLevelId: ''
})

const querForm = ref({ ...resetQuerForm.value })

const distributionLevelIdList = ref([])

function resetHandle() {
  querForm.value = { ...resetQuerForm.value }
  searchHandle()
}

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

const statusList = ref([
  {
    value: 'pending',
    label: '待入账',
    type: 'info'
  },
  {
    value: 'success',
    label: '已入账',
    type: 'success'
  },
  {
    value: 'refund',
    label: '已退款',
    type: 'danger'
  }
])

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

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

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

function tabChange(e) {
  getTableData()
}

// 获取表格数据
async function getTableData() {
  try {
    tableData.loading = true

    let res = ''

    if (tabIndex.value == 0) {
      res = await distributionUserInviteUser({
        id: rowInfo.value.id,
        shopId: rowInfo.value.shopId,
        page: tableData.page,
        size: tableData.size,
        shopUserId: querForm.value.userId,
        distributionLevelId: querForm.value.distributionLevelId
      })
    } else {
      res = await distributionFlowGet({
        id: rowInfo.value.id,
        shopId: rowInfo.value.shopId,
        parentId: rowInfo.value.id,
        shopUserId: querForm.value.userId,
        distributionLevelId: querForm.value.distributionLevelId,
        status: querForm.value.status
      })
    }

    tableData.list = res.records
    tableData.total = res.totalRow
  } catch (error) {
    console.log(error);
  }
  setTimeout(() => {
    tableData.loading = false
  }, 500);
}

// 获取等级配置
async function distributionGetAjax() {
  try {
    const res = await distributionGet()
    distributionLevelIdList.value = res.levelConfigList
  } catch (error) {
    console.log(error);
  }
}

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

defineExpose({
  show
})
</script>

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

.top_info {
  display: flex;
  justify-content: space-between;

  .user_info {
    display: flex;
    gap: 14px;

    .info {
      display: flex;
      gap: 14px;
      flex-direction: column;
      justify-content: center;

      .name {
        font-weight: bold;
        font-size: 16px;
      }
    }
  }
}

.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>