import "choerodon-ui/pro/lib/modal/style";
import _Modal from "choerodon-ui/pro/lib/modal";
import React, { Fragment } from 'react';
import { observer } from 'mobx-react-lite';
import { HeaderButtons } from '@choerodon/master';
import { useResourceStore } from "../../../../stores";
import { useModalStore } from "./stores";
import { usePVCStore } from "../stores";
import CreateForm from "./create-form";

var modalKey = _Modal.key();

var modalStyle = {
  width: 380
};
var PVCModals = observer(function () {
  var _useResourceStore = useResourceStore(),
      intlPrefix = _useResourceStore.intlPrefix,
      prefixCls = _useResourceStore.prefixCls,
      formatMessage = _useResourceStore.intl.formatMessage,
      parentId = _useResourceStore.resourceStore.getSelectedMenu.parentId,
      treeDs = _useResourceStore.treeDs;

  var _usePVCStore = usePVCStore(),
      tableDs = _usePVCStore.tableDs;

  var _useModalStore = useModalStore(),
      permissions = _useModalStore.permissions,
      projectId = _useModalStore.AppState.currentMenuType.projectId;

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

  function openModal() {
    _Modal.open({
      key: modalKey,
      style: modalStyle,
      drawer: true,
      title: formatMessage({
        id: "".concat(intlPrefix, ".create.pvc")
      }),
      children: /*#__PURE__*/React.createElement(CreateForm, {
        refresh: refresh,
        envId: parentId,
        intlPrefix: intlPrefix,
        prefixCls: prefixCls
      }),
      okText: formatMessage({
        id: 'create'
      })
    });
  }

  function getButtons() {
    var envRecord = treeDs.find(function (record) {
      return record.get('key') === parentId;
    });
    var connect = envRecord.get('connect');
    var disabled = !connect;
    return [{
      permissions: ['choerodon.code.project.deploy.app-deployment.resource.ps.create-pvc'],
      name: formatMessage({
        id: "".concat(intlPrefix, ".create.pvc")
      }),
      icon: 'playlist_add',
      handler: openModal,
      display: true,
      service: permissions,
      disabled: disabled
    }, {
      icon: 'refresh',
      handler: refresh,
      display: true
    }];
  }

  return /*#__PURE__*/React.createElement(HeaderButtons, {
    items: getButtons(),
    showClassName: true
  });
});
export default PVCModals;