import "choerodon-ui/pro/lib/tooltip/style";
import _Tooltip from "choerodon-ui/pro/lib/tooltip";
import "choerodon-ui/pro/lib/modal/style";
import _Modal from "choerodon-ui/pro/lib/modal";
import "choerodon-ui/pro/lib/table/style";
import _Table from "@hzero-front-ui/c7n-ui/lib/TablePro";

function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

import React, { useCallback, Fragment } from 'react';
import { Page, Content, Header, Permission, Action, Breadcrumb, Choerodon, HeaderButtons } from '@choerodon/boot';
import { FormattedMessage } from 'react-intl';
import { withRouter } from 'react-router-dom';
import { observer } from 'mobx-react-lite';
import omit from 'lodash/omit';
import { usePVManagerStore } from "./stores";
import CreateForm from "./modals/create-form";
import PermissionManager from "./modals/permission-mananger";
import StatusTag from "../../components/status-tag";
import { handlePromptError } from "../../utils";
import StatusDot from "../../components/status-dot";
import StatusIcon from "../../components/StatusIcon/StatusIcon";
import "./index.less";
var Column = _Table.Column;

var modalKey1 = _Modal.key();

var modalKey2 = _Modal.key();

var deleteKey = _Modal.key();

var modalStyle1 = {
  width: 740
};
var modalStyle2 = {
  width: 'calc(100vw - 3.52rem)'
};
var statusStyle = {
  minWidth: 56,
  marginRight: 8,
  height: '.16rem',
  lineHeight: '.16rem',
  maxWidth: '50%'
};
var AppService = withRouter(observer(function (props) {
  var _usePVManagerStore = usePVManagerStore(),
      formatMessage = _usePVManagerStore.intl.formatMessage,
      projectId = _usePVManagerStore.AppState.currentMenuType.projectId,
      intlPrefix = _usePVManagerStore.intlPrefix,
      prefixCls = _usePVManagerStore.prefixCls,
      permissions = _usePVManagerStore.permissions,
      listDs = _usePVManagerStore.listDs,
      pvStore = _usePVManagerStore.pvStore;

  function refresh() {
    listDs.query();
  }

  function renderName(_ref) {
    var value = _ref.value,
        record = _ref.record;
    var status = record.get('status');
    var color = 'rgba(0, 0, 0, 0.26)';

    switch (status) {
      case 'Pending':
      case 'Operating':
      case 'Terminating':
        color = '#4D90FE';
        break;

      case 'Available':
        color = '#00BFA5';
        break;

      case 'Bound':
        color = '#FFB100';
        break;

      case 'Released':
        color = 'rgba(0, 0, 0, 0.26)';
        break;

      case 'Failed':
        color = '#F44336';
        break;

      default:
    }

    return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(StatusTag, {
      name: status,
      color: color,
      style: statusStyle
    }), /*#__PURE__*/React.createElement(_Tooltip, {
      title: value,
      placement: "topLeft"
    }, value));
  }

  function renderPvcName(_ref2) {
    var value = _ref2.value,
        record = _ref2.record;
    var status = record.get('status');
    return /*#__PURE__*/React.createElement(StatusIcon, {
      name: value,
      status: status === 'Released' ? 'deleted' : 'success',
      width: 0.08
    });
  }

  function renderActions(_ref3) {
    var record = _ref3.record;
    var actionData = {
      permission: {
        service: ['choerodon.code.project.deploy.cluster.pv-management.ps.permission-manage'],
        text: formatMessage({
          id: "".concat(intlPrefix, ".permission")
        }),
        action: openPermission
      },
      "delete": {
        service: ['choerodon.code.project.deploy.cluster.pv-management.ps.delete'],
        text: formatMessage({
          id: 'delete'
        }),
        action: handleDelete
      }
    };
    var status = record.get('status');
    var data;

    switch (status) {
      case 'Available':
        if (record.get('pvcName')) {
          data = [actionData.permission];
        } else {
          data = [actionData.permission, actionData["delete"]];
        }

        break;

      case 'Released':
        data = [actionData["delete"]];
        break;

      case 'Failed':
        if (!record.get('pvcName')) {
          data = [actionData["delete"]];
        }

        break;

      default:
    }

    return data && /*#__PURE__*/React.createElement(Action, {
      data: data
    });
  }

  function _getStatus(record) {
    var connect = record.get('clusterConnect');

    if (connect) {
      return ['running', 'connect'];
    }

    return ['disconnect'];
  }

  function renderCluster(_ref4) {
    var value = _ref4.value,
        record = _ref4.record;
    return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("span", null, /*#__PURE__*/React.createElement(StatusDot, {
      size: "small",
      getStatus: function getStatus() {
        return _getStatus(record);
      }
    }), "\xA0", value));
  }

  function openCreate() {
    _Modal.open({
      key: modalKey1,
      style: modalStyle1,
      drawer: true,
      title: formatMessage({
        id: "".concat(intlPrefix, ".create")
      }),
      children: /*#__PURE__*/React.createElement(CreateForm, {
        intlPrefix: intlPrefix,
        prefixCls: prefixCls,
        refresh: refresh
      }),
      okText: formatMessage({
        id: 'create'
      })
    });
  }

  function openPermission() {
    return _openPermission.apply(this, arguments);
  }

  function _openPermission() {
    _openPermission = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
      var record;
      return regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              record = listDs.current;

              _Modal.open({
                key: modalKey2,
                style: modalStyle2,
                drawer: true,
                title: formatMessage({
                  id: "".concat(intlPrefix, ".permission")
                }),
                children: /*#__PURE__*/React.createElement(PermissionManager, {
                  intlPrefix: intlPrefix,
                  prefixCls: prefixCls,
                  refresh: refresh,
                  pvId: record.get('id')
                }),
                okText: formatMessage({
                  id: 'save'
                })
              });

            case 2:
            case "end":
              return _context.stop();
          }
        }
      }, _callee);
    }));
    return _openPermission.apply(this, arguments);
  }

  function handleDelete() {
    return _handleDelete.apply(this, arguments);
  }

  function _handleDelete() {
    _handleDelete = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() {
      var record, modalProps;
      return regeneratorRuntime.wrap(function _callee2$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              record = listDs.current;
              modalProps = {
                title: formatMessage({
                  id: "".concat(intlPrefix, ".delete.title")
                }, {
                  name: record.get('name')
                }),
                children: formatMessage({
                  id: "".concat(intlPrefix, ".delete.des")
                }),
                okText: formatMessage({
                  id: 'delete'
                }),
                okProps: {
                  color: 'red'
                },
                cancelProps: {
                  color: 'dark'
                }
              };
              listDs["delete"](record, modalProps);

            case 3:
            case "end":
              return _context2.stop();
          }
        }
      }, _callee2);
    }));
    return _handleDelete.apply(this, arguments);
  }

  return /*#__PURE__*/React.createElement(Page, {
    service: ['choerodon.code.project.deploy.cluster.pv-management.ps.default']
  }, /*#__PURE__*/React.createElement(Header, null, /*#__PURE__*/React.createElement(HeaderButtons, {
    showClassName: false,
    items: [{
      name: /*#__PURE__*/React.createElement(FormattedMessage, {
        id: "".concat(intlPrefix, ".create")
      }),
      icon: 'playlist_add',
      display: true,
      permissions: ['choerodon.code.project.deploy.cluster.pv-management.ps.create'],
      handler: openCreate
    }, {
      icon: 'refresh',
      display: true,
      iconOnly: true,
      handler: refresh
    }]
  })), /*#__PURE__*/React.createElement(Breadcrumb, null), /*#__PURE__*/React.createElement(Content, {
    className: "".concat(prefixCls, "-content")
  }, /*#__PURE__*/React.createElement(_Table, {
    dataSet: listDs,
    border: false,
    queryBar: "bar",
    className: "".concat(prefixCls, "-table")
  }, /*#__PURE__*/React.createElement(Column, {
    name: "name",
    renderer: renderName,
    sortable: true
  }), /*#__PURE__*/React.createElement(Column, {
    renderer: renderActions,
    width: 70
  }), /*#__PURE__*/React.createElement(Column, {
    name: "description",
    sortable: true
  }), /*#__PURE__*/React.createElement(Column, {
    name: "clusterName",
    renderer: renderCluster
  }), /*#__PURE__*/React.createElement(Column, {
    name: "type",
    width: 100
  }), /*#__PURE__*/React.createElement(Column, {
    name: "pvcName",
    renderer: renderPvcName
  }), /*#__PURE__*/React.createElement(Column, {
    name: "accessModes",
    width: 140
  }), /*#__PURE__*/React.createElement(Column, {
    name: "requestResource",
    width: 100
  }))));
}));
export default AppService;