import "choerodon-ui/pro/lib/button/style";
import _Button from "choerodon-ui/pro/lib/button";
import "choerodon-ui/pro/lib/form/style";
import _Form from "@hzero-front-ui/c7n-ui/lib/FormPro";
import "choerodon-ui/pro/lib/number-field/style";
import _NumberField from "choerodon-ui/pro/lib/number-field";
import "choerodon-ui/pro/lib/text-area/style";
import _TextArea from "choerodon-ui/pro/lib/text-area";
import "choerodon-ui/pro/lib/text-field/style";
import _TextField from "choerodon-ui/pro/lib/text-field";
import "choerodon-ui/pro/lib/select/style";
import _Select from "choerodon-ui/pro/lib/select";
import "choerodon-ui/pro/lib/modal/style";
import _Modal from "choerodon-ui/pro/lib/modal";
import "choerodon-ui/pro/lib/select-box/style";
import _SelectBox from "choerodon-ui/pro/lib/select-box";
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 } from 'react';
import { observer } from 'mobx-react-lite';
import { injectIntl, FormattedMessage } from 'react-intl';
import { Action, Choerodon } from '@choerodon/boot';
import { SMALL } from "../../../../utils/getModalWidth";
import { usePVCreateStore } from "./stores";
import StatusDot from "../../../../components/status-dot";
import Tips from "../../../../components/new-tips";
import AddProject from "./AddProject";
import "./index.less";
var Column = _Table.Column;
var Option = _SelectBox.Option;

var modalKey = _Modal.key();

var modalStyle = {
  width: SMALL
};

var CreateForm = function CreateForm() {
  var _usePVCreateStore = usePVCreateStore(),
      formDs = _usePVCreateStore.formDs,
      formatMessage = _usePVCreateStore.intl.formatMessage,
      modal = _usePVCreateStore.modal,
      refresh = _usePVCreateStore.refresh,
      intlPrefix = _usePVCreateStore.intlPrefix,
      prefixCls = _usePVCreateStore.prefixCls,
      projectTableDs = _usePVCreateStore.projectTableDs,
      projectOptionsDs = _usePVCreateStore.projectOptionsDs,
      selectDs = _usePVCreateStore.selectDs,
      labelDs = _usePVCreateStore.labelDs;

  modal.handleOk( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
    return regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            _context.prev = 0;
            _context.next = 3;
            return formDs.submit();

          case 3:
            _context.t0 = _context.sent;

            if (!(_context.t0 !== false)) {
              _context.next = 7;
              break;
            }

            refresh();
            return _context.abrupt("return", true);

          case 7:
            return _context.abrupt("return", false);

          case 10:
            _context.prev = 10;
            _context.t1 = _context["catch"](0);
            Choerodon.handleResponseError(_context.t1);
            return _context.abrupt("return", false);

          case 14:
          case "end":
            return _context.stop();
        }
      }
    }, _callee, null, [[0, 10]]);
  })));

  function renderClusterOption(_ref2) {
    var record = _ref2.record,
        text = _ref2.text,
        value = _ref2.value;
    return /*#__PURE__*/React.createElement(React.Fragment, null, text && /*#__PURE__*/React.createElement(StatusDot, {
      active: true,
      synchronize: true,
      size: "inner",
      connect: record.get('connect')
    }), text);
  }

  function getClusterOptionProp(_ref3) {
    var record = _ref3.record;
    return {
      disabled: !record.get('connect')
    };
  }

  function renderAction(_ref4) {
    var record = _ref4.record;
    var actionData = [{
      text: formatMessage({
        id: "".concat(intlPrefix, ".permission.delete.title")
      }),
      action: function action() {
        return projectTableDs.remove(record);
      }
    }];
    return /*#__PURE__*/React.createElement(Action, {
      data: actionData
    });
  }

  function openAddPermission() {
    _Modal.open({
      key: modalKey,
      style: modalStyle,
      drawer: true,
      title: formatMessage({
        id: "".concat(intlPrefix, ".project.add")
      }),
      children: /*#__PURE__*/React.createElement(AddProject, {
        dataSet: selectDs,
        tableDs: projectTableDs,
        optionsDs: projectOptionsDs,
        intlPrefix: intlPrefix,
        prefixCls: prefixCls
      }),
      okText: formatMessage({
        id: 'add'
      }),
      onCancel: function onCancel() {
        return selectDs.reset();
      }
    });
  }

  var handleRemoveLabel = useCallback(function (labelRecord) {
    labelDs.remove(labelRecord);
  }, []);
  var handleAddLabel = useCallback(function () {
    labelDs.create();
  }, []);
  return /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefixCls, "-create-wrap")
  }, /*#__PURE__*/React.createElement(_Form, {
    dataSet: formDs,
    columns: 3,
    style: {
      width: '5.2rem'
    }
  }, /*#__PURE__*/React.createElement(_Select, {
    name: "clusterId",
    searchable: true,
    colSpan: 3,
    clearButton: false,
    optionRenderer: renderClusterOption,
    onOption: getClusterOptionProp
  }), /*#__PURE__*/React.createElement(_TextField, {
    name: "name",
    colSpan: 3,
    disabled: !formDs.current.get('clusterId')
  }), /*#__PURE__*/React.createElement(_TextArea, {
    name: "description",
    colSpan: 3,
    resize: "vertical"
  }), /*#__PURE__*/React.createElement(_Select, {
    name: "type",
    colSpan: 3,
    clearButton: false
  }), /*#__PURE__*/React.createElement(_Select, {
    name: "accessModes",
    colSpan: 3,
    clearButton: false
  }), /*#__PURE__*/React.createElement(_NumberField, {
    name: "storage",
    step: 1,
    colSpan: 2
  }), /*#__PURE__*/React.createElement(_Select, {
    name: "unit",
    clearButton: false
  }), /*#__PURE__*/React.createElement(_TextField, {
    name: "path",
    colSpan: 3
  }), formDs.current.get('type') === 'NFS' && /*#__PURE__*/React.createElement(_TextField, {
    name: "server",
    colSpan: 3
  }), formDs.current.get('type') === 'LocalPV' && /*#__PURE__*/React.createElement(_Select, {
    name: "clusterNodeName",
    clearButton: false,
    colSpan: 3
  })), /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefixCls, "-create-wrap-label-title")
  }, /*#__PURE__*/React.createElement("span", null, formatMessage({
    id: 'label'
  }))), labelDs.map(function (labelRecord) {
    return /*#__PURE__*/React.createElement(_Form, {
      record: labelRecord,
      columns: 14,
      key: labelRecord.id,
      style: {
        width: '5.2rem'
      }
    }, /*#__PURE__*/React.createElement(_TextField, {
      colSpan: 6,
      name: "key"
    }), /*#__PURE__*/React.createElement("span", {
      className: "".concat(prefixCls, "-create-wrap-label-equal")
    }, "="), /*#__PURE__*/React.createElement(_TextField, {
      colSpan: 6,
      name: "value"
    }), labelDs.length > 1 ? /*#__PURE__*/React.createElement(_Button, {
      funcType: "flat",
      icon: "delete",
      onClick: function onClick() {
        return handleRemoveLabel(labelRecord);
      },
      className: "".concat(prefixCls, "-create-wrap-delete-btn")
    }) : /*#__PURE__*/React.createElement("span", null));
  }), /*#__PURE__*/React.createElement(_Button, {
    funcType: "flat",
    color: "primary",
    icon: "add",
    onClick: handleAddLabel
  }, formatMessage({
    id: 'label.add'
  })), /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefixCls, "-create-wrap-permission")
  }, /*#__PURE__*/React.createElement(Tips, {
    title: formatMessage({
      id: "".concat(intlPrefix, ".share")
    }),
    helpText: formatMessage({
      id: "".concat(intlPrefix, ".share.tips")
    })
  })), /*#__PURE__*/React.createElement(_Form, {
    dataSet: formDs
  }, /*#__PURE__*/React.createElement(_SelectBox, {
    name: "skipCheckProjectPermission",
    colSpan: 3
  }, /*#__PURE__*/React.createElement(Option, {
    value: true
  }, /*#__PURE__*/React.createElement("span", {
    className: "".concat(prefixCls, "-create-wrap-radio")
  }, formatMessage({
    id: "".concat(intlPrefix, ".project.all")
  }))), /*#__PURE__*/React.createElement(Option, {
    value: false
  }, /*#__PURE__*/React.createElement("span", {
    className: "".concat(prefixCls, "-create-wrap-radio")
  }, formatMessage({
    id: "".concat(intlPrefix, ".project.special")
  }))))), !formDs.current.get('skipCheckProjectPermission') && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(_Button, {
    color: "primary",
    funcType: "flat",
    icon: "add",
    onClick: openAddPermission,
    className: "".concat(prefixCls, "-permission-wrap-button"),
    disabled: !formDs.current.get('clusterId')
  }, /*#__PURE__*/React.createElement(FormattedMessage, {
    id: "".concat(intlPrefix, ".project.add")
  })), /*#__PURE__*/React.createElement(_Table, {
    dataSet: projectTableDs,
    queryBar: "none"
  }, /*#__PURE__*/React.createElement(Column, {
    name: "name"
  }), /*#__PURE__*/React.createElement(Column, {
    renderer: renderAction
  }), /*#__PURE__*/React.createElement(Column, {
    name: "code"
  }))));
};

export default injectIntl(observer(CreateForm));