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

export default defineComponent({
  mpName: 'wechat-config-edit',
  props: {
    editMode: {
      type: String,
      required: true
    },
    rowData: {
      type: Object,
      required: false
    },
    appData: {
      type: Array as any,
      required: true
    }
  },
  emits: ['close', 'getList'],
  setup(props, {slots, attrs, emit}) {


    const message = useMessage()
    const btnLoading = ref(false)
    const forms = reactive({
      mpName: null, //公众号名称
      appid: null, //公众号ID
      secret: null, //接入密钥
      token: null, //消息密钥
      aeskey: null, //加密模式密钥
      appKey: null, //应用
      content: null, //关注公众号提示信息
    })
    const formsRef = ref()

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

    onMounted(async () => {
      state.rowData = props.rowData
      if (props.editMode == 'edit' && props.rowData) {
        forms.mpName = state.rowData.mpName
        forms.appid = state.rowData.appid
        forms.secret = state.rowData.secret
        forms.token = state.rowData.token
        forms.aeskey = state.rowData.aeskey
        forms.appKey = state.rowData.appKey
        forms.content = state.rowData.content
      }
    })

    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 wxConfigInfoSave(
                {
                  ...forms,
                }
            ) as any;
          } else {
            res = await wxConfigInfoUpdate(
                {
                  ...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="mpName"
                    rule={[
                      {
                        required: true,
                        message: '请输入公众号名称'
                      }
                    ]}
                >
                  <NInput
                      v-model:value={forms.mpName}
                      placeholder="请输入公众号名称"
                      clearable
                  />
                </NFormItemGi>
                <NFormItemGi
                    label="公众号ID"
                    path="appid"
                    rule={[
                      {
                        required: true,
                        message: '请输入公众号ID'
                      }
                    ]}
                >
                  <NInput
                      v-model:value={forms.appid}
                      placeholder="请输入公众号ID"
                      clearable
                  />
                </NFormItemGi>
                <NFormItemGi
                    label="接入密钥"
                    path="secret"
                    rule={[
                      {
                        required: true,
                        message: '请输入接入密钥'
                      }
                    ]}
                >
                  <NInput
                      v-model:value={forms.secret}
                      placeholder="请输入接入密钥"
                      clearable
                  />
                </NFormItemGi>
                <NFormItemGi
                    label="消息密钥"
                    path="token"
                    rule={[
                      {
                        required: true,
                        message: '请输入消息密钥'
                      }
                    ]}
                >
                  <NInput
                      v-model:value={forms.token}
                      placeholder="请输入消息密钥"
                      clearable
                  />
                </NFormItemGi>
                <NFormItemGi
                    label="加密模式密钥"
                    path="aeskey"
                    rule={[
                      {
                        required: true,
                        message: '请输入加密模式密钥'
                      }
                    ]}
                >
                  <NInput
                      v-model:value={forms.aeskey}
                      placeholder="请输入加密模式密钥"
                      clearable
                  />
                </NFormItemGi>
                <NFormItemGi
                    label="应用"
                    path="appKey"
                    rule={[
                      {
                        required: true,
                        message: '请选择应用'
                      }
                    ]}
                >
                  <NSelect
                      placeholder="请选择应用"
                      v-model:value={forms.appKey}
                      options={props.appData}
                      clearable
                  />
                </NFormItemGi>
              </NGrid>
              <NGrid cols={1}>
                <NFormItemGi
                    label="关注公众号提示信息"
                    path="content"
                    rule={[
                      {
                        required: true,
                        message: '关注公众号提示信息'
                      }
                    ]}
                >
                  <NInput
                      type={'textarea'}
                      v-model:value={forms.content}
                      placeholder="关注公众号提示信息"
                      autosize={{minRows:3}}
                      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>
      )
    }
  }
})