<template>
  <div class="examine-container">
    <div class="left-container">
      <template v-for="source in sourceTypes" :key="source.name">
        <div
          class="soure-item-row"
          :class="{ active: current === source.name }"
          @click="current = source.name"
        >
          {{ `${source.name}(${source.num})` }}
        </div>
      </template>
    </div>
    <div class="right-container">
      <BasicTable :rowSelection="{ type: 'checkbox' }" rowKey="serviceid" @register="registerTable">
        <template #action="{ record }">
          <TableAction
            :actions="[
              {
                label: '浏览',
                icon: '' /**clarity:note-edit-line*/,
                onClick: handleEdit.bind(null, record),
              },
              {
                label: '详情',
                icon: '' /**clarity:note-edit-line*/,
                onClick: handleEdit.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>
      <MapSourceModal @register="registerModal" @success="handleSuccess" />
    </div>
  </div>
</template>
<script lang="ts">
import { defineComponent, nextTick, onBeforeMount, reactive, ref, toRef } 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 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 } from '/@/api/resource/map';
import Moment from 'moment';
import { active } from 'sortablejs';

export default defineComponent({
  name: 'RoleManagement',
  components: { BasicTable, MapSourceModal, TableAction, Authority, Switch, Popconfirm },
  setup() {
    let current = ref('地图资源');
    //const [registerDrawer, { openDrawer }] = useDrawer();
    const [registerModal, { openModal }] = useModal();
    const [registerTable, { getSelectRows, setProps, reload, setSelectedRowKeys }] = useTable({
      title: '地图资源列表',
      api: list, //求接口
      //dataSource: dataSources, //表格的数据
      columns,
      rowKey: 'serviceid',
      formConfig: {
        labelWidth: 100,
        schemas: searchFormSchema,
      },
      useSearchForm: true,
      showTableSetting: true,
      bordered: true,
      showIndexColumn: true,
      actionColumn: {
        width: 200,
        title: '操作',
        dataIndex: 'action',
        slots: { customRender: 'action' },
        fixed: 'right',
      },
      pagination: {
        hideOnSinglePage: false,
      },
      clickToRowSelect: true, //点击当前行多选框不选中,默认是true
      rowSelection: { type: 'checkbox' }, //是否有多选功能
    });
    const moment = Moment;

    const sourceTypes = ref([
      { name: '地图资源', num: 4 },
      { name: '场景资源', num: 5 },
      { name: '文件资源', num: 10 },
      { name: '组件资源', num: 2 },
    ]);
    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 handleSuccess() {
      openModal(false, {
        isUpdate: false,
      });
      reload();
    }

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

    onBeforeMount(async () => {});

    return {
      current,
      moment,
      sourceTypes,
      registerTable,
      registerModal,
      handleCreate,
      handleEdit,
      handleSuccess,
      RoleEnum,
      hasBatchDelete,
      handleDeleteOrBatchDelete,
      statusChange,
      batchExamine,
    };
  },
});
</script>
<style scoped lang="less">
.examine-container {
  display: flex;
  height: 100%;
  width: 100%;
  .left-container {
    width: 240px;
    height: calc(100% - 32px);
    margin: 16px 0 16px 10px;
    padding: 10px 20px;
    background-color: #fff;
    border-radius: 2px;
    .soure-item-row {
      height: 34px;
      width: 100%;
      margin: 20px 0;
      line-height: 34px;
      text-align: center;
      font-size: 14px;
      font-weight: normal;
      letter-spacing: 0px;
      color: #333333;
      background: #eff0f5;
      border-radius: 2px;
    }
    .active {
      background: #0671dd;
      color: #fff;
    }
  }
  .right-container {
    width: calc(100% - 260px);
    height: 100%;
  }
}
</style>

