<template>
  <el-dialog title="选择用户" width="850px" v-model="show" top="20px">
    <div class="app-container">
      <div class="head-container">
        <el-form :model="query" inline>
          <el-form-item label="">
            <el-input v-model="query.key" placeholder="请输入昵称或手机号"></el-input>
          </el-form-item>

          <el-form-item>
            <div class="flex gap-20">
              <el-button type="primary" @click="getTableData">搜索</el-button>
              <el-button @click="noChooseUser">不选择用户</el-button>
            </div>
          </el-form-item>
        </el-form>
      </div>

      <div class="head-container">
        <el-table :data="tableData.data" v-loading="tableData.loading" @cell-click="cellClick">
          <el-table-column label="ID" prop="id"></el-table-column>
          <el-table-column label="用户" prop="headImg" width="200px">
            <template v-slot="scope">
              <div class="user_info">
                <el-image
                  :src="scope.row.headImg"
                  style="width: 40px; height: 40px; flex-shrink: 0"
                >
                  <template #error>
                    <div class="image-slot">
                      <i class="el-icon-user"></i>
                    </div>
                  </template>
                </el-image>
                <span class="name">{{ scope.row.nickName }}</span>
                <el-tag
                  disable-transitions
                  type="primary"
                  style="margin-left: 10px"
                  v-if="scope.row.new"
                >
                  新
                </el-tag>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="手机号" prop="phone" width="160"></el-table-column>

          <el-table-column label="会员" prop="isVip" width="140">
            <template v-slot="scope">
              <el-tag type="warning" v-if="scope.row.isVip">
                会员等级{{ scope.row.memberLevelName }}
              </el-tag>
              <span v-else>否</span>
            </template>
          </el-table-column>
          <el-table-column label="余额" prop="amount">
            <template #default="scope">
              {{ multiplyAndFormat(scope.row.amount || 0) }}
            </template>
          </el-table-column>
          <el-table-column label="积分" prop="pointBalance">
            <template #default="scope">
              {{ scope.row.pointBalance || 0 }}
            </template>
          </el-table-column>
          <el-table-column label="操作" width="90" fixed="right">
            <template v-slot="scope">
              <el-button type="primary" size="small" @click="choose(scope.row)">选择</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="head-container">
        <el-pagination
          :total="tableData.total"
          :current-page="tableData.page"
          :page-size="tableData.size"
          @size-change="sizeChange"
          @current-change="paginationChange"
          layout="total, sizes, prev, pager, next, jumper"
        ></el-pagination>
      </div>
    </div>
  </el-dialog>
</template>

<script setup>
import shopUserApi from "@/api/account/shopUser";
import dayjs from "dayjs";
import { multiplyAndFormat } from "@/utils";
let cacheData = {};

const state = reactive({
  show: false,
  query: {
    key: "",
    // isVip: 1,
  },
  tableData: {
    data: [],
    page: 1,
    size: 10,
    loading: false,
    total: 0,
  },
});
const { tableData, query, show } = toRefs(state);

function timeFilter(s) {
  return dayjs(s).format("YYYY-MM-DD HH:mm:ss");
}
const emits = defineEmits(["chooseUser"]);
function cellClick(user) {
  console.log(user);
  emits("chooseUser", user);
  close();
}
function noChooseUser() {
  emits("chooseUser", null);
  close();
}
function choose(user) {
  emits("chooseUser", user);
  close();
}

function close() {
  show.value = false;
}
function open() {
  getTableData();
  show.value = true;
}

const router = useRouter();
function toPage(type) {
  const pages = {
    charge: "charge_list",
    cost: "cost_list",
  };
  router.push({
    name: pages[type],
  });
}

function sizeChange() {
  tableData.value.page = 1;
  getTableData();
}
// 切换状态
async function statusChange(e, row) {
  try {
    tableData.value.loading = true;
    const data = { ...row };
    data.status = e;
    await modityActivate(data);
    getTableData();
  } catch (error) {
    console.log(error);
    tableData.value.loading = false;
  }
}
// 重置查询
function resetHandle() {
  query.valuename = "";
  getTableData();
}
// 分页回调
function paginationChange(e) {
  tableData.value.page = e;
  getTableData();
}
async function getTableData() {
  tableData.value.loading = true;
  try {
    const res = await shopUserApi.getList({
      ...query.value,
      size: tableData.value.size,
      page: tableData.value.page,
    });
    tableData.value.loading = false;
    tableData.value.data = res.records;
    tableData.value.total = res.totalRow * 1;
  } catch (error) {
    console.log(error);
  }
}

defineExpose({
  open,
  close,
});
</script>

<style scoped lang="scss">
.user_info {
  display: flex;
  align-items: center;

  .name {
    margin-left: 10px;
  }
}

:deep(.el-input--small .el-input__inner) {
  height: 36px;
  line-height: 36px;
}

:deep(.image-slot) {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #efefef;
  font-size: 20px;
  color: #999;
}

.card {
  background-color: #f5f5f5;
  padding: 0 14px;

  .title {
    font-size: 22px;
    padding-top: 14px;
  }

  .row {
    display: flex;
    padding: 20px 0;

    .item {
      flex: 1;

      .t {
        text-align: center;
        color: #555;
      }

      .n {
        color: #000;
        font-size: 20px;
        font-weight: bold;
        padding-top: 6px;
        text-align: center;
      }
    }
  }
}

.flex {
  display: flex;
  align-items: center;
}

.gap-20 {
  gap: 20px;
}
</style>
