<script setup lang="ts">
import { jsonClone } from '@sa/utils'
import { computed, ref, watch } from 'vue'
import { enableStatusOptions } from '@/constants/business'
import { useFormRules, useNaiveForm } from '@/hooks/common/form'

defineOptions({
  name: 'TemplateOperateDrawer',
})

const props = defineProps<Props>()

const emit = defineEmits<Emits>()

interface Props {
  operateType: NaiveUI.TableOperateType
  rowData?: Api.SystemManage.User | null
}

interface Emits {
  (e: 'submitted'): void
}

const visible = defineModel<boolean>('visible', {
  default: false,
})

const { formRef, validate, restoreValidation } = useNaiveForm()
const { defaultRequiredRule } = useFormRules()

const title = computed(() => {
  const titles: Record<NaiveUI.TableOperateType, string> = {
    add: '新增模板',
    edit: '编辑模板',
  }
  return titles[props.operateType]
})

type Model = Pick<
  Api.SystemManage.User,
  'userName' | 'userGender' | 'nickName' | 'userPhone' | 'userEmail' | 'userRoles' | 'status'
>

const model = ref(createDefaultModel())

function createDefaultModel(): Model {
  return {
    userName: '',
    userGender: null,
    nickName: '',
    userPhone: '',
    userEmail: '',
    userRoles: [],
    status: null,
  }
}

type RuleKey = Extract<keyof Model, 'userName' | 'status'>

const rules: Record<RuleKey, App.Global.FormRule> = {
  userName: defaultRequiredRule,
  status: defaultRequiredRule,
}

function handleInitModel() {
  model.value = createDefaultModel()

  if (props.operateType === 'edit' && props.rowData) {
    Object.assign(model.value, jsonClone(props.rowData))
  }
}

function closeDrawer() {
  visible.value = false
}

async function handleSubmit() {
  await validate()
  // request
  window.$message?.success('更新成功')
  closeDrawer()
  emit('submitted')
}

watch(visible, () => {
  if (visible.value) {
    handleInitModel()
    restoreValidation()
  }
})

const competitionOptions = [
  { label: '第九届阅读之星大赛', value: '1' },
  { label: '科普阅读大赛', value: '2' },
]

const sizeOptions = [
  { label: '210mmx297mm', value: '1' },
  { label: 'A3', value: '2' },
]
</script>

<template>
  <NDrawer v-model:show="visible" display-directive="show" :width="360">
    <NDrawerContent :title="title" :native-scrollbar="false" closable>
      <NForm ref="formRef" :model="model" :rules="rules">
        <NFormItem label="名称" path="userName">
          <NInput v-model:value="model.userName" placeholder="请输入名称" />
        </NFormItem>
        <NFormItem label="模板ID" path="nickName">
          <NInput v-model:value="model.nickName" placeholder="请输入模板ID" />
        </NFormItem>
        <NFormItem label="关联比赛" path="userGender">
          <NSelect
            v-model:value="model.userGender"
            :options="competitionOptions"
            placeholder="请选择关联比赛"
          />
        </NFormItem>
        <NFormItem label="设计尺寸" path="userPhone">
          <NSelect
            v-model:value="model.userPhone"
            :options="sizeOptions"
            placeholder="请选择设计尺寸"
          />
        </NFormItem>
        <NFormItem label="状态" path="status">
          <NRadioGroup v-model:value="model.status">
            <NRadio v-for="item in enableStatusOptions" :key="item.value" :value="item.value" :label="item.label" />
          </NRadioGroup>
        </NFormItem>
      </NForm>
      <template #footer>
        <NSpace :size="16">
          <NButton @click="closeDrawer">
            取消
          </NButton>
          <NButton type="primary" @click="handleSubmit">
            确认
          </NButton>
        </NSpace>
      </template>
    </NDrawerContent>
  </NDrawer>
</template>

<style scoped></style>
