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

export default defineComponent({
  name: 'push-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, // 平台名称
      accessAPP: null, // 接入应用
      clientId: null, // 客户端
      accessUrl: null, // 接入地址
      pushEvn: null, // 推送环境
      accessKey: null, // 接入key
      accessSecret: null, // 接入密钥
      extendData: null, // 拓展参数
    })
    const formsRef = ref()

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

    onMounted(async () => {
      state.rowData = props.rowData
      if (props.editMode == 'edit' && props.rowData) {
        forms.name = state.rowData.platformName
        forms.accessAPP = state.rowData.accessAPP
        forms.clientId = state.rowData.clientId
        forms.accessUrl = state.rowData.accessUrl
        forms.pushEvn = state.rowData.pushEvn
        forms.accessKey = state.rowData.accessKey
        forms.accessSecret = state.rowData.accessSecret
        forms.extendData = state.rowData.extendData
      }

      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.id + ''})
        })
      }
    })

    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="accessAPP"
                    rule={[
                      {
                        required: true,
                        message: '请输入接入应用'
                      }
                    ]}
                >
                  <NInput
                      v-model:value={forms.accessAPP}
                      placeholder="请输入接入应用"
                      clearable
                  />
                </NFormItemGi>
                <NFormItemGi
                    label="客户端"
                    path="clientId"
                    rule={[
                      {
                        required: true,
                        message: '请选择客户端'
                      }
                    ]}
                >
                  <NSelect
                      v-model:value={forms.clientId}
                      options={state.appData}
                      placeholder="请选择客户端"
                      clearable
                  />
                </NFormItemGi>
                <NFormItemGi
                    label="接入地址"
                    path="accessUrl"
                    rule={[
                      {
                        required: true,
                        message: '请输入接入地址'
                      }
                    ]}
                >
                  <NInput
                      v-model:value={forms.accessUrl}
                      placeholder="请输入接入地址"
                      clearable
                  />
                </NFormItemGi>
                <NFormItemGi
                    label="推送环境"
                    path="pushEvn"
                    rule={[
                      {
                        required: true,
                        message: '请输入推送环境'
                      }
                    ]}
                >
                  <NInput
                      v-model:value={forms.pushEvn}
                      placeholder="请输入推送环境"
                      clearable
                  />
                </NFormItemGi>
                <NFormItemGi
                    label="接入key"
                    path="accessKey"
                    rule={[
                      {
                        required: true,
                        message: '请输入接入key'
                      }
                    ]}
                >
                  <NInput
                      v-model:value={forms.accessKey}
                      placeholder="请输入接入key"
                      clearable
                  />
                </NFormItemGi>
                <NFormItemGi
                    label="接入密钥"
                    path="accessSecret"
                    rule={[
                      {
                        required: true,
                        message: '请输入接入密钥'
                      }
                    ]}
                >
                  <NInput
                      v-model:value={forms.accessSecret}
                      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>
      )
    }
  }
})