<template>
  <div>
    <BasicTable :rowSelection="{ type: 'checkbox' }" @register="registerTable" :clickToRowSelect="false">
      <template #toolbar>
        <Authority>
          <a-button type="primary" @click="handleCreate">新增组件</a-button>
        </Authority>
        <!-- <Authority>
          <Popconfirm title="您确定要批量删除数据" ok-text="确定" cancel-text="取消" @confirm="handleDeleteOrBatchDelete(null)">
            <a-button type="primary" color="error" :disabled="hasBatchDelete"> 批量删除 </a-button>
          </Popconfirm>
        </Authority> -->
      </template>
      <!-- <template #status="{ record }">
        <Switch :checked="record.status === 1" :loading="record.pendingStatus" checkedChildren="启用" unCheckedChildren="禁用"
          @change="(checked: boolean) => statusChange(checked, record)" />
      </template> -->
      <template #action="{ record }">
        <TableAction :actions="[
          {
            label: '浏览',
            icon: '' /**clarity:note-edit-line*/,
            // ifShow: record.servicealiasname,
            // ifShow: ((action) => action.servicealiasname),
            ifShow: record.servicealiasname,
            onClick: see.bind(null, record),
          },
          {
            label: '查看',
            icon: '' /**clarity:note-edit-line*/,
            onClick: handleEdit.bind(null, record),
          },
          {
            label: '提交审核',
            icon: '' /**clarity:note-edit-line*/,
            // ifShow: record.servicealiasname,
            // ifShow: ((action) => action.servicealiasname),
            ifShow: record.servicealiasname,
            onClick: see.bind(null, record),
          },
        ]" :drop-down-actions="[
  {
    label: '编辑',
    icon: '' /**clarity:note-edit-line*/,
    color: 'warning',
    onClick: handleEdit.bind(null, record),
  },
  {
    label: '删除',
    icon: '' /**ant-design:delete-outlined'*/,
    color: 'error',
    ifShow: record.roleType != RoleEnum.SYS_ADMIN,
    popConfirm: {
      title: '是否确认删除',
      confirm: handleDelete.bind(null, record),
    },
  },
]" />
      </template>
      <template #pdate="{ record }">
        <Tag :color="'red'">
          {{
            record.publishdate ? moment(record.publishdate).format('YYYY-MM-DD HH:mm:ss') : ''
          }}
        </Tag>
      </template>
      <template #status="{ record }">
        <Tag :style="`color:${record.status === 3 || record.status === 3 ? 'red' : ''};`">
          {{
            record.status === 0 ? '待审核' : (record.status === 1 ? '审核通过' : (record.status === 2 ? '审核不通过' : (record.status
              === 3 ? '被驳回' : '未提交')))
          }}
        </Tag>
      </template>
    </BasicTable>
    <!-- <MapDrawer @register="registerDrawer" @success="handleSuccess" /> -->
    <MapSourceModal @register="registerModal" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
import { defineComponent, nextTick, onBeforeMount } from 'vue';
import { BasicTable, useTable, TableAction } from '/@/components/Table';
import { delRole, getRoleListByPage, setRoleStatus } from '/@/api/system/system';
// import { useDrawer } from '/@/components/Drawer';
import { useModal } from '/@/components/Modal';
import MapDrawer from './MapDrawer.vue';
import MapSourceModal from './MapSourceModal.vue';
import { columns, searchFormSchema } from './map.data';
import { RoleEnum } from '/@/enums/roleEnum';
import { Authority } from '/@/components/Authority';
import { useBatchDelete } from '/@/hooks/web/useBatchDelete';
import { useMessage } from '/@/hooks/web/useMessage';
import { Switch, Popconfirm } from 'ant-design-vue';
import { list, deleteService } from '/@/api/dataAdmin/assembly';
import Moment from 'moment'

export default defineComponent({
  name: 'RoleManagement',
  components: { BasicTable, MapDrawer, MapSourceModal, TableAction, Authority, Switch, Popconfirm },
  setup() {
    //const [registerDrawer, { openDrawer }] = useDrawer();
    const [registerModal, { openModal }] = useModal();
    const [registerTable, { setProps, reload, setSelectedRowKeys }] = useTable({
      title: '地图资源列表',
      api: list,
      columns,
      formConfig: {
        labelWidth: 100,
        schemas: searchFormSchema,
      },
      useSearchForm: true,
      showTableSetting: true,
      bordered: true,
      showIndexColumn: true,
      actionColumn: {
        width: 200,
        title: '操作',
        dataIndex: 'action',
        slots: { customRender: 'action' },
        fixed: 'right',
      },
    });
    const moment = Moment
    const { createMessage } = useMessage();
    const { hasBatchDelete, handleDeleteOrBatchDelete, selectionOptions, resetSelectedRowKeys } =
      useBatchDelete(delRole, handleSuccess, setProps);
    selectionOptions.rowSelection.getCheckboxProps = (record: Recordable) => {
      // Demo:status为1的选择框禁用
      if (record.status === 1) {
        return { disabled: true };
      } else {
        return { disabled: false };
      }
    };
    nextTick(() => {
      setProps(selectionOptions);
    });

    function handleCreate() {
      // openDrawer(true, {
      //   isUpdate: false,
      // });
      openModal(true, {
        isUpdate: false,
      });
    }

    function handleEdit(record: Recordable) {
      // openDrawer(true, {
      //   record,
      //   isUpdate: true,
      // });
      openModal(true, {
        record,
        isUpdate: true,
      });
    }

    function see(e) {
      console.log(e);
      if (e.servicealiasname) window.open(`../../mapview.html?onlineIde_${e.servicealiasname}`, '_blank');
    }

    const handleDelete = async (record: Recordable) => {
      if (record?.serviceid) {
        const res = await deleteService(record?.serviceid)
        if (res?.status !== '-1') {
          reload();
          createMessage.success('删除成功！', 1)
        }
        else {
          createMessage.error('删除失败！失败原因：' + res?.message, 1)
        }
      }
    }

    function handleSuccess() {
      openModal(false, {
        isUpdate: false,
      });
      reload();
    }

    const statusChange = async (checked, record) => {
      setProps({
        loading: true,
      });
      setSelectedRowKeys([]);
      resetSelectedRowKeys();
      const newStatus = checked ? 1 : 0;
      try {
        await setRoleStatus(record.id, newStatus);
        if (newStatus) {
          createMessage.success(`启用成功`);
        } else {
          createMessage.success('禁用成功');
        }
      } finally {
        setProps({
          loading: false,
        });
        reload();
      }
    };
    onBeforeMount(async () => {

    })

    return {
      moment,
      createMessage,
      registerTable,
      // registerDrawer,
      registerModal,
      handleCreate,
      see,
      handleEdit,
      handleDelete,
      handleSuccess,
      RoleEnum,
      hasBatchDelete,
      handleDeleteOrBatchDelete,
      statusChange,
    };
  },
});
</script>
