<template>
  <div>
    <el-form :model="searchForm" inline @submit.prevent>
      <el-form-item>
        <el-input
          v-model="searchForm.phone"
          placeholder="请输入手机号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="searchForm.createTime"
          type="daterange"
          range-separator="-"
          value-format="YYYY-MM-DD"
          format="YYYY-MM-DD"
          start-placeholder="注册日期"
          end-placeholder="注册日期"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">搜索</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="tableList" border stripe>
      <el-table-column type="index" width="50" />
      <el-table-column label="头像" width="80">
        <template #default="scope">
          <el-avatar icon="el-icon-user-solid" shape="circle" :src="scope.row.avatar" fit="fill" />
        </template>
      </el-table-column>
      <el-table-column label="昵称" prop="userName" min-width="120" />
      <el-table-column label="手机号码" prop="phone" width="120" />
      <el-table-column label="生日" prop="birthday" min-width="120" />
      <el-table-column
        label="性别"
        prop="sex"
        width="100"
        :formatter="(row) => (row.sex === 1 ? '男' : '女')"
      />
      <el-table-column label="注册日期" prop="createTime" min-width="120" />
      <el-table-column label="会员类型" prop="memberName" min-width="120" />
      <el-table-column label="会员截止日期" prop="endDate" min-width="120" />
      <!-- 操作 -->
      <el-table-column label="操作" width="120">
        <template #default="{ row }">
          <el-button type="primary" link @click="handleAddVip(row)">赠送会员</el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination
      :total="total"
      v-model:page="searchForm.pageNo"
      v-model:limit="searchForm.pageSize"
      @pagination="getList"
    />

    <el-dialog title="赠送会员" v-model="showDialog" width="500px" :close-on-click-modal="false">
      <el-form :model="form" ref="formRef" :rules="rules" label-width="80px">
        <el-form-item label="会员类型" prop="memberId">
          <el-select v-model="form.memberId" clearable filterable style="width: 100%">
            <el-option
              v-for="item in vipOptions"
              :key="item.memberId"
              :label="item.memberName"
              :value="item.memberId"
            >
              <span style="float: left">{{ item.memberName }}</span>
              <span style="float: right; color: #aaa">{{ item.carName }}</span>
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>

      <template #footer>
        <span>
          <el-button @click="showDialog = false">取消</el-button>
          <el-button type="primary" @click="sureAddVip">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script name="AppletUser" setup>
import { removeNullField } from '@/utils'
import * as UserApi from '@/api/gds/appletUser.js'
import { getVipTypeList } from '@/api/gds/appletUser'

const message = useMessage()

const searchForm = ref({
  phone: '',
  createTime: [],
  createTimeBegin: undefined,
  createTimeEnd: undefined,
  pageNo: 1,
  pageSize: 20
})

onMounted(() => {
  handleQuery()
})

/** 搜索按钮操作 */
const handleQuery = () => {
  searchForm.value.pageNo = 1
  getList()
}

const loading = ref(false)
const tableList = ref([])
const total = ref(0)
/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    if (searchForm.value.createTime && searchForm.value.createTime.length > 0) {
      searchForm.value.createTimeBegin = searchForm.value.createTime[0] + ' 00:00:00'
      searchForm.value.createTimeEnd = searchForm.value.createTime[1] + ' 23:59:59'
    } else {
      searchForm.value.createTimeBegin = undefined
      searchForm.value.createTimeEnd = undefined
    }
    const data = await UserApi.getUserPage(removeNullField(searchForm.value))
    tableList.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

const showDialog = ref(false)
const form = ref({
  phone: '',
  memberId: ''
})

const vipOptions = ref([])
const rules = {
  memberId: [{ required: true, message: '请选择会员类型', trigger: 'change' }]
}
const formRef = ref()

function handleAddVip(row) {
  getVipTypeList({ pageNo: 1, pageSize: -1 }).then((response) => {
    vipOptions.value = response.list
  })
  form.value.phone = row.phone
  showDialog.value = true
}

function sureAddVip() {
  if (!formRef.value) return
  formRef.value.validate((valid) => {
    if (!valid) return
    UserApi.addUserMember(form.value).then(() => {
      showDialog.value = false
      getList()
      message.success('操作成功')
    })
  })
}
</script>

<style lang="scss" scoped></style>
