import "choerodon-ui/pro/lib/form/style";
import _Form from "@hzero-front-ui/c7n-ui/lib/FormPro";
import "choerodon-ui/pro/lib/select/style";
import _Select from "choerodon-ui/pro/lib/select";

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

/* eslint-disable max-len */
import React, { useState, useEffect, useCallback } from 'react';
import { observer } from 'mobx-react-lite';
import { Page, Header, Content, Breadcrumb } from '@choerodon/boot';
import _ from 'lodash';
import moment from 'moment';
import HeaderButtons from "../Component/HeaderButtons";
import ChartSwitch from "../Component/ChartSwitch";
import "./BuildNumber.less";
import TimePicker from "../Component/TimePicker";
import NoChart from "../Component/NoChart";
import BuildTable from "./BuildTable/BuildTable";
import LoadingBar from "../../../components/loading";
import BuildChart from "./BuildChart";
import { useReportsStore } from "../stores";
import { useBuildNumberStore } from "./stores";
var Option = _Select.Option;
var BuildNumber = observer(function () {
  var _useReportsStore = useReportsStore(),
      ReportsStore = _useReportsStore.ReportsStore,
      _useReportsStore$Repo = _useReportsStore.ReportsStore,
      getProRole = _useReportsStore$Repo.getProRole,
      changeIsRefresh = _useReportsStore$Repo.changeIsRefresh,
      setAllData = _useReportsStore$Repo.setAllData,
      setBuildNumber = _useReportsStore$Repo.setBuildNumber,
      setStartTime = _useReportsStore$Repo.setStartTime,
      setEndTime = _useReportsStore$Repo.setEndTime,
      setAppId = _useReportsStore$Repo.setAppId,
      setPageInfo = _useReportsStore$Repo.setPageInfo,
      setStartDate = _useReportsStore$Repo.setStartDate,
      setEndDate = _useReportsStore$Repo.setEndDate,
      setAllApps = _useReportsStore$Repo.setAllApps,
      loadAllApps = _useReportsStore$Repo.loadAllApps,
      pageInfo = _useReportsStore$Repo.pageInfo,
      getAllApps = _useReportsStore$Repo.getAllApps,
      appId = _useReportsStore$Repo.appId,
      echartsLoading = _useReportsStore$Repo.echartsLoading,
      isRefresh = _useReportsStore$Repo.isRefresh,
      getAppId = _useReportsStore$Repo.getAppId,
      loadBuildNumber = _useReportsStore$Repo.loadBuildNumber,
      loadBuildTable = _useReportsStore$Repo.loadBuildTable,
      formatMessage = _useReportsStore.intl.formatMessage,
      AppState = _useReportsStore.AppState,
      history = _useReportsStore.history,
      _useReportsStore$hist = _useReportsStore.history.location,
      state = _useReportsStore$hist.state,
      search = _useReportsStore$hist.search;

  var _useBuildNumberStore = useBuildNumberStore(),
      BuildNumberSelectDataSet = _useBuildNumberStore.BuildNumberSelectDataSet;

  var record = BuildNumberSelectDataSet.current;

  var _useState = useState('seven'),
      _useState2 = _slicedToArray(_useState, 2),
      dateType = _useState2[0],
      setDateType = _useState2[1];

  useEffect(function () {
    changeIsRefresh(true);
    loadDatas();
    return function () {
      setAllData([]);
      setBuildNumber({});
      setStartTime(moment().subtract(6, 'days'));
      setEndTime(moment());
      setAppId(null);
      setPageInfo({
        pageNum: 0,
        total: 0,
        pageSize: 10
      });
      setStartDate();
      setEndDate();
      setAllApps([]);
    };
  }, []);
  useEffect(function () {
    record.set('buildNumberApps', appId);
  }, [appId]);
  /**
   * 加载数据
   */

  var loadDatas = function loadDatas() {
    var historyAppId = null;

    if (state && state.appId) {
      historyAppId = state.appId;
    }

    var id = AppState.currentMenuType.id;
    loadAllApps(id).then(function (data) {
      if (data && data.length) {
        var selectApp = data[0].id;

        if (historyAppId) {
          selectApp = historyAppId;
        }

        setAppId(selectApp);
        loadCharts();
      } else {
        ReportsStore.judgeRole(['choerodon.code.project.develop.app-service.ps.create']);
      }
    });
  };
  /**
   * 刷新
   */


  var handleRefresh = function handleRefresh() {
    var id = AppState.currentMenuType.id;
    loadAllApps(id);
    loadCharts(pageInfo);
  };
  /**
   * 选择应用
   * @param value
   */


  var handleAppSelect = function handleAppSelect(value) {
    setAppId(value);
    loadCharts();
  };

  var loadCharts = function loadCharts(pageInfoCurrent) {
    var projectId = AppState.currentMenuType.id;
    var startTime = ReportsStore.getStartTime.format('YYYY-MM-DD HH:mm:ss');
    var appIdCurrent = ReportsStore.getAppId;
    var endTime = ReportsStore.getEndTime.format('YYYY-MM-DD HH:mm:ss');
    loadBuildNumber(projectId, appIdCurrent, startTime, endTime);

    if (pageInfoCurrent) {
      loadBuildTable(projectId, appIdCurrent, startTime, endTime, pageInfoCurrent.current, pageInfoCurrent.pageSize);
    } else {
      loadBuildTable(projectId, appIdCurrent, startTime, endTime);
    }
  };

  var handleDateChoose = function handleDateChoose(type) {
    setDateType(type);
  };

  var _AppState$currentMenu = AppState.currentMenuType,
      id = _AppState$currentMenu.id,
      name = _AppState$currentMenu.name,
      type = _AppState$currentMenu.type,
      organizationId = _AppState$currentMenu.organizationId;
  var backPath = state && state.backPath ? state.backPath : 'reports';
  var content = getAllApps.length ? /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
    className: "c7n-buildNumber-select"
  }, /*#__PURE__*/React.createElement(_Form, {
    dataSet: BuildNumberSelectDataSet,
    className: "c7n-app-select_247"
  }, /*#__PURE__*/React.createElement(_Select, {
    name: "buildNumberApps" // defaultValue={appId}
    // value={appId}
    ,
    optionFilterProp: "children",
    filterOption: function filterOption(input, option) {
      return option.props.children.props.children.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0;
    },
    filter: true,
    searchable: true,
    onChange: handleAppSelect,
    clearButton: false
  }, _.map(getAllApps, function (app, index) {
    return /*#__PURE__*/React.createElement(Option, {
      value: app.id,
      key: index
    }, app.name);
  }))), /*#__PURE__*/React.createElement(TimePicker, {
    startTime: ReportsStore.getStartDate,
    endTime: ReportsStore.getEndDate,
    func: loadCharts,
    type: dateType,
    onChange: handleDateChoose,
    store: ReportsStore
  })), /*#__PURE__*/React.createElement(BuildChart, {
    ReportsStore: ReportsStore,
    echartsLoading: echartsLoading,
    height: "400px",
    top: "15%",
    languageType: "report"
  }), /*#__PURE__*/React.createElement(BuildTable, null)) : /*#__PURE__*/React.createElement(NoChart, {
    getProRole: getProRole,
    type: "app"
  });
  return /*#__PURE__*/React.createElement(Page, {
    className: "c7n-region c7n-ciPipeline"
  }, /*#__PURE__*/React.createElement(Header, {
    title: formatMessage({
      id: 'report.build-number.head'
    })
  }, /*#__PURE__*/React.createElement(HeaderButtons, {
    refresh: handleRefresh,
    reportKey: "build-number",
    reportType: "develop"
  })), /*#__PURE__*/React.createElement(Breadcrumb, {
    title: formatMessage({
      id: 'report.build-number.head'
    })
  }), /*#__PURE__*/React.createElement(Content, {
    className: "c7n-buildNumber-content"
  }, isRefresh ? /*#__PURE__*/React.createElement(LoadingBar, {
    display: isRefresh
  }) : content));
});
export default BuildNumber;