<template>
  <div>
    <!--引用表格-->
   <BasicTable @register="registerTable"  @edit-end="handleEditEnd" @edit-cancel="handleEditCancel" :beforeEditSubmit="beforeEditSubmit">
     <!--插槽:table标题-->
      <template #tableTitle>
          <a-button type="primary" @click="handleAdd" preIcon="ant-design:plus-outlined"> 新增</a-button>
          <!-- <a-tag v-if="dataList.length>=8" color="orange" style="padding:5px;margin-left:3px">
            最多只能添加8条记录
          </a-tag>  -->
      </template>
       <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)"/>
      </template>
    </BasicTable>
    <!-- 表单区域 -->
    <PageTitleconfigModal @register="registerModal" @success="handleSuccess"></PageTitleconfigModal>
  </div>
</template>

<script lang="ts" name="page-pageTitleconfig" setup>
  import {ref, computed, unref} from 'vue';
  import {BasicTable, useTable, TableAction} from '/@/components/Table';
  import {useModal} from '/@/components/Modal';
  import { useListPage } from '/@/hooks/system/useListPage'
  import PageTitleconfigModal from './components/PageTitleconfigModal.vue'
  import {columns, searchFormSchema} from './PageTitleconfig.data';
  import {list, deleteOne, batchDelete, saveOrUpdate} from './PageTitleconfig.api';
  import { JEllipsis } from '/@/components/Form';
  const checkedKeys = ref<Array<string | number>>([]);

  
  const dataList = ref([])  

  const [registerModal, {openModal}] = useModal();
  const { prefixCls,tableContext } = useListPage({
      tableProps:{
           title: '首页配置-生产指标',
           api:list,
           columns,
           canResize:false,
           pagination:true,
           useSearchForm:false,
           showIndexColumn:true,
           formConfig: {
              schemas: searchFormSchema,
              autoSubmitOnEnter:true,
              showAdvancedButton:true,
              fieldMapToNumber: [
              ],
              fieldMapToTime: [
              ],
            },
            defSort :{
             column: 'tp',
             order: 'asc',
            },
           actionColumn: {
              width: 120,
              fixed:'right'
          },
      },
  })

  const [registerTable, {reload,getDataSource},{ rowSelection, selectedRowKeys }] = tableContext


  function handleAdd() {
     openModal(true, {
       isUpdate: false,
       showFooter: true,
     });
  }

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

  function handleDetail(record: Recordable) {

     openModal(true, {
       record,
       isUpdate: true,
       showFooter: false,
     });
   }

  async function handleDelete(record) {
    
     await deleteOne({id: record.id}, handleSuccess);
  }

  async function batchHandleDelete() {
     await batchDelete({ids: selectedRowKeys.value}, handleSuccess);
   }
   /**
    * 成功回调
    */
  async function handleSuccess() {
      await reload()
      await setDataList();
   }

  function getTableAction(record){
       return [
         {
           label: '编辑',
           onClick: handleEdit.bind(null, record),
         }
       ]
   }

  function getDropDownAction(record){
       return [
         {
           label: '详情',
           onClick: handleDetail.bind(null, record),
         }, {
           label: '删除',
           popConfirm: {
             title: '是否确认删除',
             confirm: handleDelete.bind(null, record),
           }
         }
       ]
   }

   async function setDataList() {
     const cfgList = await list({});
     dataList.value = cfgList.records
  }
  setDataList()


  async function beforeEditSubmit({ record, index, key, value }) {
    if(value) {
      record[key] = value
      await saveOrUpdate(record,true)
      reload()
    } else {
      return false
    }
  }

  function handleEditCancel() {
    console.log('cancel');
  }

  function handleEditEnd(record:Recordable) {
    //handleSuccess()
  }

</script>

<style scoped>

</style>