<template>
  <div>
    <BasicTable  @register="registerTable" :searchInfo="searchInfo">
      <template #toolbar>
        <PermissionBtn @btn-event="onBtnClicked" />
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'streetname'">
          <TableAction
            :actions="[
              {
                label: record.streetname,
                onClick: handlePreViewData.bind(null, record),
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>

	<div class="data-preview-container" v-if="showRecordList">
		<a-tabs  hide-add type="editable-card" @edit="onEdit">
			<a-tab-pane v-for="pane in panes" :key="pane.key" :tab="pane.title" :closable="pane.closable">
				{{ pane.content }}
			</a-tab-pane>
		</a-tabs>
		<RecordList></RecordList>
		<div class="data-preview-container-option">
			<div @click="handleCloseRecordList()">
				<MinusOutlined />
			</div>
			<div @click="handleCloseRecordList()">
				<CloseOutlined />
			</div>
		</div>
	</div>
  </div>
</template>
<script lang="ts" setup>
  import { ref,reactive } from 'vue';

  import { BasicTable, useTable, TableAction } from '@/components/Table';
  import { getStatisticalList, deleteRole } from '@/api/demo/system';

	// import {DataPreivew} from './RecordList.vue'

  import { useMessage } from '@/hooks/web/useMessage';
  import { useModal } from '@/components/Modal';
  import { RecordList } from './page';

  import { PermissionBtn } from '@/components/PermissionBtn/index';

  import { columns, columnsDataPreview,searchFormSchema } from './statistical.data';
  import {MinusOutlined,CloseOutlined} from '@ant-design/icons-vue'

  defineOptions({ name: 'RoleManagement' });

  const { createConfirm, createMessage } = useMessage();
  const [registerModal, { openModal: openRoleModal }] = useModal();
  const [registerModulesModal, { openModal: openModulesModal }] = useModal();
  const [registerAccountModal, { openModal: openAccountModal }] = useModal();
  const searchInfo = reactive<Recordable>({});

  const showRecordList = ref<boolean>(false);

  const panes = reactive<{ title: string; content: string; key: string; closable?: boolean}[]>([
	{ title: '东蒙镇-24h为核查', content: '', key: '1' },
	{ title: '大田庄乡-违法', content: '', key: '2' },
	{ title: '费城街道-伪变化', content: '', key: '3' }
  ])
  const [registerTable, { reload, getSelectRows, clearSelectedRowKeys }] = useTable({
    // 表格名称
    title: '统计报表',
    // 获取数据的接口
    api: getStatisticalList,
    // 表单列信息 BasicColumn[]
    columns,
    rowKey: 'id',
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
    },
    // 使用搜索表单
    useSearchForm: true,
    // 显示表格设置工具
    showTableSetting: true,
    // 是否显示表格边框
    bordered: true,
    // 序号列
    showIndexColumn: false,
    // 勾选列
    rowSelection: false,
    // 搜索
    handleSearchInfoFn(info) {
      return info;
    },
  });


  const [registerTableDataPreview,{ reloadDataPreview, getSelectRowsDataPreview, clearSelectedRowKeysDataPreview }] = useTable({
    // 表格名称
    title: '',
    // 获取数据的接口
    api: getStatisticalList,
    // 表单列信息 BasicColumn[]
    columnsDataPreview,
    rowKey: 'id',
    formConfig: {
      labelWidth: 120,
    //   schemas: searchFormSchema,
    },
    // 使用搜索表单
    useSearchForm: false,
    // 显示表格设置工具
    showTableSetting: false,
    // 是否显示表格边框
    bordered: true,
    // 序号列
    showIndexColumn: false,
    // 勾选列
    rowSelection: false,
    // 搜索
    handleSearchInfoFn(info) {
      return info;
    },
  });
  function handleCloseRecordList(){
	showRecordList.value = false;
  }
  function handleCreate() {
    openRoleModal(true, {
      isUpdate: false,
    });
  }
  function viewAccount(record: Recordable) {
    openAccountModal(true, {
      record,
    });
  }

  function handlePreViewData(record:Recordable){
	showRecordList.value = true;
	console.log(record)
  }

  function handleEdit() {
    let rows = getSelectRows();
    if (rows.length == 0) {
      return createMessage.warn('请勾选一个角色进行编辑');
    }
    const record = rows[0];
    openRoleModal(true, {
      record,
      isUpdate: true,
    });
  }

  async function handleDelete() {
    let rows = getSelectRows();
    if (rows.length == 0) {
      return createMessage.warn('请勾选一个角色进行删除');
    }
    const query = [rows[0].id];
    createConfirm({
      iconType: 'info',
      title: '删除',
      content: '确定要删除当前角色吗',
      onOk: async () => {
        const data = await deleteRole(query);
        if (data) {
          handleSuccess();
          return createMessage.success('删除成功');
        } else {
          return createMessage.error('删除失败');
        }
      },
    });
  }

  function handlePrint(){

  }

  function handleExport(){
	createMessage.success("数据导出成功！")
  }

  function handleSuccess() {
    clearSelectedRowKeys();
    reload();
	reloadDataPreview();
  }
  function onBtnClicked(domId) {
    switch (domId) {
      case 'btnAdd':
        handleCreate();
        break;
      case 'btnEdit':
        handleEdit();
        break;
      case 'btnDelete':
        handleDelete();
        break;
      case 'btnModules':
        let rows = getSelectRows();
        if (rows.length == 0) {
          return createMessage.warn('请勾选一个角色进行编辑');
        }
        const record = rows[0];
        openModulesModal(true, {
          record,
        });
        break;
	  case 'btnPrint':
		handlePrint();
		break;
      case 'btnExport':
	  	handleExport();
		break;
      default:
        break;
    }
  }
</script>
<style>
.data-preview-container{
	width:100%;
	height: calc( 100% - 30px);
	position:absolute;
	padding:30px 0px;
	top:0px;
	left:0px;
}
.data-preview-container-option{
	width:80px;
	height: 40px;
	position:absolute;
	top:30px;
	right:0px;
}
.data-preview-container-option div{
	width:40px;
	height:40px;
	line-height:40px;
	float:left;
	text-align: center;
	cursor:pointer;
}
</style>
