import {defineComponent, onMounted, reactive, ref} from "vue";
import {NButton, NForm, NFormItemGi, NGrid, NInput, NSelect, NSpace, useMessage} from "naive-ui";
import {smsConfigSave, smsConfigUpdate, sysMessageConfigSave, sysMessageConfigUpdate} from "@views/message/api";
import {getSelectDataFromObj} from "@/utils/objectUtil";
import {clientType, messageSenderFunctionModule} from "@/utils/constant";

export default defineComponent({
  name: 'message-template-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({
      clientId: null,
      content: null,
      contentExample: null,
      description: null,
      group: null,
      params: null,
      triggerCondition: 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.clientId = state.rowData.clientId
        forms.content = state.rowData.content
        forms.contentExample = state.rowData.contentExample
        forms.description = state.rowData.description
        forms.group = state.rowData.group
        forms.params = state.rowData.params
        forms.triggerCondition = state.rowData.triggerCondition
      }
    })

    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 sysMessageConfigSave(
                {
                  ...forms,
                }
            ) as any;
          } else {
            res = await sysMessageConfigUpdate(
                {
                  ...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="clientId"
                    rule={[
                      {
                        required: false,
                        message: '请选择客户端'
                      }
                    ]}
                >
                  <NSelect
                      placeholder="请选择客户端"
                      v-model:value={forms.clientId}
                      options={getSelectDataFromObj(clientType)}
                      filterable
                  />
                </NFormItemGi>
                <NFormItemGi
                    label="功能模块"
                    path="group"
                    rule={[
                      {
                        required: true,
                        message: '请选择功能模块'
                      }
                    ]}
                >
                  <NSelect
                      placeholder="请选择功能模块"
                      v-model:value={forms.group}
                      options={getSelectDataFromObj(messageSenderFunctionModule)}
                      filterable
                  />
                </NFormItemGi>
                <NFormItemGi
                    label="参数"
                    path="params"
                    rule={[
                      {
                        required: false,
                        message: '请输入参数'
                      }
                    ]}
                >
                  <NInput
                      v-model:value={forms.params}
                      placeholder="请输入参数"
                      clearable
                  />
                </NFormItemGi>
                <NFormItemGi
                    label="触发条件"
                    path="triggerCondition"
                    rule={[
                      {
                        required: false,
                        message: '请输入触发条件'
                      }
                    ]}
                >
                  <NInput
                      v-model:value={forms.triggerCondition}
                      placeholder="请输入触发条件"
                      clearable
                  />
                </NFormItemGi>
              </NGrid>
              <NGrid cols={1}>
                <NFormItemGi
                    label="消息内容"
                    path="content"
                    rule={[
                      {
                        required: true,
                        message: '请输入消息内容'
                      }
                    ]}
                >
                  <NInput
                      v-model:value={forms.content}
                      placeholder="请输入消息内容"
                      type={'textarea'}
                      autosize={{minRows: 3}}
                  />
                </NFormItemGi>
                <NFormItemGi
                    label="消息内容示例"
                    path="contentExample"
                    rule={[
                      {
                        required: false,
                        message: '请输入消息内容示例'
                      }
                    ]}
                >
                  <NInput
                      v-model:value={forms.contentExample}
                      placeholder="请输入消息内容示例"
                      type={'textarea'}
                      autosize={{minRows: 3}}
                  />
                </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>
      )
    }
  }
})