import "choerodon-ui/pro/lib/tooltip/style";
import _Tooltip from "choerodon-ui/pro/lib/tooltip";
import "choerodon-ui/pro/lib/table/style";
import _Table from "@hzero-front-ui/c7n-ui/lib/TablePro";
import React, { Fragment } from 'react';
import { observer } from 'mobx-react-lite';
import { Action } from '@choerodon/boot';
import StatusTags from "../../../../../components/status-tag";
import { useResourceStore } from "../../../stores";
import { usePVCStore } from "./stores";
import Modals from "./modals";
import ResourceListTitle from "../../components/resource-list-title";
import "./index.less";
var Column = _Table.Column;
var statusStyle = {
  width: 54,
  marginRight: 8,
  height: '.16rem',
  lineHeight: '.16rem'
};
var pvcContent = observer(function (props) {
  var _useResourceStore = useResourceStore(),
      prefixCls = _useResourceStore.prefixCls,
      intlPrefix = _useResourceStore.intlPrefix,
      parentId = _useResourceStore.resourceStore.getSelectedMenu.parentId,
      treeDs = _useResourceStore.treeDs;

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

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

  function getEnvIsNotRunning() {
    var envRecord = treeDs.find(function (record) {
      return record.get('key') === parentId;
    });
    var connect = envRecord.get('connect');
    return !connect;
  }

  function renderName(_ref) {
    var record = _ref.record,
        value = _ref.value;
    var status = record.get('status');
    var color = 'rgba(0, 0, 0, 0.26)';

    switch (status) {
      case 'Pending':
      case 'Terminating':
      case 'Operating':
        color = '#4D90FE';
        break;

      case 'Bound':
        color = '#FFB100';
        break;

      case 'Lost':
        color = 'rgba(0, 0, 0, 0.26)';
        break;

      default:
    }

    if (status) {
      return /*#__PURE__*/React.createElement(Fragment, null, /*#__PURE__*/React.createElement(StatusTags, {
        name: status,
        color: color,
        style: statusStyle
      }), /*#__PURE__*/React.createElement(_Tooltip, {
        title: value
      }, /*#__PURE__*/React.createElement("span", null, value)));
    }
  }

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

  function renderAction(_ref2) {
    var record = _ref2.record;
    var status = record.get('status');
    var disabled = getEnvIsNotRunning() || status === 'Terminating' || status === 'Operating';

    if (disabled) {
      return;
    }

    var action = [{
      service: ['choerodon.code.project.deploy.app-deployment.resource.ps.delete-pvc'],
      text: formatMessage({
        id: 'delete'
      }),
      action: handleDelete
    }];
    return /*#__PURE__*/React.createElement(Action, {
      data: action
    });
  }

  return /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefixCls, "-PVC")
  }, /*#__PURE__*/React.createElement(Modals, null), /*#__PURE__*/React.createElement(ResourceListTitle, {
    type: "pvcs"
  }), /*#__PURE__*/React.createElement(_Table, {
    dataSet: tableDs,
    border: false,
    queryBar: "bar"
  }, /*#__PURE__*/React.createElement(Column, {
    name: "name",
    renderer: renderName,
    sortable: true
  }), /*#__PURE__*/React.createElement(Column, {
    renderer: renderAction,
    width: 70
  }), /*#__PURE__*/React.createElement(Column, {
    name: "pvName"
  }), /*#__PURE__*/React.createElement(Column, {
    name: "type"
  }), /*#__PURE__*/React.createElement(Column, {
    name: "accessModes",
    sortable: true
  }), /*#__PURE__*/React.createElement(Column, {
    name: "requestResource"
  })));
});
export default pvcContent;