import {defineComponent, onMounted, reactive, ref} from "vue";
import {NButton, NForm, NFormItem, NFormItemGi, NGrid, NInput, NSpace, useMessage} from "naive-ui";
import {musicSheetApplicationExtendUpdate, smsConfigSave, smsConfigUpdate} from "@views/music-library/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,
      accessUrl: null,
      account: null,
      password: null,
      extendData: 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.platformName
        forms.accessUrl = state.rowData.accessUrl
        forms.account = state.rowData.accessAccount
        forms.password = state.rowData.accessPassword
        forms.extendData = state.rowData.accessExtParam
      }
    })

    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="accessUrl"
                    rule={[
                      {
                        required: true,
                        message: '请输入接入地址'
                      }
                    ]}
                >
                  <NInput
                      v-model:value={forms.accessUrl}
                      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="extendData"
                    rule={[
                      {
                        required: false,
                        message: '请输入拓展参数'
                      }
                    ]}
                >
                  <NInput
                      v-model:value={forms.extendData}
                      placeholder="请输入拓展参数"
                      autosize={{minRows: 3}}
                      type={'textarea'}
                      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>
      )
    }
  }
})