import {defineComponent, onMounted, reactive, ref} from "vue";
import {NButton, NForm, NFormItemGi, NGrid, NInput, NSelect, NSpace, useMessage} from "naive-ui";
import {sysApplicationPage} from "@views/menu-manage/api";
import {emailConfigSave, emailConfigUpdate} from "@views/message/api";

export default defineComponent({
  mpName: 'email-config-edit',
  props: {
    editMode: {
      type: String,
      required: true
    },
    rowData: {
      type: Object,
      required: false
    },
  },
  emits: ['close', 'getList'],
  setup(props, {slots, attrs, emit}) {
    const message = useMessage()
    const btnLoading = ref(false)
    const forms = reactive({
      name: null, //平台名称
      sender: null, //三方服务厂商
      smtpPort: null, //端口
      account: null, //账号
      password: null, //密码
      from: null, //密码
      fromName: null, //发送方名称
      appKey: null, //应用端
      hostUrl: null, //域名
    })
    const formsRef = ref()

    const state = reactive({
      rowData: null as any,
      appData: [] as any,
    })

    onMounted(async () => {
      state.rowData = props.rowData
      if (props.editMode == 'edit' && props.rowData) {
        forms.name = state.rowData.name
        forms.sender = state.rowData.sender
        forms.smtpPort = state.rowData.smtpPort
        forms.account = state.rowData.account
        forms.password = state.rowData.password
        forms.from = state.rowData.from
        forms.fromName = state.rowData.fromName
        forms.appKey = state.rowData.appKey
        forms.hostUrl = state.rowData.hostUrl
      }
      // 应用
      {
        state.appData = []
        const {data} = await sysApplicationPage({page: 1, rows: 999})
        if (data && data.rows) {
          data.rows.forEach((item: any) => {
            state.appData.push({label: item.appName, value: item.appKey})
          })
        }
      }


    })

    const onSubmit = async () => {
      formsRef.value.validate(async (error: any) => {
        if (error) return false
        btnLoading.value = true
        try {
          let res;
          if (props.editMode == 'add') {
            res = await emailConfigSave(
                {
                  ...forms,
                }
            ) as any;
          } else {
            res = await emailConfigUpdate(
                {
                  ...forms,
                  id: state.rowData.id
                }
            ) as any;
          }
          if (res && res.code === 200) {
            emit('close')
            emit('getList')
          }
        } catch (error) {
        }
        btnLoading.value = false
      })
    }

    return () => {
      return (
          <div style="background: #fff; padding-top: 12px">
            <NForm
                ref={formsRef}
                labelPlacement="top"
                model={forms}
                label-placement="left"
                label-width="100"
            >
              <NGrid cols={2}>
                <NFormItemGi
                    label="平台名称"
                    path="name"
                    rule={[
                      {
                        required: true,
                        message: '请输入平台名称'
                      }
                    ]}
                >
                  <NInput
                      v-model:value={forms.name}
                      placeholder="请输入平台名称"
                      clearable
                  />
                </NFormItemGi>
                <NFormItemGi
                    label="域名"
                    path="hostUrl"
                    rule={[
                      {
                        required: true,
                        message: '请输入域名'
                      }
                    ]}
                >
                  <NInput
                      v-model:value={forms.hostUrl}
                      placeholder="请输入域名"
                      clearable
                  />
                </NFormItemGi>
                <NFormItemGi
                    label="端口"
                    path="smtpPort"
                    rule={[
                      {
                        required: true,
                        message: '请输入端口'
                      }
                    ]}
                >
                  <NInput
                      v-model:value={forms.smtpPort}
                      placeholder="请输入端口"
                      clearable
                  />
                </NFormItemGi>
                <NFormItemGi
                    label="用户名"
                    path="account"
                    rule={[
                      {
                        required: true,
                        message: '请输入用户名'
                      }
                    ]}
                >
                  <NInput
                      v-model:value={forms.account}
                      placeholder="请输入用户名"
                      clearable
                  />
                </NFormItemGi>
                <NFormItemGi
                    label="密码"
                    path="password"
                    rule={[
                      {
                        required: true,
                        message: '请输入密码'
                      }
                    ]}
                >
                  <NInput
                      v-model:value={forms.password}
                      placeholder="请输入密码"
                      clearable
                  />
                </NFormItemGi>
                <NFormItemGi
                    label="发送方"
                    path="from"
                    rule={[
                      {
                        required: true,
                        message: '请输入发送方'
                      }
                    ]}
                >
                  <NInput
                      v-model:value={forms.from}
                      placeholder="请输入发送方"
                      clearable
                  />
                </NFormItemGi>
                <NFormItemGi
                    label="发送方名称"
                    path="fromName"
                    rule={[
                      {
                        required: true,
                        message: '请输入发送方名称'
                      }
                    ]}
                >
                  <NInput
                      v-model:value={forms.fromName}
                      placeholder="请输入发送方名称"
                      clearable
                  />
                </NFormItemGi>
                <NFormItemGi
                    label="接入应用"
                    path="appKey"
                    rule={[
                      {
                        required: true,
                        message: '请选择接入应用'
                      }
                    ]}
                >
                  <NSelect
                      v-model:value={forms.appKey}
                      placeholder="请选择接入应用"
                      options={state.appData}
                      clearable
                  />
                </NFormItemGi>
              </NGrid>
            </NForm>

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