<template>
  <div>
    <BasicTable
      @register="registerTable"
      @selection-change="handleSelectionChange"
    >
      <template #expandedRowRender="{ record }">
        <BasicTable :searchInfo="{ concernFanId: record.id }" @register="pushTypeRegisterTable">
          <template #bodyCell="{ column, record }">
            <template v-if="column.key === 'action'">
              <TableAction :actions="[
                {
                  label: '编辑',
                  icon: 'fa6-regular:pen-to-square',
                  onClick: handlePushTypeEdit.bind(null, record)
                }]"
              />
            </template>
          </template>
        </BasicTable>
      </template>
      <template #toolbar>
        <a-button
          type="primary"
          :disabled="state.multiple"
          @click="handleDel()"
        >取消关注</a-button>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                label: '查看',
                icon: 'fa6-regular:pen-to-square',
                onClick: handleViewEdit.bind(null, record)
              },
              {
                label: '推送消息',
                icon: 'fa6-regular:message',
                ifShow: ![SystemUserType.ENTERPRISE_USER, SystemUserType.INTERNAL_USER].includes(record.userType),
                onClick: handleSend.bind(null, record)
              }]"
            :dropDownActions="[
              {
                label: '取消关注',
                icon: 'ant-design:delete-outlined',
                color: 'error',
                disabled: record?.cfType == PushConcernType.INTERNAL_COMPANY,
                onClick: handleDel.bind(null, record)
              }]"
          />
        </template>
      </template>
    </BasicTable>
    <ConcernModal @register="registerModal" @success="handleRefreshTable"/>
    <TypeModal @register="pushTypeRegisterModal" @success="handleRefreshPushTypeTable"/>
    <ConcernSendModal @register="concernSendRegisterModal"/>
  </div>
</template>

<script lang="ts" setup>
  import { reactive, toRaw } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { listPushConcernFan, delPushConcernFan } from '/@/api/platform/common/controller/pushConcernFan';
  import { useModal } from '/@/components/Modal';
  import ConcernModal from './ConcernModal.vue';
  import { columns, pushTypeColumns, searchFormSchema } from './concern.data';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useUserStore } from '/@/store/modules/user';
  import { listPushConcernFanType } from '/@/api/platform/common/controller/pushConcernFanType';
  import { searchFormSchema as pushTypeSearchFormSchema } from '/@/views/common/push/pushType/type.data';
  import TypeModal from '../pushType/TypeModal.vue';
  import ConcernSendModal from './ConcernSendModal.vue';
  import { PushConcernType, PushAuditStatus, PushConcernDynamicJoinType } from '/@/enums/pushEnum';
  import { SystemUserType } from '/@/enums/permissionEnum';


  const userStore = useUserStore();
  const userInfoStore = userStore.getUserInfo;
  console.log(userInfoStore.userType)
  interface TableState {
    single: boolean;
    multiple: boolean;
  }

  const state = reactive<TableState>({
    // 非单个禁用
    single: true,
    // 非多个禁用
    multiple: true
  });
  const { createConfirm, createMessage } = useMessage();
  const [registerModal, { openModal }] = useModal();
  const [pushTypeRegisterModal, { openModal: pushTypeOpenModal }] = useModal();
  const [concernSendRegisterModal, { openModal: concernSendTypeOpenModal }] = useModal();
  const [registerTable, { reload, clearSelectedRowKeys, getSelectRowKeys }] = useTable({
    title: '关注列表',
    api: listPushConcernFan,
    rowKey: 'id',
    columns,
    formConfig: {
      compact: true,
      labelWidth: 80,
      schemas: searchFormSchema,
      autoSubmitOnEnter: true,
      showAdvancedButton: true,
      autoAdvancedLine: 3,
    },
    rowSelection: { type: 'checkbox' },
    useSearchForm: true,
    showTableSetting: true,
    bordered: true,
    clickToRowSelect: false,
    showIndexColumn: false,
    actionColumn: {
      width: 220,
      title: '操作',
      dataIndex: 'action',
      fixed: false
    },
    searchInfo: {
      dynamicJoinType: PushConcernDynamicJoinType.CONCERN_TYPE,
      status: PushAuditStatus.APPROVED,
      fanUserId: userInfoStore.id
    },
    handleSearchInfoFn: () => clearSelectedRowKeys()
  });
  const [pushTypeRegisterTable, { reload: pushTypeReload, setColumns }] = useTable({
    title: '推送类型列表',
    api: listPushConcernFanType,
    rowKey: 'id',
    columns: pushTypeColumns(handleRefreshPushTypeTable),
    formConfig: {
      compact: true,
      labelWidth: 80,
      schemas: pushTypeSearchFormSchema,
      autoSubmitOnEnter: true,
      showAdvancedButton: true,
      autoAdvancedLine: 3,
    },
    maxHeight: 150,
    useSearchForm: true,
    showTableSetting: true,
    bordered: true,
    clickToRowSelect: false,
    showIndexColumn: false,
    actionColumn: {
      width: 220,
      title: '操作',
      dataIndex: 'action',
      fixed: false
    }
  });

  /** 处理多选框选中数据 */
  function handleSelectionChange(selection?: Recordable) {
    const rowSelection = toRaw(selection?.keys) || [];
    state.single = rowSelection.length != 1;
    state.multiple = !rowSelection.length;
  }

  /** 查看按钮操作,行内查看 */
  function handleViewEdit(record?: Recordable) {
    record = record || { id: getSelectRowKeys() };
    openModal(true, { _tag: 'view', record, dynamicJoinType: PushConcernDynamicJoinType.CONCERN_TYPE });
  }

  function handlePushTypeEdit(record?: Recordable) {
    record = record || { id: getSelectRowKeys() };
    pushTypeOpenModal(true, { _tag: 'concernEdit', record, onlineRingtoneCreateId: record.concernUserId });
  }

  /** 删除按钮操作,行内删除 */
  async function handleDel(record?: Recordable) {
    const ids = record?.id || getSelectRowKeys();
    createConfirm({
      iconType: 'warning',
      title: '警告',
      content: `是否确认取消编号为${ids}的数据?`,
      onOk: async () => {
        await delPushConcernFan(ids);
        createMessage.success('取消成功!');
        handleRefreshTable();
      }
    });
  }

  function handleSend(record?: Recordable) {
    concernSendTypeOpenModal(true, { record });
  }

  /** 处理表格刷新 */
  function handleRefreshTable() {
    clearSelectedRowKeys();
    reload();
  }

  function handleRefreshPushTypeTable() {
    pushTypeReload();
  }

</script>
