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(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import { useSetState } from 'ahooks';
import { Col, Row } from 'antd';
import dayjs from 'dayjs';
import * as echarts from 'echarts';
import _ from 'lodash';
import { useContext } from 'react';
import { VmBarCharts, VmFactor, VmLineCharts, VmWrapper } from "../../../../_components";
import useSettings from "../../../../_hooks/useSettings";
import { BaseService, OilService } from "../../../api";
import darkMileageIcon from "../../../imgs/factor/dark_mileage.png";
import darkOilIcon from "../../../imgs/factor/dark_oil.png";
import mileageIcon from "../../../imgs/factor/mileage.png";
import oilIcon from "../../../imgs/factor/oil.png";
import { CarContext } from "../../../index";
import "./index.less";
import List from "./List";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
var OilConsumption = function OilConsumption() {
  var _oilLineRes$values;
  var _useSettings = useSettings(),
    isDark = _useSettings.isDark;
  var _useContext = useContext(CarContext),
    id = _useContext.id,
    sDate = _useContext.startDate,
    eDate = _useContext.endDate;
  var _useSetState = useSetState({
      startDate: sDate ? sDate : dayjs().subtract(6, 'day'),
      endDate: eDate ? eDate : dayjs()
    }),
    _useSetState2 = _slicedToArray(_useSetState, 2),
    state = _useSetState2[0],
    setState = _useSetState2[1];
  var startDate = dayjs(state.startDate).format('YYYY-MM-DD');
  var endDate = dayjs(state.endDate).format('YYYY-MM-DD');
  var filterChange = function filterChange(dateValue) {
    setState({
      startDate: dateValue[0],
      endDate: dateValue[1]
    });
  };
  var _OilService$useSummar = OilService.useSummary(id, startDate, endDate),
    summaryRes = _OilService$useSummar.summaryRes;
  var _OilService$useOilLin = OilService.useOilLine(id, startDate, endDate),
    oilLineRes = _OilService$useOilLin.oilLineRes,
    loading = _OilService$useOilLin.loading;
  var _BaseService$useDaily = BaseService.useDailyCarRun(id, startDate, endDate),
    dailyCarRunRes = _BaseService$useDaily.dailyCarRunRes,
    loadingDaily = _BaseService$useDaily.loading;
  var totalMileage = _.sumBy(dailyCarRunRes, 'sumMileage');
  return /*#__PURE__*/_jsxs("div", {
    className: 'vtx-car-oil-wrap',
    children: [/*#__PURE__*/_jsx("div", {
      style: {
        height: 348,
        flexShrink: 0
      },
      children: /*#__PURE__*/_jsx(VmWrapper, {
        title: '油耗汇总',
        filterType: "rangePicker",
        dateValue: [state.startDate, state.endDate],
        filterChange: filterChange,
        children: /*#__PURE__*/_jsxs(Row, {
          children: [/*#__PURE__*/_jsx(Col, {
            span: 12,
            children: /*#__PURE__*/_jsxs(Row, {
              align: 'middle',
              justify: 'center',
              children: [/*#__PURE__*/_jsx(VmFactor, {
                name: '总油耗',
                unit: 'L',
                value: (summaryRes === null || summaryRes === void 0 ? void 0 : summaryRes.totalOil) || 0,
                img: isDark ? darkOilIcon : oilIcon
              }), /*#__PURE__*/_jsx(VmBarCharts, {
                dataList: [{
                  name: '每日油耗',
                  data: (oilLineRes === null || oilLineRes === void 0 || (_oilLineRes$values = oilLineRes.values) === null || _oilLineRes$values === void 0 ? void 0 : _oilLineRes$values[0]) || []
                }],
                unit: 'L',
                xAxis: oilLineRes.names || [],
                yAxisList: [{
                  unit: 'L',
                  name: '(L)',
                  key: ''
                }],
                loading: loading,
                height: 216,
                showLegend: false
              })]
            })
          }), /*#__PURE__*/_jsx(Col, {
            span: 12,
            children: /*#__PURE__*/_jsxs(Row, {
              align: 'middle',
              justify: 'center',
              children: [/*#__PURE__*/_jsx(VmFactor, {
                name: '总里程',
                unit: 'km',
                value: Number(totalMileage.toFixed(2)) || 0,
                img: isDark ? darkMileageIcon : mileageIcon
              }), /*#__PURE__*/_jsx(VmLineCharts, {
                dataList: [{
                  name: '每日里程',
                  data: _.map(dailyCarRunRes, 'sumMileage'),
                  color: '#0FB964',
                  smooth: true,
                  symbol: 'none',
                  areaStyle: {
                    color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                      offset: 0,
                      color: 'rgba(15, 185, 100, 0.30)'
                    },
                    // 半透明
                    {
                      offset: 1,
                      color: 'rgba(15, 185, 100, 0)'
                    } // 更透明
                    ])
                  }
                }],
                loading: loadingDaily,
                unit: 'km',
                xAxis: oilLineRes.names || [],
                height: 216,
                showLegend: false
              })]
            })
          })]
        })
      })
    }), /*#__PURE__*/_jsx("div", {
      style: {
        flex: 1,
        minHeight: 0
      },
      children: /*#__PURE__*/_jsx(VmWrapper, {
        title: '油耗明细',
        children: /*#__PURE__*/_jsx(List, {
          startDate: startDate,
          endDate: endDate
        })
      })
    })]
  });
};
export default OilConsumption;