<template>
  <div>
    <el-form :model="searchForm" label-width="0" inline @submit.prevent>
      <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" v-hasPermi="['customer:resell:search']">查询</el-button>
        <el-button type="primary" @click="handleAdd" v-hasPermi="['customer:resell:add']">
          新增
        </el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="tableData" max-height="calc(100vh - 200px)">
      <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 prop="memberName" label="赠送会员" />
      <el-table-column prop="discountDiscription" label="赠送折扣" />
      <el-table-column label="小程序码" align="center" width="120px">
        <template #default="{ row }">
          <img :src="row.appletUrl" style="width: 80px; height: 80px" />
        </template>
      </el-table-column>
      <el-table-column label="操作" width="120px">
        <template #default="{ row }">
          <el-button
            type="primary"
            link
            @click="handleEdit(row)"
            v-hasPermi="['customer:resell:update']"
          >
            修改
          </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-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-item label="赠送折扣" prop="discountId">
          <el-select v-model="form.discountId" clearable filterable style="width: 100%">
            <el-option
              v-for="item in discountOptions"
              :key="item.id"
              :label="item.description"
              :value="item.id"
            />
          </el-select>
        </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">
import { getResellList, addResell, updateResell } from '@/api/xjapplet/resell'
import { getVipDiscountOptions } from '@/api/xjapplet/discount'
import { getVipTypeOptions } from '@/api/xjapplet/vip'

const message = useMessage()

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

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

const loading = ref(false)
const showDialog = ref(false)

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

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

const vipOptions = ref([])
const discountOptions = ref([])

onMounted(() => {
  searchList()
  getVipDiscountOptions().then((response) => {
    discountOptions.value = response
  })
  getVipTypeOptions({ carTypeId: searchForm.value.carTypeId }).then((response) => {
    vipOptions.value = response
  })
})

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

function getList() {
  loading.value = true
  getResellList(searchForm.value).then((response) => {
    tableData.value = response.list
    total.value = response.total
    loading.value = false
  })
}

function handleAdd() {
  showDialog.value = true
  form.value = {
    phone: '',
    name: '',
    mark: '',
    discountId: undefined,
    memberId: undefined
  }
}
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

  // 调用接口
  if (form.value.distributionId) {
    updateResell(form.value).then(() => {
      message.success('修改成功')
      showDialog.value = false
      searchList()
    })
  } else {
    addResell(form.value).then(() => {
      message.success('新增成功')
      showDialog.value = false
      searchList()
    })
  }
}
</script>

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