<template>
  <div class="apps-form">
    <el-dialog
      append-to-body :title="t('marketingApps.editIp')" width="880" v-model="visible" :close-on-click-modal="false" :destroy-on-close="true"
      draggable>
      <IpCollapse :data=ips  @ips="updateIps" @delIps="updateDelIps"></IpCollapse>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="visible = false">{{ t('common.cancelButtonText') }}</el-button>
          <el-button type="primary" @click="onSubmit" :disabled="loading">{{ t('common.confirmButtonText')
          }}</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { useMessage } from '/@/hooks/message';
import { useI18n } from 'vue-i18n';
import { updateModIp } from '/@/api/marketing/apps';
const IpCollapse = defineAsyncComponent(() => import('./ipCollapse.vue'));
import { IpItem } from '../types';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);
const { t } = useI18n();

// 定义变量内容
const visible = ref(false);
const loading = ref(false);
const ips = ref<IpItem[]>([]);
const childIps = ref();
const delIps = ref<string[]>([]);
const rowData = ref();

// 打开弹窗
const openDialog = async (data: any, row) => {
  visible.value = true;
  ips.value = data;
  rowData.value = row;
};

const updateIps = (data: IpItem[])=>{
  childIps.value = data;
}
const updateDelIps = (data: string[]) => {
  delIps.value = data;
}

// 保存数据
const onSubmit = async () => {
  try {
    await updateModIp({ ips: childIps.value, delIps: delIps.value, id: rowData.value.id});
    emit('refresh');
    useMessage().success(t('marketingApps.submitSuccess'));
    visible.value = false;
  } catch (error) {
    useMessage().error(t('marketingApps.submitFail'));
    console.error(error);
  }
};

// 暴露变量 只有暴漏出来的变量 父组件才能使用
defineExpose({
  openDialog,
});
</script>
<style lang="scss">
.el-overlay {
  .el-overlay-dialog {
    .el-dialog {
      .el-dialog__body {
        padding: 0 !important;
      }
    }
  }
}
</style>