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, { useState, useEffect, useCallback, Fragment } from 'react';
import { Page, Content, Header, Permission, Action, Breadcrumb, HeaderButtons } from '@choerodon/boot';
import { FormattedMessage } from 'react-intl';
import { withRouter } from 'react-router-dom';
import { observer } from 'mobx-react-lite';
import { useCertificateStore } from "./stores";
import PermissionManage from "./modals/permission";
import CreateForm from "./modals/create-form";
import ClickText from "../../components/click-text";
import Tips from "../../components/new-tips";
import "./index.less";
var Column = _Table.Column;

var modalKey1 = _Modal.key();

var modalKey2 = _Modal.key();

var modalStyle1 = {
  width: 380
};
var modalStyle2 = {
  width: 'calc(100vw - 3.52rem)'
};
var AppService = withRouter(observer(function (props) {
  var _useCertificateStore = useCertificateStore(),
      formatMessage = _useCertificateStore.intl.formatMessage,
      id = _useCertificateStore.AppState.currentMenuType.id,
      intlPrefix = _useCertificateStore.intlPrefix,
      prefixCls = _useCertificateStore.prefixCls,
      permissions = _useCertificateStore.permissions,
      listDs = _useCertificateStore.listDs,
      certStore = _useCertificateStore.certStore;

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

  function renderName(_ref) {
    var value = _ref.value;
    return /*#__PURE__*/React.createElement(ClickText, {
      value: value,
      clickAble: true,
      onClick: function onClick() {
        return openModal('edit');
      },
      permissionCode: ['choerodon.code.project.deploy.cluster.cert-management.ps.create']
    });
  }

  function renderActions() {
    var actionData = [{
      service: ['choerodon.code.project.deploy.cluster.cert-management.ps.permission'],
      text: formatMessage({
        id: "".concat(intlPrefix, ".permission")
      }),
      action: openPermission
    }, {
      service: ['choerodon.code.project.deploy.cluster.cert-management.ps.delete'],
      text: formatMessage({
        id: 'delete'
      }),
      action: handleDelete
    }];
    return /*#__PURE__*/React.createElement(Action, {
      data: actionData
    });
  }

  function openModal(type) {
    _Modal.open({
      key: modalKey1,
      style: modalStyle1,
      drawer: true,
      title: formatMessage({
        id: "".concat(intlPrefix, ".").concat(type)
      }),
      children: /*#__PURE__*/React.createElement(CreateForm, {
        projectId: id,
        intlPrefix: intlPrefix,
        prefixCls: prefixCls,
        refresh: refresh,
        store: certStore,
        certId: type === 'edit' ? listDs.current.get('id') : null
      }),
      okText: formatMessage({
        id: type === 'edit' ? 'save' : 'create'
      }),
      afterClose: function afterClose() {
        return certStore.setCert({});
      }
    });
  }

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

  function _openPermission() {
    _openPermission = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
      return regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              _Modal.open({
                key: modalKey2,
                style: modalStyle2,
                drawer: true,
                className: 'c7ncd-modal-wrapper',
                title: /*#__PURE__*/React.createElement(Tips, {
                  helpText: formatMessage({
                    id: "".concat(intlPrefix, ".permission.tips")
                  }),
                  title: formatMessage({
                    id: "".concat(intlPrefix, ".permission")
                  })
                }),
                children: /*#__PURE__*/React.createElement(PermissionManage, {
                  intlPrefix: intlPrefix,
                  prefixCls: prefixCls,
                  refresh: refresh,
                  certId: listDs.current.get('id')
                })
              });

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

  function handleDelete() {
    var record = listDs.current;
    var 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);
  }

  return /*#__PURE__*/React.createElement(Page, {
    service: ['choerodon.code.project.deploy.cluster.cert-management.ps.default']
  }, /*#__PURE__*/React.createElement(Header, {
    title: /*#__PURE__*/React.createElement(FormattedMessage, {
      id: "app.head"
    })
  }, /*#__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.cert-management.ps.create'],
      handler: function handler() {
        return openModal('create');
      }
    }, {
      icon: 'refresh',
      display: true,
      iconOnly: true,
      handler: function handler() {
        return 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",
    sortable: true,
    renderer: renderName
  }), /*#__PURE__*/React.createElement(Column, {
    renderer: renderActions,
    width: "0.7rem"
  }), /*#__PURE__*/React.createElement(Column, {
    name: "domain",
    sortable: true
  }))));
}));
export default AppService;