import {defineComponent, onMounted, reactive, ref} from "vue";
import {NButton, NForm, NFormItem, NFormItemGi, NGrid, NInput, NSelect, NSpace, useDialog, useMessage} from "naive-ui";
import {musicSheetApplicationExtendUpdate, smsAppConfigUpdate, smsConfigRemove, smsConfigSave, smsConfigStatus, smsConfigUpdate, ssmsAppConfigSave} from "@views/music-library/api";
import {sysApplicationPage} from "@views/menu-manage/api";

export default defineComponent({
  name: 'sms-config-app-edit',
  props: {
    editMode: {
      type: String,
      required: true
    },
    smsConfigId: {
      type: Number,
      required: true
    },
    rowData: {
      type: Object,
      required: false
    },
    appData: {
      type: Array as any,
      required: true
    }
  },
  emits: ['close', 'getList'],
  setup(props, {slots, attrs, emit}) {
    const btnLoading = ref(false)
    const forms = reactive({
      appKey: null as any,
      sign: 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.appKey = state.rowData.appKey
        forms.sign = state.rowData.sign
        forms.extendData = state.rowData.extendData
      }
    })

    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 ssmsAppConfigSave(
                {
                  ...forms,
                  sysConfigId: props.smsConfigId
                }
            ) as any;
          } else {
            res = await smsAppConfigUpdate(
                {
                  ...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="appKey"
                    rule={[
                      {
                        required: true,
                        message: '请输入平台名称'
                      }
                    ]}
                >
                  <NSelect
                      placeholder="请选择平台"
                      v-model:value={forms.appKey}
                      options={props.appData}
                      filterable
                  />
                </NFormItemGi>
                <NFormItemGi
                    label="签名"
                    path="sign"
                    rule={[
                      {
                        required: true,
                        message: '请输入签名'
                      }
                    ]}
                >
                  <NInput
                      v-model:value={forms.sign}
                      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>
      )
    }
  }
})