<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="注册时间">
                    <daterange-picker
                        v-model:startTime="queryParams.create_time_start"
                        v-model:endTime="queryParams.create_time_end"
                    />
                </el-form-item>
                <el-form-item label="注册来源">
                    <el-select class="w-[280px]" v-model="queryParams.channel">
                        <el-option
                            v-for="(item, key) in ClientMap"
                            :key="key"
                            :label="item"
                            :value="key"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                    <el-button @click="dialogFormVisible=true">创建</el-button>
                </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" />
                <el-table-column label="用户编号" prop="sn" min-width="120" />
                <el-table-column label="昵称" prop="nickname" min-width="100" />
                <el-table-column label="账号" prop="account" min-width="120" />
                <el-table-column label="手机号码" prop="mobile" min-width="100" />
                <el-table-column label="是否禁用" prop="is_disable" align="center" min-width="100">
                    <template #default="{ row }">
                        <span v-if="row.is_disable == 1">是</span>
                        <span v-if="row.is_disable == 0">否</span>
                    </template>
                </el-table-column>
                <el-table-column label="注册来源" prop="channel" min-width="100" />
                <el-table-column label="注册时间" prop="create_time" min-width="120" />
                <el-table-column label="操作" width="200" align="center" fixed="right">
                    <template #default="{ row }">
                        <el-button type="primary" link>
                            <router-link
                                :to="{
                                    path: getRoutePath('farm.farm/lists'),
                                    query: {
                                        user_id: row.id
                                    }
                                }"
                            >
                                基地信息
                            </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
                                    }
                                }"
                            >
                                详情
                            </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>

      <el-dialog v-model="dialogFormVisible" title="添加用户" width="30%">
        <el-form :model="form" ref="addForm">
          <el-form-item label="手机号" prop="mobile">
            <el-input v-model="form.mobile" autocomplete="off" />
              <span>
                <el-icon><Pointer /></el-icon>
                初始密码:123456
              </span>
          </el-form-item>
        </el-form>
        <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="addUser">
          确定
        </el-button>
      </span>
        </template>
      </el-dialog>
    </div>
</template>
<script lang="ts" setup name="consumerLists">
import { usePaging } from '@/hooks/usePaging'
import { getRoutePath } from '@/router'
import { getUserList, addUser as addUserApi } from '@/api/consumer'
import { ElMessage } from 'element-plus'
import { ClientMap } from '@/enums/appEnums'
const queryParams = reactive({
    keyword: '',
    channel: '',
    create_time_start: '',
    create_time_end: ''
})

const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: getUserList,
    params: queryParams
})
onActivated(() => {
    getLists()
})

const dialogFormVisible = ref(false)
const form = reactive({
  "mobile": ""
})
const addForm = ref()
const  addUser = () => {
  addUserApi(form).then(() => {
    ElMessage({
      message: '用户添加成功',
      type: 'success',
    })
    addForm.value.resetFields()
    dialogFormVisible.value = false
    getLists()
  })
}
getLists()
</script>
