import "choerodon-ui/lib/tooltip/style";
import _Tooltip from "choerodon-ui/lib/tooltip";
import "choerodon-ui/pro/lib/table/style";
import _Table from "@hzero-front-ui/c7n-ui/lib/TablePro";
import React, { Fragment, useMemo, useState } from 'react';
import { FormattedMessage } from 'react-intl';
import { observer } from 'mobx-react-lite';
import { Action } from '@choerodon/boot';
import StatusIcon from "../../../../../components/StatusIcon";
import { useResourceStore } from "../../../stores";
import { useCertificateStore } from "./stores";
import Modals from "./modals";
import MouserOverWrapper from "../../../../../components/MouseOverWrapper";
import { getTimeLeft } from "../../../../../utils";
import { useMainStore } from "../../stores";
import ResourceListTitle from "../../components/resource-list-title";
import "./index.less";
var Column = _Table.Column;
var CertContent = observer(function () {
  var _useResourceStore = useResourceStore(),
      prefixCls = _useResourceStore.prefixCls,
      intlPrefix = _useResourceStore.intlPrefix,
      treeDs = _useResourceStore.treeDs,
      parentId = _useResourceStore.resourceStore.getSelectedMenu.parentId;

  var _useCertificateStore = useCertificateStore(),
      certificateDs = _useCertificateStore.certificateDs,
      formatMessage = _useCertificateStore.intl.formatMessage,
      projectId = _useCertificateStore.AppState.currentMenuType.projectId;

  var _useMainStore = useMainStore(),
      openDeleteModal = _useMainStore.mainStore.openDeleteModal;

  function refresh() {
    treeDs.query();
    certificateDs.query();
  }

  function renderName(_ref) {
    var value = _ref.value,
        record = _ref.record;
    var commandStatus = record.get('commandStatus');
    var error = record.get('error');
    return /*#__PURE__*/React.createElement("div", {
      className: "c7n-network-service"
    }, /*#__PURE__*/React.createElement(StatusIcon, {
      name: value,
      status: commandStatus || '',
      error: error || ''
    }));
  }

  function renderDomains(_ref2) {
    var value = _ref2.value;
    return /*#__PURE__*/React.createElement(MouserOverWrapper, {
      text: value[0] || '',
      width: 0.25
    }, value[0]);
  }

  function renderValid(_ref3) {
    var record = _ref3.record;
    var validFrom = record.get('validFrom');
    var validUntil = record.get('validUntil');
    var commandStatus = record.get('commandStatus');
    var msg = null;
    var content = null;
    if (!(validFrom && validUntil && commandStatus === 'success')) return content;
    content = /*#__PURE__*/React.createElement(Fragment, null, /*#__PURE__*/React.createElement(FormattedMessage, {
      id: "timeFrom"
    }), "\uFF1A", validFrom, /*#__PURE__*/React.createElement("br", null), /*#__PURE__*/React.createElement(FormattedMessage, {
      id: "timeUntil"
    }), "\uFF1A", validUntil);
    var start = new Date(validFrom.replace(/-/g, '/')).getTime();
    var end = new Date(validUntil.replace(/-/g, '/')).getTime();
    var now = Date.now();

    if (now < start) {
      msg = /*#__PURE__*/React.createElement(FormattedMessage, {
        id: "notActive"
      });
    } else if (now > end) {
      msg = /*#__PURE__*/React.createElement(FormattedMessage, {
        id: "expired"
      });
    } else {
      msg = getTimeLeft(now, end);
    }

    return /*#__PURE__*/React.createElement(_Tooltip, {
      title: content,
      trigger: "hover",
      placement: "top"
    }, /*#__PURE__*/React.createElement("span", null, msg));
  }

  function renderAction(_ref4) {
    var record = _ref4.record;
    var commandStatus = record.get('commandStatus');
    var id = record.get('id');
    var name = record.get('certName');

    if (commandStatus === 'operating') {
      return null;
    }

    var buttons = [{
      service: ['choerodon.code.project.deploy.app-deployment.resource.ps.delete-certificate'],
      text: formatMessage({
        id: 'delete'
      }),
      action: function action() {
        return openDeleteModal(parentId, id, name, 'certificate', refresh);
      }
    }];
    return /*#__PURE__*/React.createElement(Action, {
      data: buttons
    });
  }

  return /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefixCls, "-ingress-table")
  }, /*#__PURE__*/React.createElement(Modals, null), /*#__PURE__*/React.createElement(ResourceListTitle, {
    type: "certifications"
  }), /*#__PURE__*/React.createElement(_Table, {
    dataSet: certificateDs,
    border: false,
    queryBar: "bar"
  }, /*#__PURE__*/React.createElement(Column, {
    name: "certName",
    renderer: renderName,
    sortable: true
  }), /*#__PURE__*/React.createElement(Column, {
    renderer: renderAction,
    width: "0.7rem"
  }), /*#__PURE__*/React.createElement(Column, {
    name: "domains",
    renderer: renderDomains
  }), /*#__PURE__*/React.createElement(Column, {
    renderer: renderValid,
    header: formatMessage({
      id: 'validDate'
    }),
    width: "1rem"
  })));
});
export default CertContent;