import {defineComponent, onMounted, reactive, ref} from "vue";
import {NButton, NForm, NFormItemGi, NGrid, NInput, NSpace, useMessage} from "naive-ui";
import {smsConfigSave, smsConfigUpdate} from "@views/message/api";

export default defineComponent({
  name: 'sms-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,
      accessUrl: null,
      account: null,
      password: null,
      extendData: null,
      appId: null,
    })
    const formsRef = ref()

    const state = reactive({
      rowData: null as any,
      musicSheetCategories: [] 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.accessUrl = state.rowData.accessUrl
        forms.account = state.rowData.account
        forms.password = state.rowData.password
        forms.extendData = state.rowData.extendData
        forms.appId = state.rowData.appId
      }
    })

    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 smsConfigSave(
                {
                  ...forms,
                }
            ) as any;
          } else {
            res = await smsConfigUpdate(
                {
                  ...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="sender"
                    rule={[
                      {
                        required: true,
                        message: '请输入平台标识'
                      }
                    ]}
                >
                  <NInput
                      v-model:value={forms.sender}
                      placeholder="请输入平台标识"
                      clearable
                  />
                </NFormItemGi>
                <NFormItemGi
                    label="接入地址"
                    path="accessUrl"
                    rule={[
                      {
                        required: true,
                        message: '请输入接入地址'
                      }
                    ]}
                >
                  <NInput
                      v-model:value={forms.accessUrl}
                      placeholder="请输入接入地址"
                      clearable
                  />
                </NFormItemGi>
                <NFormItemGi
                    label="应用ID"
                    path="appId"
                    rule={[
                      {
                        required: false,
                        message: '请输入应用ID'
                      }
                    ]}
                >
                  <NInput
                      v-model:value={forms.appId}
                      placeholder="请输入应用ID"
                      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>
              </NGrid>
              <NGrid cols={1}>
                <NFormItemGi
                    label="拓展参数"
                    path="extendData"
                    rule={[
                      {
                        required: false,
                        message: '请输入拓展参数'
                      }
                    ]}
                >
                  <NInput
                      v-model:value={forms.extendData}
                      placeholder="请输入拓展参数"
                      autosize={{minRows: 3}}
                      type={'textarea'}
                  />
                </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>
      )
    }
  }
})