<template>
  <Modal
    :maskClosable="false"
    width="45vw"
    title="编辑"
    @cancel="close"
    @ok="save"
    visible
    cancelText="取消"
    okText="确定"
  >
    <Form layout="vertical" :model="inputData" ref="formRef">
      <FormItem
        label="kafka地址"
        name="address"
        :rules="[
          {
            max: 64,
            message: '最多输入64个字符'
          }
        ]"
      >
        <Input v-model:value="inputData.address" placeholder="请输入kafka地址" />
      </FormItem>
      <FormItem
        label="topic"
        name="topic"
        :rules="[
          {
            max: 64,
            message: '最多输入64个字符'
          }
        ]"
      >
        <Input v-model:value="inputData.topic" />
      </FormItem>
      <FormItem label="状态">
        <Switch checked-children="启用" un-checked-children="启用" v-model:checked="inputData.status" />
      </FormItem>
    </Form>
  </Modal>
</template>

<script lang="ts" setup>
  import { saveOutputData } from '../../../../../../api/rule-engine/config';
  import { Form, message, Modal, FormItem, Switch, Input } from 'linker-ui-components';
  import { ref, reactive, watchEffect } from 'vue';

  const useForm = Form.useForm;
  const formRef = ref();
  const Myprops = defineProps({
    data: {
      default: ''
    }
  });
  let inputData = reactive({
    status: false,
    address: '',
    topic: ''
  });
  watchEffect(() => {
    inputData.status = Myprops.data?.data?.state?.value === 'enabled' ? true : false;
    inputData.address = Myprops.data?.data?.config?.config?.address;
    inputData.topic = Myprops.data?.data?.config?.config?.topic;
  });

  const close = () => {
    emit('closeModel');
  };
  const save = () => {
    formRef.value.validateFields().then(() => {
      saveOutputData({
        config: {
          sourceType: 'kafka',
          config: {
            ...inputData,
            state: inputData?.status ? 'enabled' : 'disable'
          }
        },
        state: inputData?.status ? 'enabled' : 'disable',
        id: Myprops?.data?.data?.id,
        sourceType: 'kafka',
        exchangeType: 'consume'
      }).then((res) => {
        if (res.status === 200) {
          message.success('操作成功');
          emit('saveSuc');
        }
      });
    });
  };
  const emit = defineEmits(['closeModel', 'saveSuc']);
</script>
<style lang="less" scoped></style>
