<template>
  <div>
    <el-row :gutter="16">
      <el-col :span="8" v-for="item in userList" :key="item.id">
        <people :datas="item" />
      </el-col>
    </el-row>

    <el-card class="!border-none mt-4" shadow="never">
      <el-alert
        type="warning"
        title="温馨提示：用户账户变动记录"
        :closable="false"
        show-icon
      ></el-alert>
      <el-form
        ref="formRef"
        class="mb-[-16px] mt-[16px]"
        :model="queryParams"
        :inline="true"
      >
        <el-form-item label="用户信息">
          <el-input
            class="w-[280px]"
            v-model="queryParams.user_info"
            placeholder="请输入用户编号/昵称/手机号"
            clearable
            @keyup.enter="resetPage"
          />
        </el-form-item>
        <el-form-item label="变动类型">
          <el-select class="w-[280px]" v-model="queryParams.change_type">
            <el-option label="全部" value />
            <el-option
              v-for="(value, key) in optionsData.change_type"
              :key="key"
              :label="value"
              :value="key"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="记录时间">
          <daterange-picker
            v-model:startTime="queryParams.start_time"
            v-model:endTime="queryParams.end_time"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="resetPage">查询</el-button>
          <el-button @click="resetParams">重置</el-button>
          <export-data
            class="ml-2.5"
            :fetch-fun="accountLog"
            :params="queryParams"
            :page-size="pager.size"
          />
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="!border-none mt-4" shadow="never">
      <el-table size="large" v-loading="pager.loading" :data="pager.lists">
        <el-table-column label="ID" prop="id" min-width="80" />
        <el-table-column label="用户编号" prop="user_info.sn" min-width="100" />
        <el-table-column
          label="归属公司"
          prop="company_info.company_name"
          min-width="160"
        />
        <el-table-column label="用户昵称" min-width="100">
          <template #default="{ row }">
            <div class="flex items-center">
              <image-contain
                class="flex-none mr-2"
                :src="row.user_info?.avatar"
                :width="40"
                :height="40"
                preview-teleported
                fit="contain"
              />
              {{ row.user_info?.nickname }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="角色名称"
          prop="user_info.group_name"
          min-width="160"
        />
        <el-table-column
          label="手机号码"
          prop="user_info.mobile"
          min-width="100"
        />
        <el-table-column label="变动金额" prop="change_amount" min-width="100">
          <template #default="{ row }">
            <span
              class="text-warning"
              :class="{ 'text-error': row.action == 2 }"
            >
              {{ row.change_amount }}
            </span>
          </template>
        </el-table-column>
        <el-table-column label="剩余金额" prop="left_amount" min-width="100">
          <template #default="{ row }">
            <span class="text-success">
              {{ row.left_amount }}
            </span>
          </template>
        </el-table-column>
        <el-table-column
          label="变动类型"
          prop="change_type_desc"
          min-width="120"
        >
          <template #default="{ row }">
            <span :style="{ color: getTypeColor(row.change_type) }">{{
              row.change_type_desc
            }}</span>
          </template>
        </el-table-column>

        <el-table-column label="来源单号" prop="source_sn" min-width="100" />
        <el-table-column label="记录时间" prop="create_time" min-width="140" />
      </el-table>
      <div class="flex justify-end mt-4">
        <pagination v-model="pager" @change="getLists" />
      </div>
    </el-card>
  </div>
</template>
<script lang="ts" setup name="articleLists">
import { getUmChangeType, accountLog, getCompanyUserList } from "@/api/finance";
import { useDictOptions } from "@/hooks/useDictOptions";
import { usePaging } from "@/hooks/usePaging";
import { useRoute } from "vue-router";
import people from "@/views/finance/component/people.vue";
import { apiUserRoleLists } from "@/api/user_role";

const route = useRoute();
// console.log("company_id", route.query?.company_id);

const queryParams = reactive({
  user_info: "",
  change_type: "",
  start_time: "",
  end_time: "",
  company_id: "",
});
if (route.query?.company_id) queryParams.company_id = route.query?.company_id;
if (route.query?.s_date) queryParams.start_time = route.query?.s_date;
if (route.query?.e_date) queryParams.end_time = route.query?.e_date;

const userList = ref([]);
const initUserList = async () => {
  let res = await getCompanyUserList({
    company_id: queryParams.company_id,
  });
  let role = await apiUserRoleLists({});
  res = res.map((item: any) => {
    let p = role.lists.find((e: any) => e.id == item.group_id);
    p.name ? (item.group_name = p.name) : (item.group_name = "暂无角色");
    return item;
  });
  userList.value = res;
};
initUserList();

const { pager, getLists, resetPage, resetParams } = usePaging({
  fetchFun: accountLog,
  params: queryParams,
});

const { optionsData } = useDictOptions<{
  change_type: Record<number, string>;
}>({
  change_type: {
    api: getUmChangeType,
  },
});

const getTypeColor = (type: number) => {
  let color = "#333";
  switch (type) {
    case 100:
      color = "#f56c6c";
      break;
    case 101:
      color = "#f56c6c";
      break;
    case 200:
      color = "#409eff";
      break;
    case 201:
      color = "#409eff";
      break;
    case 202:
      color = "#67c23a";
      break;
    case 203:
      color = "#e6a23c";
      break;
  }
  return color;
};

onMounted(() => {
  getLists();
});
</script>


