import {
  NForm,
  NFormItem,
  NInput,
  NSelect,
  NSpace,
  NButton,
  useMessage,
  NCascader,
  NInputNumber
} from 'naive-ui'
import type { TreeSelectOption } from 'naive-ui'
import { defineComponent, PropType, reactive, ref } from 'vue'
import { musicTagSave, musicTagUpdate } from '../../api'

export default defineComponent({
  name: 'city-operation',
  props: {
    type: {
      type: String,
      default: 'add'
    },
    data: {
      type: Object as PropType<any>,
      default: () => {}
    },
    catalogList: {
      type: Array as PropType<Array<TreeSelectOption>>,
      default: () => []
    },
    clientTypeList: {
      type: Array as PropType<Array<string>>,
      default: () => []
    }
  },
  emits: ['close', 'getList'],
  setup(props, { slots, attrs, emit }) {
    const forms = reactive({
      name: null,
      sortNumber: null
    })

    const btnLoading = ref(false)
    const formsRef = ref()
    const message = useMessage()

    // 提交记录
    const onSubmit = async () => {
      formsRef.value.validate(async (error: any) => {
        if (error) return
        try {
          btnLoading.value = true
          if (props.type === 'add') {
            await musicTagSave({ ...forms })
            message.success('添加成功')
          } else if (props.type === 'edit') {
            await musicTagUpdate({ ...forms, id: props.data.id })
            message.success('修改成功')
          }
          emit('getList')
          emit('close')
        } catch {
          //
        }
        setTimeout(() => {
          btnLoading.value = false
        }, 100)
      })
    }

    // 初始化数据
    if (props.type === 'edit') {
      const data = props.data
      forms.name = data.name
      forms.sortNumber = data.sortNumber
    }

    return () => (
      <div style="background: #fff; padding-top: 12px">
        <NForm model={forms} ref={formsRef} label-placement="left" label-width="auto">
          <NFormItem
            label="名称"
            path="name"
            rule={[
              {
                required: true,
                message: '请输入名称'
              }
            ]}
          >
            <NInput
              v-model:value={forms.name}
              placeholder="请输入名称"
              clearable
              showCount
              maxlength={6}
            />
          </NFormItem>
          <NFormItem label="排序" path="sortNumber">
            <NInputNumber
              v-model:value={forms.sortNumber}
              placeholder="请输入排序"
              min={0}
              style={{ width: '100%' }}
            />
          </NFormItem>
        </NForm>

        <NSpace justify="end">
          <NButton type="default" onClick={() => emit('close')}>
            取消
          </NButton>
          <NButton
            type="primary"
            onClick={() => onSubmit()}
            loading={btnLoading.value}
            disabled={btnLoading.value}
          >
            保存
          </NButton>
        </NSpace>
      </div>
    )
  }
})
