"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _antd = require("antd");

var _jsCookie = _interopRequireDefault(require("js-cookie"));

var _city = _interopRequireDefault(require("./city.json"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function _readOnlyError(name) { throw new Error("\"" + name + "\" is read-only"); }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

var ERR_OK = '0000';

var Site =
/*#__PURE__*/
function () {
  function Site(props) {
    _classCallCheck(this, Site);

    this.weatherCity = null;
    this.globalConfig = props.global;
  }

  _createClass(Site, [{
    key: "init",
    value: function init(config) {}
  }, {
    key: "initWeatherCity",
    value: function initWeatherCity() {
      var _this = this;

      try {
        if (navigator.onLine && this.globalConfig && this.globalConfig.userInfo && this.globalConfig.userInfo.site) {
          http.getCity().then(function (res) {
            if (res && !isNaN(res.cid)) {
              var cityResult = JSON.parse(_city["default"]);
              var city = cityResult[res.cid];
              _this.weatherCity = city ? city : null;
            }
          });
        }
      } catch (e) {
        console.error(e);
      }
    }
  }, {
    key: "getCityStationsForUser",
    value: function getCityStationsForUser() {
      var _this2 = this;

      debugger;
      var loginName = this.globalConfig.userInfo.loginName;
      var self = this;
      Http.getAllGroupsInfoForUser({
        loginName: loginName,
        'request.preventCache': new Date().getTime()
      }, {
        ignoreSite: true
      }).then(function (res) {
        debugger;

        if (res && res.say.errMsg === '' && res.say.statusCode === ERR_OK) {
          var result = res.getMe;
          var city = self.weatherCity;
          var arr = [];
          self.globalConfig.userInfo.groupType = "";
          self.globalConfig.userInfo.Groups = res.getMe;
          self.globalConfig.userInfo.Industries = new Array();

          if (self.globalConfig.userInfo.Groups && self.globalConfig.userInfo.Groups.length) {
            self.globalConfig.userInfo.Groups.forEach(function (group) {
              if (group.industry && self.globalConfig.userInfo.Industries.indexOf(group.industry) < 0) self.globalConfig.userInfo.Industries.push(group.industry);
            });
          }

          if (self.globalConfig.userInfo && self.globalConfig.userInfo.site && self.globalConfig.userInfo.Groups && self.globalConfig.userInfo.Groups.length) {
            var _indestryItem = self.globalConfig.userInfo.Groups.find(function (item) {
              return item.site == self.globalConfig.userInfo.site;
            });

            if (_indestryItem && _indestryItem.industry) {
              self.globalConfig.Industry = _indestryItem.industry;
            }
          }

          var allStation = [];
          var projectStation = [];
          debugger;

          if (self.globalConfig.Industry) {
            allStation = result.filter(function (item) {
              return item.industry == self.globalConfig.Industry;
            });
            self.showStations = allStation.filter(function (item) {
              return item.promoteIndex && item.promoteIndex > 0;
            });
            projectStation = allStation.filter(function (item) {
              return !item.promoteIndex;
            });
          } else {
            allStation = result;
          }

          if (allStation.length) {
            var allChoice = false;
            allStation.forEach(function (val) {
              if (val.promoteIndex == 0) {
                allChoice = true;
              }
            });

            if (!allChoice) {// me.$el.find('#changeCityWays').hide();
              // me.$el.find('.LMBcityBox').hide();
              // me.$el.find('div.cities').css('min-width', 'initial');
              // me.$el.find('.focusStations').addClass('LMBcolumnList');
            }

            if (allStation.length > 120) {// $(me.el).find("div.cities div.cityContent").css("height", '335px');
            }

            var _enterprise = null;

            if (self.globalConfig.userInfo.site && self.globalConfig.userInfo.site.length) {
              _enterprise = allStation.filter(function (enterprise) {
                return enterprise.site == self.globalConfig.userInfo.site;
              });
            }

            if (_enterprise && _enterprise.length) {
              if (_enterprise[0].groupName.indexOf("演示" >= 0)) {
                arr[0] = _enterprise[0].groupName;
              } else {
                arr = _enterprise[0].city.split('/');
              }

              var tmp = _enterprise[0].city.split('/')[1];

              city = tmp.substr(0, tmp.length - 1);
              self.globalConfig.userInfo.groupType = _enterprise[0].groupType;
            } else {
              arr = allStation[0].city.split('/');
              var _tmp = arr[1];
              city = _tmp.substr(0, _tmp.length - 1);
              self.globalConfig.userInfo.groupType = allStation[0].groupType;
            }
          }

          if (allStation.length > 1) {
            var currentStationName = arr[arr.length - 1]; // $(me.el).find('span.city').html(currentStationName);
            // $(me.el).find(".item").show();
            // $(me.el).find("div.cityAndWeather span.Weather").css('border-left', '1px solid #aaa');

            self.AvailableofRegionName(projectStation);
            self.buildCitySelectTemple(projectStation);
          } //只有演示环境出现友好提示


          if (allStation.length == self.showStations.length) {
            self.isOnlyDisplay = true; // $(me.el).find(".city_pane").hide().siblings('.noStations').css('display', 'flex')
          } else {
            self.AvailableofRegionName(projectStation);
            self.buildCitySelectTemple(projectStation);
          }

          console.log("showStations", self.showStations);
          console.log("city", city);
          return city;

          _this2.insertYSStation(); // if(city) {
          //     Http.getWeather({
          //         city: city
          //     }).then(res => {
          //         if(res.say.statusCode === ERR_OK) {
          //             const firtValue = res.getMe[0];
          //             if(firtValue.cityName) {
          //                 const text = firtValue.forcastFirst.split(' ')[1];
          //                 const imgPath = firtValue.presentPictureFirst.replace('gif', 'svg')
          //                 return (
          //                     <>
          //                      <img src={`https://panda-water.cn/web4/assets/images/weather2/${imgPath}`}/>
          //                      <span>{text + "  " + firtValue.temperatureFirst}</span>
          //                     </>
          //                 )
          //             }
          //         }
          //     })
          // }

        }
      });
    }
  }, {
    key: "insertYSStation",
    value: function insertYSStation() {
      var list = [];
      var hot = ["HOT", "县", "市", "New"];
      this.showStations.sort(function (a, b) {
        return a.promoteIndex - b.promoteIndex;
      });
    }
  }, {
    key: "getNumberofRegion",
    value: function getNumberofRegion() {}
  }, {
    key: "AvailableofRegionName",
    value: function AvailableofRegionName() {}
  }, {
    key: "writeCookie",
    value: function writeCookie(tk, site) {
      var date = new Date();
      date.setTime(date.getTime() + 24 * 60 * 60 * 1000);

      _jsCookie["default"].set("token", tk, {
        expires: date.toGMTString(),
        path: "/"
      });

      var encodeSite = Base64.encode(encodeURIComponent(site));

      _jsCookie["default"].set("site", encodeSite, {
        expires: date.toGMTString(),
        path: "/"
      });

      var loginSite = this.getLocalSites();
      loginSite[token] = site;
      localStorage.setItem("loginSite", JSON.stringify(loginSite)); // window.location.reload
    }
  }, {
    key: "changeGroup",
    value: function changeGroup(event) {
      var _this3 = this;

      c;
      Http.generateTokenQuick({
        loginName: this.globalConfig.userInfo.loginName
      }).then(function (res) {
        var token = res.token;

        if (token) {
          _this3.beforeChangeCheck(token, "");
        }
      })["catch"](function (error) {
        _antd.message.warning("切换企业失败");
      });
    }
  }, {
    key: "beforeChangeCheck",
    value: function beforeChangeCheck(token, site) {
      var _this4 = this;

      Http.getUserInfo({
        token: token,
        subOID: 'subOID',
        site: site
      }).then(function (res) {
        if (res && !res.errMsg) {
          _this4.writeCookie(token, site);
        } else {
          _antd.message.warning('企业切换失败，请联系管理员排查问题！');
        }
      })["catch"](function (e) {
        _antd.message.warning('企业切换失败，请联系管理员排查问题！');
      });
    }
  }, {
    key: "AvailableofRegionName",
    value: function AvailableofRegionName() {}
  }, {
    key: "buildCitySelectTemple",
    value: function buildCitySelectTemple() {}
  }, {
    key: "getLocalSites",
    value: function getLocalSites() {
      var localSite = localStorage.getItem("loginSite"),
          value = {};

      if (localSite) {
        value = (_readOnlyError("value"), JSON.parse(localSite));
      }

      return value;
    }
  }]);

  return Site;
}();

var _default = Site;
exports["default"] = _default;