<script setup lang="ts">
import { ref } from "vue";
import { usePageSearch, type PageHooks, type PageEvents } from "/@/hooks/page";
import RoleModal from "./components/role-modal.vue";
import contentConfig from "./config/content.config";
import { httpSetRole } from "/@/api/system/account";
import { useAsync } from "/@/hooks/core/useAsync";
import searchConfig from "./config/search.config";
import { ElMessage, ElMessageBox } from "element-plus";
import { useRenderIcon } from "/@/components/ReIcon/src/hooks";
import { ModalTypes } from "./types";


import { httpWxDel } from "/@/api/system/accountRole";

const PageName = "wechatManager";
const roleModalRef = ref<InstanceType<typeof RoleModal> | null>(null);
const instance = ref(null);

const searchCallback = (params: any) => ({
  result: { ...(params ? params : {}) },
  deleteProp: []
});

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

const { run } = useAsync({ success: () => instance.value?.onSearch() });

const events: PageEvents = {
  content: {
    create: () => roleModalRef.value?.onDisplay(undefined, ModalTypes.create),
    preview: data => roleModalRef.value?.onDisplay(data, ModalTypes.preview),
    update: data => roleModalRef.value?.onDisplay(data, ModalTypes.update)
  }
};

async function delWx(id) { 
  ElMessageBox.confirm("是否确认解除绑定?").then(async () => { 
    const result = await httpWxDel({ id })
    if (result.code === 1) { instance.value?.onSearch() }
  })
}

function handleSave(data: any, type: ModalTypes) {
  const { roleid, id } = data;

  const params = {
    status: "1",
    user_id: id,
    role_id: roleid
  };

  switch (type) {
    case ModalTypes.update:
      run(httpSetRole(params));
      break;
  }
}
</script>

<template>
  <PageAuth :pageName="PageName">
    <PageContainer
      :hooks="hooks"
      :events="events"
      :contentConfig="contentConfig"
      :get-content-ref="ref => (instance = ref)"
    ><template #content_action="{ is_wx, id }">
      <el-tooltip content="解除微信绑定">
         <el-button v-if="Number(is_wx) === 1" type="text" size="mini" :icon="useRenderIcon('close')" @click="delWx(id)"/>
      </el-tooltip>
    </template>
  </PageContainer>
    <RoleModal ref="roleModalRef" @refresh="instance.onSearch()" />
  </PageAuth>
</template>
