<template>
  <div>
    <el-card class="!border-none" shadow="never">
      <el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true">
        <el-form-item label="用户信息">
          <el-input class="w-[280px]" v-model="queryParams.keyword" placeholder="用户编号/昵称/手机号码" clearable
            @keyup.enter="resetPage" />
        </el-form-item>
        <el-form-item label="时间段">
          <el-date-picker v-model="queryTime" type="daterange" unlink-panels range-separator="-" start-placeholder="开始时间"
            end-placeholder="结束时间" @change="changeTime" value-format="YYYY-MM-DD" />
        </el-form-item>
        <el-form-item label="省" >
          <el-select class="w-[280px]" v-model="queryParams.province" placeholder="请选择省" clearable @change="province_change"
            >
            <el-option v-for="(item, index) in datas.provinceOptions" :key="index" :label="item.province_name"
              :value="item.province_code"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="市">
          <el-select class="w-[280px]" v-model="queryParams.city" placeholder="请选择市" clearable @change="city_change"
           >
            <el-option v-for="(item, index) in datas.cityOptions" :key="index" :label="item.city_name"
              :value="item.city_code"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="区县" >
          <el-select class="w-[280px]" v-model="queryParams.area" placeholder="请选择区县" clearable @change="area_change"
            >
            <el-option v-for="(item, index) in datas.areaOptions" :key="index" :label="item.area_name"
              :value="item.area_code"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="乡镇" >
          <el-select class="w-[280px]" v-model="queryParams.street" placeholder="请选择乡镇" clearable @change="street_change"
            >
            <el-option v-for="(item, index) in datas.streetOptions" :key="index" :label="item.street_name"
              :value="item.street_code"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="村社" >
          <el-select class="w-[280px]" v-model="queryParams.village" placeholder="请选择村社" clearable @change="village_change"
           >
            <el-option v-for="(item, index) in datas.villageOptions" :key="index" :label="item.village_name"
              :value="item.village_code"></el-option>
          </el-select>
        </el-form-item>
      
      
      
        <el-form-item>
          <el-button type="primary" @click="resetPage">查询</el-button>
          <el-button @click="resetParams">重置</el-button>
          <el-button type="primary" @click="exportUerListsFn">导出</el-button>
          <router-link :to="{
            path: getRoutePath('user.user/edit'),
          }">
            <el-button v-perms="['user.user/edit']" class="ml-2.5" type="primary"> 创建
            </el-button>

          </router-link>

        </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="100" />
        <el-table-column label="用户编号" prop="sn" min-width="120" />
        <el-table-column label="头像" min-width="100">
          <template #default="{ row }">
            <el-avatar :src="row.avatar" :size="50" />
          </template>
        </el-table-column>
        <el-table-column label="账号" prop="account" min-width="120" />
        <el-table-column label="姓名" prop="nickname" min-width="100" />
        <el-table-column label="联系方式" prop="mobile" min-width="120" />
        <el-table-column label="隶属公司" prop="company_name" min-width="180" align="center">
          <template #default="{ row }">
            {{ row.company?.company_name || "/" }}
          </template>
        </el-table-column>
        <el-table-column label="所在乡镇" prop="street_name" min-width="120" />
        <el-table-column label="授权身份" prop="role_name" min-width="120">
        </el-table-column>
        <el-table-column label="操作" min-width="300" align="center" fixed="right">
          <template #default="{ row }">
            <el-button v-perms="['user.user/detail']" type="primary" link>
              <router-link :to="{
                path: getRoutePath('user.user/detail'),
                query: {
                  id: row.id,
                  mode: 'detail'
                },
              }">
                详情
              </router-link>
            </el-button>
            <el-button v-perms="['user.user/detail']" type="primary" link>
              <router-link :to="{
                path: getRoutePath('user.user/detail'),
                query: {
                  id: row.id,
                  mode: 'edit'
                },
              }">
                编辑
              </router-link>
            </el-button>
          </template>
        </el-table-column>
      </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="consumerLists">
import { usePaging } from "@/hooks/usePaging";
import { getRoutePath } from "@/router";
import { getUserList, exportUerLists } from "@/api/consumer";
import {
  apiCityList,
  apiAreaList,
  apiStreetList,
  apiProvinceList,
  apiVillageList,
  apiBrigadeList
} from "@/api/common";

const queryTime = ref('')
const changeTime = () => {
  queryParams.create_time_start = queryTime.value[0]
  queryParams.create_time_end = queryTime.value[1]
}
const datas: any = reactive({
  provinceOptions: [],
  cityOptions: [],
  areaOptions: [],
  streetOptions: [],
  villageOptions:[],
  brigadeOptions:[],
});

const queryParams = reactive({
  keyword: "",
  create_time_start: "",
  create_time_end: "",
  province:"",
  city:"",
  area:"",
  street:"",
  village:"",
});

//获取省份
function province_change(value: string) {
  getCityList();
}
function city_change(value: string) {
  getAreaList();
}
function area_change(value: string) {
  getStreetList();
}
function street_change(value: string) {
  getVillageList()
}
function village_change(value: string) {
}

const getProvinceList = async () => {
  const data = await apiProvinceList({});
  datas["provinceOptions"] = data;
};
const getCityList = async () => {
  const data = await apiCityList({ city: queryParams.province });
  datas["cityOptions"] = data;
};
const getAreaList = async () => {
  const data = await apiAreaList({ area: queryParams.city });
  datas["areaOptions"] = data;
};
const getStreetList = async () => {
  const data = await apiStreetList({ street: queryParams.area });
  datas["streetOptions"] = data;
};
const getVillageList = async () => {
  const data = await apiVillageList({ village: queryParams.street });
  datas["villageOptions"] = data;

  // datas["streetOptions"] = data;
};



const { pager, getLists, resetPage, resetParams } = usePaging({
  fetchFun: getUserList,
  params: queryParams,
});
getLists();
getProvinceList()
//导出
const exportUerListsFn = () => {
  exportUerLists({ ...queryParams })
}
</script>
<style scoped>
h1 {
  text-align: center;
  font-weight: bold;
  font-size: 30px;
  color: red;
  margin-bottom: 10px;
}

.content {
  font-size: 20px;
}

.info {
  color: red;
  font-weight: bold;
  font-size: 18px;
  display: inline-block;
  margin: 0 5px;
}

.btn_menu {
  margin-top: 10vh;
  display: flex;
  justify-content: space-around;
}
</style>
