<!-- 分销员列表 -->
<template>
  <div>
    <el-form inline>
      <el-form-item>
        <selectUser v-model="queryForm.id" />
      </el-form-item>
      <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>
        <el-button type="primary" @click="searchHandle" icon="Search" :loading="tableData.loading">搜索</el-button>
        <el-button @click="resetHandle" icon="Refresh" :loading="tableData.loading">重置</el-button>
        <el-button type="primary" plain icon="Plus" @click="addUserDialogRef.show()">添加分销员</el-button>
      </el-form-item>
    </el-form>
    <div class="row">
      <el-table :data="tableData.list" stripe border v-loading="tableData.loading">
        <el-table-column label="ID" prop="id" width="80"></el-table-column>
        <el-table-column label="用户" prop="nickName">
          <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="distributionLevelName" width="100"></el-table-column>
        <el-table-column label="总收益(元)" prop="totalIncome" width="100">
          <template #default="scope">
            {{ multiplyAndFormat(scope.row.totalIncome || 0) }}
          </template>
        </el-table-column>
        <el-table-column label="待入账金额(元)" prop="pendingIncome" width="120">
          <template #default="scope">
            {{ multiplyAndFormat(scope.row.pendingIncome || 0) }}
          </template>
        </el-table-column>
        <el-table-column label="已入账金额(元)" prop="receivedIncome" width="120">
          <template #default="scope">
            {{ multiplyAndFormat(scope.row.receivedIncome || 0) }}
          </template>
        </el-table-column>
        <el-table-column label="开通方式" prop="openingMethod" width="100"></el-table-column>
        <el-table-column label="开通时间" prop="createTime"></el-table-column>
        <el-table-column label="操作" width="300" fixed="right">
          <template #default="scope">
            <el-button link type="primary" @click="editorUserDialogRef.show(1, scope.row)"
              v-if="scope.row.isAssignLevel === 0">
              更改分销组
            </el-button>
            <el-button link type="primary" @click="editorUserDialogRef.show(3, scope.row)" v-else>
              重置分销组
            </el-button>
            <el-button link type="primary" @click="editorUserDialogRef.show(2, scope.row)"
              v-if="scope.row.status == 1">取消分销员</el-button>
            <el-button link type="primary" @click="editorUserDialogRef.show(9, scope.row)"
              v-if="scope.row.status == 9">恢复分销员</el-button>
            <el-button link type="primary" @click="distributionUserDetailRef.show(scope.row)">分销详情</el-button>
          </template>
        </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>
    <addUserDialog ref="addUserDialogRef" @success="getTableData" />
    <editorUserDialog ref="editorUserDialogRef" @success="getTableData" />
    <distributionUserDetail ref="distributionUserDetailRef" />
  </div>
</template>

<script setup>
import dayjs from 'dayjs';
import { ref, reactive, onMounted } from 'vue'
import addUserDialog from './addUserDialog.vue';
import editorUserDialog from './editorUserDialog.vue';
import distributionUserDetail from './distributionUserDetail.vue';
import selectUser from '../../components/selectUser.vue';
import { distributionUserPage } from '@/api/coupon'
import { multiplyAndFormat } from '@/utils'

const addUserDialogRef = ref(null)
const editorUserDialogRef = ref(null)
const distributionUserDetailRef = ref(null)

const resetQuerForm = ref({
  id: '',
  startTime: '',
  endTime: ''
})
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) {
  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')
}

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

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

async function getTableData() {
  try {
    tableData.loading = true
    const res = await distributionUserPage({
      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);
}

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

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

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