import "choerodon-ui/pro/lib/modal/style";
import _Modal from "choerodon-ui/pro/lib/modal";
import "choerodon-ui/pro/lib/icon/style";
import _Icon from "choerodon-ui/pro/lib/icon";
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 } from 'react';
import { Permission, Action } from '@choerodon/boot';
import { useClusterContentStore } from "../stores";
import StatusTags from "../../../../../../components/status-tag";
import RemoveRoleModal from "./RoleRemoveForm";
import RemoveNodeModal from "./NodeRemoveModal";
import "./index.less";
var Column = _Table.Column;

var NodeList = function NodeList() {
  var _useClusterContentSto = useClusterContentStore(),
      intlPrefix = _useClusterContentSto.intlPrefix,
      formatMessage = _useClusterContentSto.formatMessage,
      NodeListDs = _useClusterContentSto.NodeListDs,
      projectId = _useClusterContentSto.projectId,
      contentStore = _useClusterContentSto.contentStore,
      clusterStore = _useClusterContentSto.clusterStore;

  var refresh = function refresh() {
    NodeListDs.query();
  };

  var renderStatusName = function renderStatusName(_ref) {
    var record = _ref.record;
    var status = record.get('status');
    var nodeName = record.get('nodeName');
    var operatingStatus = record.get('operatingStatus');
    var errorMsg = record.get('errorMsg');
    var outerNodeFlag = record.get('outerNodeFlag');
    return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(StatusTags, {
      name: status,
      colorCode: status
    }), /*#__PURE__*/React.createElement(_Tooltip, {
      title: nodeName
    }, /*#__PURE__*/React.createElement("span", null, nodeName)), outerNodeFlag && /*#__PURE__*/React.createElement("span", {
      className: "c7ncd-cluster-outerNodeFlag"
    }, "\u516C\u7F51"), operatingStatus === 'failed' && /*#__PURE__*/React.createElement(_Tooltip, {
      title: errorMsg,
      overlayStyle: {
        maxHeight: 500,
        overflow: 'auto'
      }
    }, /*#__PURE__*/React.createElement(_Icon, {
      type: "info",
      style: {
        color: '#F76776 ',
        marginLeft: '7px'
      }
    })));
  };

  var renderCm = function renderCm(record, type) {
    var content = /*#__PURE__*/React.createElement("div", {
      className: "c7n-cls-table-cm"
    }, /*#__PURE__*/React.createElement("span", {
      className: "c7n-cls-up"
    }), /*#__PURE__*/React.createElement("span", null, "".concat(record.get("".concat(type, "Request")), " (").concat(record.get("".concat(type, "RequestPercentage")), ")")), /*#__PURE__*/React.createElement("span", {
      className: "c7n-cls-down"
    }), /*#__PURE__*/React.createElement("span", {
      className: "c7n-cls-table-one-line-omit"
    }, "".concat(record.get("".concat(type, "Limit")), " (").concat(record.get("".concat(type, "LimitPercentage")), ")")));
    return /*#__PURE__*/React.createElement(_Tooltip, {
      title: content
    }, content);
  };

  var renderType = function renderType(_ref2) {
    var type = _ref2.value;
    return /*#__PURE__*/React.createElement(_Tooltip, {
      title: type
    }, /*#__PURE__*/React.createElement("span", null, type));
  };

  var renderTime = function renderTime(_ref3) {
    var time = _ref3.value;
    return /*#__PURE__*/React.createElement(_Tooltip, {
      title: time
    }, /*#__PURE__*/React.createElement("span", null, time));
  }; // 移除角色


  var openRemoveRole = function openRemoveRole(record, roleType) {
    var nodeName = record.get('nodeName');
    var nodeId = record.get('id');

    _Modal.open({
      key: _Modal.key(),
      title: formatMessage({
        id: "".concat(intlPrefix, ".node.action.removeRole.").concat(roleType)
      }),
      children: /*#__PURE__*/React.createElement(RemoveRoleModal, {
        nodeName: nodeName,
        projectId: projectId,
        nodeId: nodeId,
        roleType: roleType,
        contentStore: contentStore,
        afterOk: refresh,
        formatMessage: formatMessage,
        intlPrefix: intlPrefix
      }),
      okText: formatMessage({
        id: "".concat(intlPrefix, ".node.modal.removeRole")
      }),
      footer: function footer(okbtn, cancelbtn) {
        return /*#__PURE__*/React.createElement(React.Fragment, null, cancelbtn);
      }
    });
  }; // 移除节点


  var handleRemoveNode = function handleRemoveNode(record) {
    var nodeName = record.get('nodeName');
    var nodeId = record.get('id');

    _Modal.open({
      key: _Modal.key(),
      title: formatMessage({
        id: "".concat(intlPrefix, ".node.modal.cannotDelete")
      }),
      children: /*#__PURE__*/React.createElement(RemoveNodeModal, {
        nodeId: nodeId,
        nodeName: nodeName,
        projectId: projectId,
        formatMessage: formatMessage,
        roleType: "worker",
        intlPrefix: intlPrefix,
        contentStore: contentStore,
        afterOk: refresh
      }),
      okCancel: false,
      okText: formatMessage({
        id: 'cancel'
      })
    });
  };

  var renderNodeOpts = function renderNodeOpts(_ref4) {
    var _clusterStore$getSele;

    var record = _ref4.record,
        dataSet = _ref4.dataSet;
    var clusterType = record.get('clusterType');
    var enableDeleteEtcdRole = record.get('enableDeleteEtcdRole');
    var enableDeleteMasterRole = record.get('enableDeleteMasterRole');
    var enableDeleteNode = record.get('enableDeleteNode');

    if (clusterType === 'imported' || ((_clusterStore$getSele = clusterStore.getSelectedMenu) === null || _clusterStore$getSele === void 0 ? void 0 : _clusterStore$getSele.status) === 'operating') {
      return null;
    }

    var optsData = [];

    if (enableDeleteNode) {
      optsData.unshift({
        service: ['choerodon.code.project.deploy.cluster.cluster-management.ps.deleteNode'],
        text: formatMessage({
          id: "".concat(intlPrefix, ".node.action.removeNode")
        }),
        action: function action() {
          return handleRemoveNode(record);
        }
      });
    }

    if (enableDeleteEtcdRole) {
      optsData.unshift({
        service: ['choerodon.code.project.deploy.cluster.cluster-management.ps.deleteEtcdRole'],
        text: formatMessage({
          id: "".concat(intlPrefix, ".node.action.removeRole.etcd")
        }),
        action: function action() {
          return openRemoveRole(record, 'etcd');
        }
      });
    }

    if (enableDeleteMasterRole) {
      optsData.unshift({
        service: ['choerodon.code.project.deploy.cluster.cluster-management.ps.deleteMasterRole'],
        text: formatMessage({
          id: "".concat(intlPrefix, ".node.action.removeRole.master")
        }),
        action: function action() {
          return openRemoveRole(record, 'master');
        }
      });
    }

    if (!optsData.length) return null;
    return /*#__PURE__*/React.createElement(Action, {
      placement: "bottomRight",
      data: optsData
    });
  };

  var renderCpu = function renderCpu(_ref5) {
    var record = _ref5.record;
    return renderCm(record, 'cpu');
  };

  var renderMemory = function renderMemory(_ref6) {
    var record = _ref6.record;
    return renderCm(record, 'memory');
  };

  return /*#__PURE__*/React.createElement("div", {
    className: "c7ncd-cluster-table"
  }, /*#__PURE__*/React.createElement(_Table, {
    dataSet: NodeListDs,
    border: false,
    queryBar: "none"
  }, /*#__PURE__*/React.createElement(Column, {
    width: 170,
    header: formatMessage({
      id: "".concat(intlPrefix, ".node.ip")
    }),
    renderer: renderStatusName
  }), /*#__PURE__*/React.createElement(Column, {
    width: 60,
    renderer: renderNodeOpts
  }), /*#__PURE__*/React.createElement(Column, {
    name: "role",
    minWidth: 80,
    renderer: renderType
  }), /*#__PURE__*/React.createElement(Column, {
    header: formatMessage({
      id: "".concat(intlPrefix, ".node.cpu")
    }),
    renderer: renderCpu
  }), /*#__PURE__*/React.createElement(Column, {
    header: formatMessage({
      id: "".concat(intlPrefix, ".node.memory")
    }),
    renderer: renderMemory
  }), /*#__PURE__*/React.createElement(Column, {
    name: "createTime",
    width: 150,
    renderer: renderTime
  })));
};

export default NodeList;