<!-- 地图资源上传 -->
<template>
  <div style="height: 100%; width: 100%; margin-top: 0; padding: 0;">
    <!-- 使用搜索框和表格封装组件 -->
    <BasicTable
    :rowSelection="{ type: 'checkbox' }" 
    @register="registerTable"
    :clickToRowSelect="false"
    >
    <!-- 表格右上角按钮 -->
    <template #toolbar>
        <Authority :value="authList.addAuth">
          <a-button type="primary" @click="addMethod" >新增地图</a-button>
        </Authority>
        <Authority :value="authList.deleteAuth">
          <Popconfirm
            title="您确定要批量删除数据"
            ok-text="确定"
            cancel-text="取消"
            @confirm="handleDeleteOrBatchDelete(null)"
          >
            <a-button type="primary" color="error" :disabled="hasBatchDelete"> 批量删除 </a-button>
          </Popconfirm>
        </Authority>
      </template>
      <!-- 表格操作栏 -->
      <template #action="{ record }">
        <TableAction
          :actions="[
                        {
              label: '浏览',
              auth: 'api:yt:role:saveOrUpdateRoleInfoWithMenu:update',
              icon: 'clarity:note-edit-line',//图标
              onClick: handleEdit.bind(null, record),
            },
            {
              label: '查看',
              auth: 'api:yt:role:saveOrUpdateRoleInfoWithMenu:update',
              icon: 'clarity:note-edit-line',
              onClick: handleEdit.bind(null, record),
            },
            {
              label: '编辑',
              auth: 'api:yt:role:saveOrUpdateRoleInfoWithMenu:update',
              icon: 'clarity:note-edit-line',
              onClick: handleEdit.bind(null, record),
            },
            {
              label: '删除',
              auth: 'api:yt:role:delete',
              icon: 'ant-design:delete-outlined',
              color: 'error',
              ifShow: true,
              popConfirm: {
                title: '是否确认删除',
                confirm: handleDeleteOrBatchDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <!-- 新增和编辑 -->
    <AddMethod @register="registerModal" @success="handleSuccess"></AddMethod>
  </div>
</template>

<script>
import { defineComponent, nextTick } from 'vue';
import { BasicTable, TableAction, useTable } from '/@/components/Table';
import { Authority } from '/@/components/Authority';
import { useModal } from '/@/components/Modal';
import { Popconfirm } from 'ant-design-vue';
import { delMapResource  } from '../../api/mapuploadApi';
import { getMapListByPage } from '/@/api/sys/gis';
import { columns, searchFormSchema } from './mapUploadData';
import { useDrawer } from '/@/components/Drawer';
import { useBatchDelete } from '/@/hooks/web/useBatchDelete';
import AddMethod from './AddMethod.vue'; 

export default defineComponent({
  name: 'mapUpload',
  components: { BasicTable, Authority, Popconfirm, TableAction, AddMethod },
  setup() {
    //按钮的权限控制标识
    const bottomAuthority = "api:dataresource:mapupload:";
    const authList = {
      addAuth: bottomAuthority + "mapuploadAdd",//新增权限控制
      deleteAuth: bottomAuthority + "maploadDelete",//删除权限控制
      editAuth: bottomAuthority + "mapuploadEdit",//编辑权限控制
    }
    // 新增和编辑界面
    // const [registerDrawer, { openDrawer }] = useDrawer();
    const [registerModal, { openModal: openModal }] = useModal();
    // 新增和编辑界面操作成功的回调函数
    function handleSuccess() {
      reload();
    };

    // function handleRequest(params){
    //   console.log("&&&&&"+JSON.stringify(params));
    //     const gisToken = sessionStorage.getItem('gisToken')
    //     // let directoryFormData = new FormData()
    //     // params = new FormData()
    //     // params.append('interfaceName', 'antu.space.provider.sservicebase.ServiceBaseMetaDataViewProvider')
    //     // params.append('methodName', 'getResourcePagedList')
    //     // params.append('args[]', gisToken)
    //     // params.append('args[]', {"type":"MR","fields":{"DATA":["serviceid","servicename","servicetype","servicealiasname"],"METADATA":["publisher ","publishdate"]},"pageIndex":1,"pageSize":11})
    //     // params = new FormData()
    //     return new Promise(async (res) => {
    //        const tempData = await getMapListByPage(params);
    //        console.log("***"+tempData)
    //     })
    //   }

    // 配置下方表格
    const [registerTable, { setProps, reload, setSelectedRowKeys }] = useTable({
      // 表格题目
      title: '地图资源列表',
      // 分页查询请求函数
        api: getMapListByPage,
        // 表头
        columns,
      // 查询条件框配置
      formConfig: {
        labelWidth: 120,
        schemas: searchFormSchema,
      },
      // 请求之前进行请求参数处理
      beforeFetch : (T) =>  {
        delete T.page
        delete T.pageSize
        const gisToken = sessionStorage.getItem('gisToken')
        // let directoryFormData = new FormData()
        // directoryFormData.append('interfaceName', 'antu.space.provider.sservicebase.ServiceBaseMetaDataViewProvider')
        // directoryFormData.append('methodName', 'getResourcePagedList')
        // directoryFormData.append('args[]', gisToken)
        // directoryFormData.append('args[]', {"type":"MR","fields":{"DATA":["serviceid","servicename","servicetype","servicealiasname"],"METADATA":["publisher ","publishdate"]},"pageIndex":1,"pageSize":11})
        // T = directoryFormData
        // debugger
        // console.log("请求参数1"+JSON.stringify(directoryFormData))
        // debugger
        // T['directoryFormData'] = directoryFormData
        // console.log("请求参数1"+JSON.stringify(T))
        
        let args = []
        args.push(gisToken)
        args.push('{"type":"MR","fields":{"DATA":["serviceid","servicename","servicetype","servicealiasname"],"METADATA":["publisher ","publishdate"]},"pageIndex":1,"pageSize":11}')
        T['interfaceName'] = 'antu.space.provider.sservicebase.ServiceBaseMetaDataViewProvider'
        T['methodName'] = 'getResourcePagedList'
        T['args'] = args

        // T['args[]'] =' {"type":"MR","fields":{"DATA":["serviceid","servicename","servicetype","servicealiasname"],"METADATA":["publisher ","publishdate"]},"pageIndex":1,"pageSize":11}'
        // {
        //   interfaceName: 'antu.space.provider.sservicebase.ServiceBaseMetaDataViewProvider',
        //   methodName: 'getResourcePagedList',
        //   args
        // }
        // console.log("请求参数1"+JSON.stringify(T))

      },
      // 使用额外参数
      searchInfo: { 
        // interfaceName: 'antu.space.provider.sservicebase.ServiceBaseMetaDataViewProvider',
        // methodName: 'getResourcePagedList'
       },
      useSearchForm: true,
      showTableSetting: true,
      bordered: true,
      // 表格控制序号显示
      showIndexColumn: true,
      // 表格操作栏
        actionColumn: {
          width: 200,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: 'right',
        },
      });

      // function fun(data) {
      //   delete data.page
      //   delete data.pageSize
      //   const gisToken = sessionStorage.getItem('gisToken')
      //   Reflect.set(data, 'interfaceName', 'antu.space.provider.sservicebase.ServiceBaseMetaDataViewProvider');
      //   Reflect.set(data, 'methodName', 'getResourcePagedList');
      //   Reflect.set(data, 'args[]', gisToken);
      //   Reflect.set(data, 'args[]',  {"type":"MR","fields":{"DATA":["serviceid","servicename","servicetype","servicealiasname"],"METADATA":["publisher ","publishdate"]},"pageIndex":1,"pageSize":11});
      //   console.log("11111111"+JSON.stringify(data));
      //   // data.append('f',1)
      //   // debugger
      //   console.log("11111111"+JSON.stringify(data));
      // }

      // 
      const { hasBatchDelete, handleDeleteOrBatchDelete, selectionOptions, resetSelectedRowKeys } = useBatchDelete(delMapResource, handleSuccess, setProps);
      // 
      selectionOptions.rowSelection.getCheckboxProps = (record) => {
        // Demo:status为1的选择框禁用
        if (record.status === 1) {
          return { disabled: true };
        } else {
          return { disabled: false };
        }
      };
      nextTick(() => {
        setProps(selectionOptions);
      });
      // 新增
      // function handleCreate() {
      //   openDrawer(true, {
      //     isUpdate: false,
      //   });
      // };
      function addMethod(){
        openModal(true, {
          isUpdate: false,
        });
      };
      // 编辑
      function handleEdit(record) {
        openDrawer(true, {
          record,
          isUpdate: true,
        });
      };

      return {
        registerModal,
        authList,
        registerTable,
        addMethod,
        handleEdit,
        handleSuccess,
        hasBatchDelete,
        handleDeleteOrBatchDelete,
      };

  },
})
</script>