<template>
  <div>
    <el-form :model="searchForm" label-width="0" inline>
      <el-form-item>
        <el-input v-model="searchForm.phone" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item>
        <el-input v-model="searchForm.name" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item>
        <el-button @click="searchList">查询</el-button>
        <el-button type="primary" @click="handleAdd">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="tableData">
      <el-table-column type="index" width="50" />
      <el-table-column prop="phone" label="手机号" />
      <el-table-column prop="name" label="姓名" />
      <el-table-column prop="mark" label="标记" />
      <el-table-column label="小程序码" align="center" width="140px">
        <template #default="{ row }">
          <img :src="row.qrCode" style="width: 100px; height: 100px" />
        </template>
      </el-table-column>
      <el-table-column label="操作" width="120px">
        <template #default="{ row }">
          <el-button type="primary" link @click="handleEdit(row)">修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="searchForm.pageNo"
      v-model:limit="searchForm.pageSize"
      @pagination="getList"
    />

    <el-dialog title="分销配置" v-model="showDialog" width="500px">
      <el-form :model="form" ref="formRef" :rules="rules" label-width="80px">
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" maxlength="11" />
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="标记" prop="mark">
          <el-input v-model="form.mark" />
        </el-form-item>
      </el-form>

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

<script setup name="Resell">
const message = useMessage()

const searchForm = ref({
  pageNo: 1,
  pageSize: 10,
  phone: '',
  name: ''
})

const tableData = ref([])
const total = ref(0)

const showDialog = ref(false)

const form = ref({
  phone: '',
  name: '',
  mark: ''
})

const rules = {
  phone: [{ required: true, message: '请输入手机号', trigger: 'blur' }],
  name: [{ required: true, message: '请输入姓名', trigger: 'blur' }]
}

function searchList() {
  searchForm.value.pageNo = 1
  getList()
}

function getList() {
  console.log(searchForm.value)
}

function handleAdd() {
  showDialog.value = true
}
function handleEdit(row) {
  form.value = { ...row }
  showDialog.value = true
}

const formRef = ref(null)
async function handleSave() {
  if (!formRef.value) return
  const valid = await formRef.value.validate()
  if (!valid) return

  // 调用接口
  showDialog.value = false
  message.success('成功')
  searchList()
}
</script>

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