import "choerodon-ui/lib/spin/style";
import _Spin from "@hzero-front-ui/c7n-ui/lib/Spin";
import "choerodon-ui/pro/lib/pagination/style";
import _Pagination from "choerodon-ui/pro/lib/pagination";
import "choerodon-ui/pro/lib/text-field/style";
import _TextField from "choerodon-ui/pro/lib/text-field";
import "choerodon-ui/lib/icon/style";
import _Icon from "choerodon-ui/lib/icon";
import "choerodon-ui/lib/button/style";
import _Button2 from "choerodon-ui/lib/button";
import "choerodon-ui/lib/tooltip/style";
import _Tooltip from "choerodon-ui/lib/tooltip";
import "choerodon-ui/pro/lib/button/style";
import _Button from "choerodon-ui/pro/lib/button";
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";
import React, { useEffect } from 'react';
import { TabPage, Content, Permission, Breadcrumb, Action } from '@choerodon/boot';
import { observer } from 'mobx-react-lite';
import { withRouter } from 'react-router-dom';
import { FormattedMessage } from 'react-intl';
import NoData from "./images/nodata.png";
import { useAppTopStore } from "../stores";
import { useServiceDetailStore } from "./stores";
import HeaderButtons from "./HeaderButtons";
import ShareRule from "./modals/share-rule";
import ClickText from "../../../components/click-text";
var Column = _Table.Column;
var versionTypeStyleMaps = {
  master: {
    color: '#4D90FE',
    background: '#E6F7FF',
    border: '#91D5FF'
  },
  hotfix: {
    color: '#F76776',
    background: '#FFF1F0',
    border: '#FFA39E'
  },
  bugfix: {
    color: '#FAAD14',
    background: '#FFFBE6',
    border: '#FFE58F'
  },
  feature: {
    color: '#FD729C',
    background: '#FFF0F6',
    border: '#FFADD2'
  },
  release: {
    color: '#1FC2BB',
    background: '#E6FFFB',
    border: '#87E8DE'
  }
};

var modalKey = _Modal.key();

var modalStyle = {
  width: 380
};
var Share = withRouter(observer(function (props) {
  var _useAppTopStore = useAppTopStore(),
      intlPrefix = _useAppTopStore.intlPrefix,
      prefixCls = _useAppTopStore.prefixCls;

  var _useServiceDetailStor = useServiceDetailStore(),
      formatMessage = _useServiceDetailStor.intl.formatMessage,
      shareDs = _useServiceDetailStor.shareDs,
      id = _useServiceDetailStor.params.id,
      detailDs = _useServiceDetailStor.detailDs,
      AppState = _useServiceDetailStor.AppState;

  useEffect(function () {
    refresh();
  }, []);

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

  function renderProjectName(_ref) {
    var value = _ref.value,
        record = _ref.record;

    if (value && record.get('projectId')) {
      return /*#__PURE__*/React.createElement("span", null, value);
    }

    return /*#__PURE__*/React.createElement(FormattedMessage, {
      id: "".concat(intlPrefix, ".project.all")
    });
  }

  function renderNumber(_ref2) {
    var record = _ref2.record;
    return /*#__PURE__*/React.createElement(ClickText, {
      value: "#".concat(record.get('viewId')),
      onClick: function onClick() {
        return openModal('edit');
      },
      clickAble: true,
      permissionCode: ['choerodon.code.project.develop.app-service.ps.share.update']
    });
  }

  function renderAction(record) {
    var actionData = [{
      service: ['choerodon.code.project.develop.app-service.ps.share.delete'],
      text: formatMessage({
        id: 'delete'
      }),
      action: function action() {
        return handleDelete(record);
      }
    }];
    return /*#__PURE__*/React.createElement(Action, {
      data: actionData
    });
  }

  function openModal(type) {
    var record = shareDs.current;
    var isModify = type !== 'add';
    var shareId = isModify ? record.get('id') : null;

    _Modal.open({
      key: modalKey,
      title: formatMessage({
        id: "".concat(intlPrefix, ".share.rule.").concat(type)
      }),
      children: /*#__PURE__*/React.createElement(ShareRule, {
        intlPrefix: intlPrefix,
        prefixCls: prefixCls,
        appServiceId: id,
        shareId: shareId,
        refresh: refresh
      }),
      drawer: true,
      style: modalStyle,
      okText: formatMessage({
        id: isModify ? 'save' : 'add'
      })
    });
  }

  function handleDelete(selfRecord) {
    var record = selfRecord || shareDs.current;
    var modalProps = {
      title: formatMessage({
        id: "".concat(intlPrefix, ".rule.delete.title")
      }),
      children: formatMessage({
        id: "".concat(intlPrefix, ".rule.delete.des")
      }),
      okText: formatMessage({
        id: 'delete'
      }),
      okProps: {
        color: 'red'
      },
      cancelProps: {
        color: 'dark'
      }
    };
    shareDs["delete"](record, modalProps);
  }

  function openDetail(appServiceIds) {
    var history = props.history,
        location = props.location;
    history.push("/rducm/code-lib-management/assign".concat(location.search, "&appServiceIds=").concat(appServiceIds));
  }

  var renderShareButton = function renderShareButton() {
    var isStop = detailDs.current && !detailDs.current.get('active');
    return /*#__PURE__*/React.createElement(Permission, {
      service: ['choerodon.code.project.develop.app-service.ps.share.add']
    }, /*#__PURE__*/React.createElement("div", {
      style: {
        position: 'absolute',
        zIndex: 100,
        right: 0,
        top: '-32px'
      }
    }, /*#__PURE__*/React.createElement(_Tooltip, {
      title: isStop ? /*#__PURE__*/React.createElement(FormattedMessage, {
        id: "".concat(intlPrefix, ".button.disabled")
      }) : '',
      placement: "bottom"
    }, /*#__PURE__*/React.createElement(_Button, {
      funcType: "flat",
      icon: "playlist_add",
      onClick: function onClick() {
        return openModal('add');
      },
      disabled: isStop
    }, /*#__PURE__*/React.createElement(FormattedMessage, {
      id: "".concat(intlPrefix, ".share.rule.add")
    })))));
  };

  function renderButtons() {
    var isStop = detailDs.current && !detailDs.current.get('active');
    return [/*#__PURE__*/React.createElement(Permission, {
      service: ['choerodon.code.project.develop.app-service.ps.share.add']
    }, /*#__PURE__*/React.createElement(_Tooltip, {
      title: isStop ? /*#__PURE__*/React.createElement(FormattedMessage, {
        id: "".concat(intlPrefix, ".button.disabled")
      }) : '',
      placement: "bottom"
    }, /*#__PURE__*/React.createElement(_Button2, {
      icon: "playlist_add",
      onClick: function onClick() {
        return openModal('add');
      },
      disabled: isStop
    }, /*#__PURE__*/React.createElement(FormattedMessage, {
      id: "".concat(intlPrefix, ".share.rule.add")
    })))), /*#__PURE__*/React.createElement(Permission, {
      service: ['choerodon.code.project.develop.app-service.ps.permission.update']
    }, /*#__PURE__*/React.createElement(_Button2, {
      icon: "authority",
      onClick: function onClick() {
        return openDetail(props.match.params.id);
      }
    }, "\u6743\u9650\u7BA1\u7406"))];
  }

  function handleTableFilter(record) {
    return record.status !== 'add';
  } // eslint-disable-next-line consistent-return


  function getTitle() {
    if (detailDs.current) {
      return detailDs.current.get('name');
    }
  }

  var handleChangeListPage = function handleChangeListPage(page, pageSize) {
    shareDs.pageSize = pageSize;
    shareDs.query(page);
  };

  function handleChangeSearch(value) {
    shareDs.setQueryParameter('params', value);
    shareDs.query();
  }

  function renderTheme4Share() {
    return /*#__PURE__*/React.createElement(_Spin, {
      spinning: shareDs.status !== 'ready'
    }, renderShareButton(), /*#__PURE__*/React.createElement("div", {
      className: "c7ncd-theme4-version"
    }, /*#__PURE__*/React.createElement(_TextField, {
      placeholder: "\u641C\u7D22\u5171\u4EAB\u8BBE\u7F6E",
      style: {
        width: '100%'
      },
      suffix: /*#__PURE__*/React.createElement(_Icon, {
        type: "search"
      }),
      onEnterDown: function onEnterDown(e) {
        return handleChangeSearch(e.target.value);
      },
      onChange: handleChangeSearch
    }), shareDs.records && shareDs.records.length > 0 ? /*#__PURE__*/React.createElement(React.Fragment, null, shareDs.records.map(function (share) {
      var _versionTypeStyleMaps, _versionTypeStyleMaps2, _versionTypeStyleMaps3;

      return /*#__PURE__*/React.createElement("div", {
        className: "c7ncd-theme4-version-item"
      }, /*#__PURE__*/React.createElement("div", {
        className: "c7ncd-theme4-version-item-line"
      }, /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("p", null, /*#__PURE__*/React.createElement("span", {
        className: "c7ncd-theme4-version-item-line-viewId"
      }, "#".concat(share.get('viewId'))), /*#__PURE__*/React.createElement("span", {
        className: "c7ncd-theme4-version-item-line-around"
      }, "(\u5171\u4EAB\u8303\u56F4:", renderProjectName({
        value: share.get('projectName'),
        record: share
      }), ")")), /*#__PURE__*/React.createElement("p", {
        style: {
          marginTop: 6
        }
      }, /*#__PURE__*/React.createElement("span", {
        className: "c7ncd-theme4-version-item-line-versionType",
        style: {
          color: ((_versionTypeStyleMaps = versionTypeStyleMaps[share.get('versionType')]) === null || _versionTypeStyleMaps === void 0 ? void 0 : _versionTypeStyleMaps.color) || '#5365EA',
          background: ((_versionTypeStyleMaps2 = versionTypeStyleMaps[share.get('versionType')]) === null || _versionTypeStyleMaps2 === void 0 ? void 0 : _versionTypeStyleMaps2.background) || '#F0F5FF',
          border: "1px solid ".concat(((_versionTypeStyleMaps3 = versionTypeStyleMaps[share.get('versionType')]) === null || _versionTypeStyleMaps3 === void 0 ? void 0 : _versionTypeStyleMaps3.border) || '#ADC6FF')
        }
      }, share.get('versionType') || '未指定'), /*#__PURE__*/React.createElement("span", {
        className: "c7ncd-theme4-version-item-line-versionType"
      }, share.get('version')))), renderAction(share)));
    }), /*#__PURE__*/React.createElement(_Pagination, {
      total: shareDs.totalCount,
      pageSize: shareDs.pageSize,
      page: shareDs.currentPage,
      onChange: handleChangeListPage,
      style: {
        marginTop: '17px',
        "float": 'right'
      }
    })) : [/*#__PURE__*/React.createElement("img", {
      className: "c7ncd-theme4-version-item-nodata",
      src: NoData,
      alt: ""
    }), /*#__PURE__*/React.createElement("p", {
      className: "c7ncd-theme4-version-item-nodataText"
    }, "\u6682\u65E0\u5171\u4EAB\u8BBE\u7F6E")]));
  }

  return AppState.getCurrentTheme === 'theme4' ? renderTheme4Share() : /*#__PURE__*/React.createElement(TabPage, {
    service: ['choerodon.code.project.develop.app-service.ps.share']
  }, /*#__PURE__*/React.createElement(HeaderButtons, null, renderButtons(), /*#__PURE__*/React.createElement(_Button2, {
    icon: "refresh",
    onClick: refresh
  }, /*#__PURE__*/React.createElement(FormattedMessage, {
    id: "refresh"
  }))), /*#__PURE__*/React.createElement(Breadcrumb, {
    title: getTitle()
  }), /*#__PURE__*/React.createElement(Content, {
    className: "".concat(prefixCls, "-detail-content")
  }, /*#__PURE__*/React.createElement(_Table, {
    dataSet: shareDs,
    filter: handleTableFilter,
    pristine: true
  }, /*#__PURE__*/React.createElement(Column, {
    name: "viewId",
    renderer: renderNumber,
    align: "left",
    sortable: true
  }), /*#__PURE__*/React.createElement(Column, {
    renderer: renderAction,
    width: "0.7rem"
  }), /*#__PURE__*/React.createElement(Column, {
    name: "versionType"
  }), /*#__PURE__*/React.createElement(Column, {
    name: "version",
    sortable: true
  }), /*#__PURE__*/React.createElement(Column, {
    name: "projectName",
    renderer: renderProjectName
  }))));
}));
export default Share;