import "choerodon-ui/lib/button/style";
import _Button from "choerodon-ui/lib/button";
import "choerodon-ui/pro/lib/form/style";
import _Form from "@hzero-front-ui/c7n-ui/lib/FormPro";
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, { Fragment } from 'react';
import { Action } from '@choerodon/boot';
import { injectIntl, FormattedMessage } from 'react-intl';
import { observer } from 'mobx-react-lite';
import AddProject from "./AddProject";
import Tips from "../../../../components/new-tips";
import { useCertPermissionStore } from "./stores";
import "./index.less";
var Column = _Table.Column;
var Option = _SelectBox.Option;

var modalKey = _Modal.key();

var modalStyle = {
  width: 380
};
export default injectIntl(observer(function () {
  var _useCertPermissionSto = useCertPermissionStore(),
      detailDs = _useCertPermissionSto.detailDs,
      allProjectDs = _useCertPermissionSto.allProjectDs,
      permissionProjectDs = _useCertPermissionSto.permissionProjectDs,
      optionsDs = _useCertPermissionSto.optionsDs,
      intlPrefix = _useCertPermissionSto.intlPrefix,
      prefixCls = _useCertPermissionSto.prefixCls,
      formatMessage = _useCertPermissionSto.intl.formatMessage,
      modal = _useCertPermissionSto.modal,
      refresh = _useCertPermissionSto.refresh;

  var record = detailDs.current;
  if (!record) return;
  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 detailDs.submit();

          case 3:
            _context.t0 = _context.sent;

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

            refresh();
            _context.next = 9;
            break;

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

          case 9:
            _context.next = 14;
            break;

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

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

  function openModal() {
    _Modal.open({
      key: modalKey,
      style: modalStyle,
      drawer: true,
      title: formatMessage({
        id: "".concat(intlPrefix, ".project.add")
      }),
      children: /*#__PURE__*/React.createElement(AddProject, {
        dataSet: permissionProjectDs,
        optionsDs: optionsDs,
        intlPrefix: intlPrefix,
        prefixCls: prefixCls,
        detailDs: detailDs
      }),
      okText: formatMessage({
        id: 'add'
      }),
      onCancel: handleCancel
    });
  }

  function handleCancel() {
    permissionProjectDs.reset();
  }

  function handleDelete() {
    var modalProps = {
      title: formatMessage({
        id: 'c7ncd.deployment.permission.delete.title'
      }),
      children: formatMessage({
        id: 'c7ncd.deployment.permission.project.delete.des'
      }),
      okText: formatMessage({
        id: 'delete'
      }),
      okProps: {
        color: 'red'
      },
      cancelProps: {
        color: 'dark'
      }
    };
    permissionProjectDs["delete"](permissionProjectDs.current, modalProps);
  }

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

  return /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefixCls, "-permission-wrap")
  }, /*#__PURE__*/React.createElement(Tips, {
    helpText: formatMessage({
      id: "".concat(intlPrefix, ".share.tips")
    }),
    title: formatMessage({
      id: "".concat(intlPrefix, ".share")
    })
  }), /*#__PURE__*/React.createElement(_Form, {
    dataSet: detailDs
  }, /*#__PURE__*/React.createElement(_SelectBox, {
    name: "skipCheckProjectPermission"
  }, /*#__PURE__*/React.createElement(Option, {
    value: true
  }, /*#__PURE__*/React.createElement("span", {
    className: "".concat(prefixCls, "-permission-wrap-radio")
  }, formatMessage({
    id: "".concat(intlPrefix, ".project.all")
  }))), /*#__PURE__*/React.createElement(Option, {
    value: false
  }, /*#__PURE__*/React.createElement("span", {
    className: "".concat(prefixCls, "-permission-wrap-radio")
  }, /*#__PURE__*/React.createElement(Tips, {
    helpText: formatMessage({
      id: "".concat(intlPrefix, ".some.tips")
    }),
    title: formatMessage({
      id: "".concat(intlPrefix, ".project.some")
    })
  }))))), !record.get('skipCheckProjectPermission') ? /*#__PURE__*/React.createElement(Fragment, null, /*#__PURE__*/React.createElement(_Button, {
    type: "primary",
    icon: "add",
    onClick: openModal,
    className: "".concat(prefixCls, "-permission-wrap-button")
  }, /*#__PURE__*/React.createElement(FormattedMessage, {
    id: "".concat(intlPrefix, ".project.add")
  })), /*#__PURE__*/React.createElement(_Table, {
    dataSet: permissionProjectDs,
    pristine: true
  }, /*#__PURE__*/React.createElement(Column, {
    name: "name",
    sortable: true
  }), /*#__PURE__*/React.createElement(Column, {
    renderer: renderAction
  }), /*#__PURE__*/React.createElement(Column, {
    name: "code",
    sortable: true
  }))) : /*#__PURE__*/React.createElement(_Table, {
    dataSet: allProjectDs
  }, /*#__PURE__*/React.createElement(Column, {
    name: "name",
    sortable: true
  }), /*#__PURE__*/React.createElement(Column, {
    name: "code",
    sortable: true
  })));
}));