<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <PermissionBtn @btnEvent="buttonClick"></PermissionBtn>
      </template>
      <template #bodyCell="{ column, record, index }">
        <template v-if="column.key == 'LngLat'">
          {{ `${record.lng}, ${record.lat}` }}
        </template>
      </template>
    </BasicTable>
    <a-modal v-model:open="addOrUpdateModalOpen" :title="modalType == 'add'? '添加': '编辑'" :maskClosable="false" :footer="null" :destroyOnClose="true" @cancel="modalClose">
      <AddOrUpdateModal :modalType="modalType" :modalData="modalData" @closeAddOrUpdateModal="closeAddOrUpdateModal"/>
    </a-modal>
  </div>
</template>

<script setup lang="ts">
  import { ref, onMounted, reactive, watch, createVNode, unref } from 'vue';
  import { Modal, message } from 'ant-design-vue';
  import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
  import { BasicTable, useTable } from '@/components/Table';
  import { PermissionBtn } from '@/components/PermissionBtn/index';
  import { LoadAllPage, Delete, CloseMinePoint } from '@/api/illegalconstruction/miningsitemanagement'
  import { columns, searchFormSchema } from './util';
  import AddOrUpdateModal from './AddOrUpdateModal/index.vue'

  const addOrUpdateModalOpen = ref(false)
  const modalType = ref()
  const modalData = ref({})

  // 表格
  const [registerTable, { reload, getSelectRows, getPaginationRef }] = useTable({
    title: '巡查点列表',
    api: LoadAllPage,
    columns: columns,
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
    },
    showIndexColumn: false,
    rowSelection: {
      type: 'radio',
    },
    useSearchForm: true,
    bordered: true,
    showTableSetting: true,
    handleSearchInfoFn(info) {
      return info;
    },
  });

  // 按键分类
  const buttonClick = async (type) => {
    switch (type) {
      case 'btnAdd':
        modalType.value = 'add'
        addOrUpdateModalOpen.value = true
        break;
      case 'btnEdit':
        let select = getSelectRows()
        if (select.length !== 1) {
          message.warning('请选择一条数据');
          return;
        }
        console.log('getSelectRows()',getSelectRows())
        modalData.value = {...select[0]}
        modalType.value = 'update'
        addOrUpdateModalOpen.value = true
        break;
      case 'btnDelete':
        if (getSelectRows().length !== 1) {
          message.warning('请选择一条数据');
          return;
        }
        Modal.confirm({
          title: '是否确认删除?',
          icon: createVNode(ExclamationCircleOutlined),
          onCancel() {},
          onOk() {
            return Delete(getSelectRows()).then(res => {
              message.success('删除成功')
              reload()
            })
          },
        });
        break;
      case 'btnClose':
        if (getSelectRows().length !== 1) {
          message.warning('请选择一条数据');
          return;
        }
        Modal.confirm({
          title: '是否确认关闭该巡查点?',
          icon: createVNode(ExclamationCircleOutlined),
          onCancel() {},
          onOk() {
            let selectList = getSelectRows()
            let params = {
              pointid: selectList[0].id
            }
            return CloseMinePoint(params).then(res => {
              message.success('关闭成功')
              reload()
            })
          },
        });
        break
    }
  };
  const closeAddOrUpdateModal = (isReload=false) => {
    addOrUpdateModalOpen.value = false
    modalData.value = {}
    if(isReload){
      reload()
    }
  }
  const modalClose = () => {
    modalData.value = {}
  }
</script>
