<template>
  <BasicModal v-bind="$attrs" @register="registerModal" :title="title" @ok="handleSubmit" width="800px" :showCancelBtn="false" :showOkBtn="false">
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <template #tableTitle>
        <a-button preIcon="ant-design:plus-outlined" type="primary" @click="handleAdd" style="margin-right: 5px" v-if="showPackAddAndEdit">新增 </a-button>
        <a-button
          v-if="selectedRowKeys.length > 0"
          preIcon="ant-design:delete-outlined"
          type="primary"
          @click="handlePackBatch"
          style="margin-right: 5px"
          >批量删除
        </a-button>
        <a-button
          preIcon="ant-design:sync-outlined"
          type="primary"
          @click="handleSyncDefaultPack"
          style="margin-right: 5px"
          >初始化默认套餐
        </a-button>
      </template>
      <template #action="{ record }">
        <TableAction :actions="getActions(record)" :dropDownActions="getDropDownAction(record)" />
      </template>
    </BasicTable>
  </BasicModal>
  <TenantPackMenuModal @register="registerPackMenu" @success="success" />
  <TenantPackUserModal @register="registerPackUser" @success="success" />
  <PackPermissionDrawer @register="registerPackPermDrawer" @success="success"/>
</template>
<script lang="ts" setup name="tenant-pack-modal">
  import { reactive, ref, unref } from 'vue';
  import { BasicModal, useModal, useModalInner } from '/@/components/Modal';
  import { packColumns, userColumns, packFormSchema } from '../tenant.data';
  import { getTenantUserList, leaveTenant, packList, deleteTenantPack, syncDefaultTenantPack } from '../tenant.api';
  import { useListPage } from '/@/hooks/system/useListPage';
  import { BasicTable, TableAction } from '/@/components/Table';
  import TenantPackMenuModal from './TenantPackMenuModal.vue';
  import {Modal} from "ant-design-vue";
  import TenantPackUserModal from './TenantPackUserModal.vue';
  import {useMessage} from "/@/hooks/web/useMessage";
  import PackPermissionDrawer from "@/views/system/tenant/pack/PackPermissionDrawer.vue";
  import { useDrawer } from "@/components/Drawer";

  const [registerPackMenu, { openModal }] = useModal();
  const [registerPackUser, { openModal: packUserOpenModal }] = useModal();
  const [registerPackPermDrawer, { openDrawer: openPackPermDrawer }] = useDrawer();
  
  const tenantId = ref<number>(0);
  // 列表页面公共参数、方法
  const { prefixCls, tableContext } = useListPage({
    designScope: 'tenant-template',
    tableProps: {
      api: packList,
      columns: packColumns,
      immediate: false,
      formConfig: {
        schemas: packFormSchema,
        labelCol: {
          xxl: 8,
        },
        actionColOptions: {
          xs: 24,
          sm: 8,
          md: 8,
          lg: 8,
          xl: 8,
          xxl: 8,
        },
      },
      beforeFetch: (params) => {
        return Object.assign(params, { tenantId: unref(tenantId), packType:'custom' });
      },
    },
  });
  const [registerTable, { reload }, { rowSelection, selectedRowKeys, selectedRows }] = tableContext;
  // Emits声明
  const emit = defineEmits(['register', 'success']);
  //是否显示新增和编辑套餐包
  const showPackAddAndEdit = ref<boolean>(false);
  //表单赋值
  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    tenantId.value = data.tenantId;
    showPackAddAndEdit.value = data.showPackAddAndEdit;
    success();
  });
  //设置标题
  const title = '租户个性化套餐包';

  //表单提交事件
  async function handleSubmit(v) {
    closeModal();
  }

  function getActions(record) {
    return [
      {
        label: '用户',
        onClick: seeTenantPackUser.bind(null, record),
      },
      {
        label: '授权',
        onClick: handleRolePrem.bind(null, record),
        ifShow: ()=>{ return showPackAddAndEdit.value }
      },
    ];
  }

  /**
   * 成功
   */
  function success() {
    (selectedRowKeys.value = []) && reload();
  }

  /**
   * 编辑
   * @param record
   */
  async function handleEdit(record) {
    openModal(true, {
      isUpdate: true,
      record: record,
      tenantId: unref(tenantId),
      packType:'custom',
      showFooter: true
    });
  }

  //默认系统套餐包不允许删除,包含(超级管理员、组织账户管理员、组织应用管理员)
  const packCode = reactive<any>(['superAdmin','accountAdmin','appAdmin']);
  const { createMessage } = useMessage();
  
  /**
   * 删除套餐包
   * @param 删除
   */
  async function handleDelete(record) {
    // 代码逻辑说明: 系统默认套餐包不允许删除------------
    if(packCode.indexOf(record.packCode) != -1){
        createMessage.warning("默认系统套餐包不允许删除");
       return;
    }
    if(record.packCode && record.packCode.indexOf("default") != -1){
      createMessage.warning("默认套餐包不允许删除");
      return;
    }
    await deleteTenantPack({ ids: record.id }, success);
  }

  /**
   * 批量删除套餐包
   */
  async function handlePackBatch() {
    let value = selectedRows.value;
    if(value && value.length>0){
      for (let i = 0; i < value.length; i++) {
        if(packCode.indexOf(value[i].packCode) != -1){
          createMessage.warning("默认系统套餐包不允许删除");
          return;
        }
        // 代码逻辑说明: 默认套餐不允许删除---
        if(value[i].packCode && value[i].packCode.indexOf("default") != -1){
          createMessage.warning("默认套餐包不允许删除");
          return;
        }
      }
    }
    Modal.confirm({
      title: '删除租户套餐包',
      content: '是否删除租户套餐包',
      okText: '确认',
      cancelText: '取消',
      onOk: async () => {
        await deleteTenantPack({ ids: selectedRowKeys.value.join(',')}, success);
      }
    })
  }

  async function handleSyncDefaultPack() {
    Modal.confirm({
      title: '初始化默认套餐包',
      content: '是否初始化默认套餐包',
      okText: '确认',
      cancelText: '取消',
      onOk: async () => {
        await syncDefaultTenantPack({tenantId: unref(tenantId)}, success);
      },
    });
  }

  /**
   *
   * 新增表单
   */
  function handleAdd() {
    openModal(true, {
      isUpdate: false,
      tenantId: unref(tenantId),
      packType:'custom',
      showFooter: true
    });
  }

  /**
   * 套餐包下面的用户
   * @param record
   */
  function seeTenantPackUser(record) {
    packUserOpenModal(true,{
      record:record
    })
  }

  /**
   * 更多
   * @param record
   */
  function getDropDownAction(record) {
    return [
      {
        label: '编辑',
        onClick: handleEdit.bind(null, record),
        ifShow: ()=>{ return showPackAddAndEdit.value }
      },
      {
        label: '详情',
        onClick: handleDetail.bind(null, record),
      },
      {
        label: '删除',
        popConfirm: {
          title: '是否确认删除租户套餐包',
          confirm: handleDelete.bind(null, record),
        },
      },
    ]
  }

  /**
   * 详情
   * @param record
   */
  function handleDetail(record) {
    openModal(true, {
      isUpdate: true,
      record: record,
      tenantId: unref(tenantId),
      packType:'custom',
      showFooter: false
    });
  }


  /**
   * 授权
   *
   * @param record
   */
  function handleRolePrem(record) {
    openPackPermDrawer(true,{
      packId: record.id,
      permissionIds: record.permissionIds
    })
  }
</script>
