<template>
  <div>
    <!--引用表格-->
    <BasicTable
      @register="registerTable"
      @edit-end="handleEditEnd"
      @edit-cancel="handleEditCancel"
      :beforeEditSubmit="beforeEditSubmit"
      :rowSelection="rowSelection"
    >
      <!--插槽:table标题-->
      <template #tableTitle>
        <a-button type="primary" @click="handleAdd" preIcon="ant-design:plus-outlined"> 新增</a-button>
        <a-button type="primary" @click="handleCaluculatAllMetric" preIcon="ant-design:plus-outlined">计算所有指标值</a-button>
        <a-button type="primary" @click="handleCustomImport" preIcon="ant-design:plus-outlined">从EXCEL导入</a-button>
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <template #overlay>
            <a-menu>
              <a-menu-item key="1" @click="batchHandleDelete">
                <Icon icon="ant-design:delete-outlined"></Icon>
                删除
              </a-menu-item>
            </a-menu>
          </template>
          <a-button
            >批量操作
            <Icon icon="mdi:chevron-down"></Icon>
          </a-button>
        </a-dropdown>
      </template>

      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)" />
      </template>
      <!--字段回显插槽-->
      <template #htmlSlot="{ text }">
        <div v-html="text"></div>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'tolVal'"> 【{{ record.stTolVal }}~{{ record.tolVal }}】 </template>
        <template v-if="column.key === 'warnVal'"> ({{ record.tolVal }}~{{ record.warnVal }}） </template>
        <template v-if="column.key === 'intvVal'"> 【{{ record.warnVal }}~{{ record.intvVal }}】 </template>
      </template>
    </BasicTable>
    <!-- 表单区域 -->
    <MetricMonitorSetFomulaModal @register="registerModal" @success="handleSuccess"></MetricMonitorSetFomulaModal>
    <!-- <MetricFomulasModal @register="registerFomulasModal" @success="handleSuccess"></MetricFomulasModal> -->
  </div>
  <Import ref="refImport" @callback="importCallback"></Import>
</template>

<script lang="ts" name="metric-metricMonitorSet" 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 MetricMonitorSetFomulaModal from './components/MetricMonitorSetFomulaModal.vue';
  import { columns, searchFormSchema, formSchema } from './MetricMonitorSet.data';
  import { listCombined, deleteOne, batchDelete, caluculatAllMetric, saveOrUpdate, batchImmportCalculate } from './MetricMonitorSet.api';
  import Import from '/@/views/audit/common/components/import.vue';

  const [registerModal, { openModal }] = useModal();
  const [registerFomulasModal, { openModal: openFomulasModal }] = useModal();

  const refImport = ref();

  //注册table数据
  const { tableContext } = useListPage({
    tableProps: {
      title: '监测方案',
      api: listCombined,
      columns,
      canResize: true,
      formConfig: {
        schemas: searchFormSchema,
        autoSubmitOnEnter: true,
        showAdvancedButton: true,
        fieldMapToNumber: [],
        fieldMapToTime: [],
      },
      actionColumn: {
        width: 150,
        fixed: 'right',
      },
    },
  });

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

  /**
   * 新增事件
   */
  function handleAdd() {
    openModal(true, {
      mtrcCtp: 2,
      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,
    });
  }

  function handleFormulas(record: Recordable) {
    openFomulasModal(true, {
      record,
      isUpdate: true,
      showFooter: true,
    });
  }

  /**
   * 删除事件
   */
  async function handleDelete(record) {
    await deleteOne({ id: record.id }, handleSuccess);
  }
  /**
   * 批量删除事件
   */
  async function batchHandleDelete() {
    await batchDelete({ ids: selectedRowKeys.value }, handleSuccess);
  }
  /**
   * 成功回调
   */
  function handleSuccess() {
    (selectedRowKeys.value = []) && reload();
  }
  /**
   * 操作栏
   */
  function getTableAction(record) {
    return [
      {
        label: '编辑',
        onClick: handleEdit.bind(null, record),
      },
      //  {
      //    label: '编辑公式',
      //    onClick: handleFormulas.bind(null, record),
      //  }
    ];
  }
  /**
   * 下拉操作栏
   */
  function getDropDownAction(record) {
    return [
      {
        label: '详情',
        onClick: handleDetail.bind(null, record),
      },
      {
        label: '删除',
        popConfirm: {
          title: '是否确认删除',
          confirm: handleDelete.bind(null, record),
        },
      },
    ];
  }

  function handleEditEnd({ record, index, key, value }: Recordable) {
    handleSuccess();
    return false;
  }

  async function beforeEditSubmit({ record, index, key, value }) {
    let tmpPbj = {
      id: record.id,
      mtrcNo: record.mtrcNo,
    };
    tmpPbj[key] = value;
    await saveOrUpdate(tmpPbj);
  }

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

  function handleCaluculatAllMetric() {
    caluculatAllMetric();
  }

  const importCallback = async (datas) => {
    //const transformedData = transformData(datas,pid);
    await batchImmportCalculate(datas);
    await reload();
  };

  const handleCustomImport = () => {
    refImport.value.iniData(formSchema);
  };

  // function transformData(originalArray) {
  //   return originalArray.map(item => ({
  //       item: item.item,
  //       content: item.complineContent,
  //       fileName: item.fileName,
  //       checkMethods: [{
  //           actNames: item.actNames,
  //           content: item.content,
  //           projectid:projectId,
  //       }]
  //   }));
  // }
</script>

<style scoped></style>
