<script setup lang="ts">
import { ref } from "vue";
import Modal from "./components/modal.vue";
import contentConfig from "./config/content.config";
import searchConfig from "./config/search.config";
import { usePageSearch, type PageHooks, type PageEvents } from "/@/hooks/page";
import { httpAdd, httpUpdate } from "/@/api/serviceParam/paymentChannelManage";

import { ElMessage } from "element-plus"

const PageName = "workCompany";
const instance = ref<any>(null);

const disabled = ref(false)
const visible = ref(false)
const data = ref<any>({})

const hooks: PageHooks = {
  pageSearchHook: () => usePageSearch(undefined, undefined, searchConfig)
};

const events: PageEvents = {
  content: {
    create: () => {
      disabled.value = false
      visible.value = true
    },
    preview: _data => {
      data.value = {..._data}
      disabled.value = true
      visible.value = true
    },
    update: _data => {
      data.value = {..._data}
      disabled.value = false
      visible.value = true
    }
  }
};

async function onSubmit() {
  const { id, channel_name, companyNo } = data.value;
  const isCreate = id === undefined
  console.log(isCreate)
  const params = { id, channel_name,companyNo  }
  if(isCreate) delete params['id']
  const api = isCreate ? httpAdd : httpUpdate
  const result = await api(params)
  if(result.code === 0){
    visible.value = false
    ElMessage.success(`${isCreate ? '添加' : '更新'}成功`)
    instance.value!.onSearch()
  }else{
    ElMessage.warning(`${isCreate ? '添加' : '更新'}失败`)
  }
}
</script>

<template>
  <PageAuth :pageName="PageName">
    <PageContainer
      :hooks="hooks"
      :events="events"
      :contentConfig="contentConfig"
      :search-config="searchConfig"
      :get-content-ref="ref => (instance = ref)"
    />
    <Modal v-model:visible="visible" v-model:data="data" :disabled="disabled" @submit="onSubmit"  />
  </PageAuth>
</template>
