import {
  NForm,
  NFormItem,
  NInput,
  NSelect,
  NSpace,
  NButton,
  useMessage,
  NDatePicker
} from 'naive-ui'
import type { TreeSelectOption } from 'naive-ui'
import { defineComponent, onMounted, PropType, reactive, ref } from 'vue'
import { sysNoticeSave, sysNoticeUpdate } from '../api'
import dayjs from 'dayjs'
import Editor from '@/components/editor'

export default defineComponent({
  name: 'ad-operation',
  props: {
    type: {
      type: String,
      default: 'add'
    },
    data: {
      type: Object as PropType<any>,
      default: () => {}
    },
    clientTypeList: {
      type: Array as PropType<Array<TreeSelectOption>>,
      default: () => []
    }
  },
  emits: ['close', 'getList'],
  setup(props, { slots, attrs, emit }) {
    const forms = reactive({
      title: null,
      catalogType: null,
      // releaseTime: null as any,
      content: null,
      status: false
    })

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

    // 提交记录
    const onSubmit = async () => {
      formsRef.value.validate(async (error: any) => {
        if (error) return
        try {
          let { ...res } = forms

          btnLoading.value = true
          if (props.type === 'add') {
            await sysNoticeSave({ ...res })
            message.success('添加成功')
          } else if (props.type === 'edit') {
            await sysNoticeUpdate({ ...res, id: props.data.id })
            message.success('修改成功')
          }
          emit('getList')
          emit('close')
        } catch {
          //
        }
        setTimeout(() => {
          btnLoading.value = false
        }, 100)
      })
    }

    // onMounted(async () => {
    // 初始化数据
    if (props.type === 'edit') {
      const data = props.data
      forms.title = data.title
      forms.content = data.content
      forms.catalogType = data.catalogType
      forms.status = data.status
      // forms.releaseTime = data.releaseTime ? dayjs(data.releaseTime).valueOf() : null
    }
    // })

    return () => (
      <div style="background: #fff; padding-top: 12px">
        <NForm model={forms} ref={formsRef} label-placement="left" label-width="auto">
          <NFormItem
            label="客户端"
            path="catalogType"
            rule={[
              {
                required: true,
                message: '请选择客户端'
              }
            ]}
          >
            <NSelect
              options={props.clientTypeList}
              placeholder="请选择客户端"
              v-model:value={forms.catalogType}
            />
          </NFormItem>
          <NFormItem
            label="公告标题"
            path="title"
            rule={[
              {
                required: true,
                message: '请输入公告标题'
              }
            ]}
          >
            <NInput
              v-model:value={forms.title}
              placeholder="请输入公告标题"
              clearable
              showCount
              maxlength={15}
            />
          </NFormItem>

          {/* <NFormItem label="发布时间" path="releaseTime">
            <NDatePicker
              v-model:value={forms.releaseTime}
              type="datetime"
              placeholder="请选择发布时间"
              clearable
              style={{ width: '100%', textAlign: 'left' }}
              isDateDisabled={(ts: number) => {
                return ts < dayjs(dayjs().format('YYYY-MM-DD')).valueOf()
              }}
            />
          </NFormItem> */}
          <NFormItem
            label="内容"
            path="content"
            rule={[
              {
                required: true,
                message: '请输入内容',
                trigger: ['change', 'blur']
              }
            ]}
          >
            <Editor v-model:value={forms.content} bucketName="news-info" />
          </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>
    )
  }
})
