<template>
  <div>
    <!--引用表格-->
    <BasicTable @register="registerTable">
      <!--插槽:table标题-->
      <template #tableTitle>
        <a-button type="primary" preIcon="ant-design:plus-outlined" @click="handleAdd"> 添加设备</a-button>
      </template>
      <template #bodyCell="{ column, record, index, text }">
        <template v-if="column.dataIndex === 'relayState'">
          <span v-if="record.relayState ==null||record.relayState ===''" style="color:red">
            未知
          </span>
          <span v-else>
            <span v-if="record.relayState ==='1'" style="color:green">
              合闸
            </span>
            <span v-if="record.relayState ==='0'" style="color:red">
              拉闸
            </span>
          </span>
        </template>
      </template>
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)"/>
      </template>
    </BasicTable>
    <ApiLogModal ref="apiLogModal"></ApiLogModal>
    <DrawerModal ref="registerDrawer" @success="handleSuccess" />
    <a-modal v-model:visible="tipVisible" width="300px">
      <template #title>
        <Icon icon="ant-design:info-circle-outlined" :size="20" style="margin-right:10px;color:white;background:#1ea0fa;border-radius:10px;"/>{{tipTitle}}
      </template>
      <div style="text-align: center;margin: 40px 0;">
        {{tipContent}}
      </div>
      <template #footer style="text-align: center;">
        <a-button type="primary" style="margin-right: 35%" @click="handleCancel">知道了</a-button>
      </template>
    </a-modal>
  </div>
</template>

<script lang="ts" name="iot-nuIotElectricitynInfo" setup>
  import {reactive, ref,h} from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useListPage } from '/@/hooks/system/useListPage';
  import { useUserStore } from '/@/store/modules/user';
  import {Modal} from "ant-design-vue";
  import {list, save, deleteMeter, eleReset, eleCutOff, eleConnected, eleRead} from './electricity.api';
  import { columns, searchFormSchema } from './electricity.data';
  import {useModal} from "@/components/Modal";
  import ApiLogModal from "/@/views/iot/tq/electricity/apilog/ApiLogModal.vue";
  
  import DrawerModal from "./components/DrawerModal.vue";
  import { defHttp } from '/@/utils/http/axios';

  const queryParam = reactive<any>({});
  const apiLogModal = ref();
  const registerDrawer = ref();
  const tipVisible = ref(false);
  const tipTitle = ref('提示');
  const tipContent = ref('');
  //注册model
  const [registerModal, {openModal}] = useModal();
  //注册table数据
  const { prefixCls,tableContext,onExportXls,onImportXls } = useListPage({
    tableProps:{
      title: '智能电表',
      api: list,
      columns,
      canResize:false,
      // showIndexColumn: true,
      formConfig: {
        //labelWidth: 120,
        schemas: searchFormSchema,
        autoSubmitOnEnter:true,
        showAdvancedButton:false,
        fieldMapToNumber: [
        ],
        fieldMapToTime: [
        ],
      },
      actionColumn: {
        width: 290,
        fixed:'right'
      },
      beforeFetch: (params) => {
        return Object.assign(params, queryParam);
      },
    },
  })

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

  /**
   * 编辑
   */
  function handleAdd() {
    registerDrawer.value.disableSubmit = false;
    registerDrawer.value.add();
  }

  function handleCancel() {
    tipVisible.value = false;
  }
  /**
   * 成功回调
   */
  function handleSuccess() {
    (selectedRowKeys.value = []) && reload();
  }

  /**
   * 操作栏
   */
  function getTableAction(record){
    return [
      {
        label: '删除',
        popConfirm: {
          title: '是否确认删除数据',
          confirm: handleDelete.bind(null, record),
          placement: 'topLeft',
        },
        ifShow: record.izAllocate != 'Y',
      },
      {
        label: '抄表',
        onClick: handleRead.bind(null, record),
      },
      {
        label: '拉闸',
        popConfirm: {
          title: '是否确认拉闸',
          confirm: handleControlLz.bind(null, record),
          placement: 'topLeft',
        },
        ifShow: record.relayState == 1,
      },
      {
        label: '合闸',
        popConfirm: {
          title: '是否确认合闸',
          confirm: handleControlHz.bind(null, record),
          placement: 'topLeft',
        },
        ifShow: record.relayState == 0,
      },
      {
        label: '清零',
        popConfirm: {
          title: '是否确认清零',
          confirm: handleReset.bind(null, record),
          placement: 'topLeft',
        },
      },
      // {
      //   label: '日志',
      //   onClick: showApiLog.bind(null, record),
      // },
    ]
  }

  // 删除
  async function handleDelete(record: Recordable) {
    await deleteMeter(record);
    reload();
  }

  // 抄电表
  async function handleRead(record: Recordable) {
    const params = {
      'sn' : record.sn,
    };
    await eleRead(params);
    reload();
  }

  // 电表拉闸
  async function handleControlLz(record: Recordable) {
    if(record.relayState == '0'){
      tipVisible.value=true;
      tipTitle.value = "拉闸";
      tipContent.value = "此电表已拉闸！";
      return;
    }
    const params = {
      'sn' : record.sn
    };
    await eleCutOff(params);
    reload();
  }

  // 电表合闸
  async function handleControlHz(record: Recordable) {
    if(record.relayState == '1'){
      tipVisible.value=true;
      tipTitle.value = "合闸";
      tipContent.value = "此电表已合闸！";
      return;
    }
    const params = {
      'sn' : record.sn
    };
    await eleConnected(params);
    reload();
  }

  // 电表清零
  async function handleReset(record: Recordable) {
    const params = {
      'sn' : record.sn
    };
    await eleReset(params);
    reload();
  }

  /**
   * 查看api日志
   */
  function showApiLog(record){
    console.log(record);
    apiLogModal.value.disableSubmit = true;
    apiLogModal.value.showApiLog(record);
  }

</script>
