function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
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, VmTable, VmWrapper } from "../../../../_components";
import useSettings from "../../../../_hooks/useSettings";
import { BaseService, WaterService } 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 { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
var WaterConsumption = function WaterConsumption() {
  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 _WaterService$useSumm = WaterService.useSummary(id, startDate, endDate),
    summaryRes = _WaterService$useSumm.summaryRes;
  var _WaterService$useReco = WaterService.useRecord(id, startDate, endDate),
    recordRes = _WaterService$useReco.recordRes,
    loading = _WaterService$useReco.loading;
  var _BaseService$useDaily = BaseService.useDailyCarRun(id, startDate, endDate),
    dailyCarRunRes = _BaseService$useDaily.dailyCarRunRes,
    loadingDaily = _BaseService$useDaily.loading;
  var list = _.sortBy((recordRes === null || recordRes === void 0 ? void 0 : recordRes.rows) || [], 'day');
  var totalMileage = _.sumBy(dailyCarRunRes, 'sumMileage');
  var columns = [{
    title: '日期',
    dataIndex: 'day',
    key: 'day',
    sorter: function sorter(a, b) {
      return a.day > b.day ? 1 : -1;
    }
  }, {
    title: '总水耗(L)',
    dataIndex: 'useValue',
    key: 'useValue',
    align: 'right',
    sorter: function sorter(a, b) {
      return a.useValue - b.useValue;
    }
  }, {
    title: '总里程(km)',
    dataIndex: 'sumMileage',
    key: 'sumMileage',
    sorter: function sorter(a, b) {
      return a.sumMileage - b.sumMileage;
    },
    align: 'right'
  }];
  var tableProps = {
    pagination: {
      total: (recordRes === null || recordRes === void 0 ? void 0 : recordRes.total) || 0,
      defaultPageSize: 10
    },
    height: 360,
    dataSource: ((recordRes === null || recordRes === void 0 ? void 0 : recordRes.rows) || []).map(function (item) {
      var _dailyCarRunRes$find;
      var sumMileage = ((_dailyCarRunRes$find = dailyCarRunRes.find(function (v) {
        return v.reportDate === item.day;
      })) === null || _dailyCarRunRes$find === void 0 ? void 0 : _dailyCarRunRes$find.sumMileage) || 0;
      return _objectSpread(_objectSpread({}, item), {}, {
        sumMileage: sumMileage
      });
    }),
    loading: loading
  };
  return /*#__PURE__*/_jsxs("div", {
    className: 'vtx-car-water-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.totalWater) || 0,
                img: isDark ? darkOilIcon : oilIcon
              }), /*#__PURE__*/_jsx(VmBarCharts, {
                dataList: [{
                  name: '每日水耗',
                  data: _.map(list, 'useValue')
                }],
                unit: 'L',
                xAxis: _.map(list, 'day'),
                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: _.map(dailyCarRunRes, 'reportDate'),
                height: 216,
                showLegend: false
              })]
            })
          })]
        })
      })
    }), /*#__PURE__*/_jsx("div", {
      style: {
        flex: 1,
        minHeight: 0
      },
      children: /*#__PURE__*/_jsx(VmWrapper, {
        title: '水耗明细',
        children: /*#__PURE__*/_jsx(VmTable, _objectSpread({
          columns: columns,
          rowKey: "id"
        }, tableProps))
      })
    })]
  });
};
export default WaterConsumption;