"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"), _typeof = require("@babel/runtime/helpers/typeof");

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

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")), _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")), _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")), _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized")), _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits")), _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn")), _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf")), _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")), _react = _interopRequireWildcard(require("react")), _propTypes = _interopRequireDefault(require("prop-types")), _amosAntd = require("amos-antd"), _amosFramework = require("amos-framework"), _processor = require("../../../utils/processor"), _urlConsts = require("../../../consts/urlConsts"), _amosTool = require("amos-tool"), _DeviceEnum = require("./../common/DeviceEnum"), _DeviceLang = require("./../common/DeviceLang"), _validate = require("./../../common/utils/validate"), _UpLoadImg = _interopRequireDefault(require("./../common/UpLoadImg")), _DeviceProperty = _interopRequireDefault(require("./deviceDetail/DeviceProperty")), _DeviceInterface = _interopRequireDefault(require("./deviceDetail/DeviceInterface")), _DeviceCapacity = _interopRequireDefault(require("./deviceDetail/DeviceCapacity"));

function _getRequireWildcardCache(e) {
  if ("function" != typeof WeakMap) return null;
  var t = new WeakMap, a = new WeakMap;
  return (_getRequireWildcardCache = function(e) {
    return e ? a : t;
  })(e);
}

function _interopRequireWildcard(e, t) {
  if (!t && e && e.__esModule) return e;
  if (null === e || "object" !== _typeof(e) && "function" != typeof e) return {
    default: e
  };
  var a = _getRequireWildcardCache(t);
  if (a && a.has(e)) return a.get(e);
  var r = {}, i = Object.defineProperty && Object.getOwnPropertyDescriptor;
  for (var l in e) if ("default" !== l && Object.prototype.hasOwnProperty.call(e, l)) {
    var n = i ? Object.getOwnPropertyDescriptor(e, l) : null;
    n && (n.get || n.set) ? Object.defineProperty(r, l, n) : r[l] = e[l];
  }
  return r.default = e, a && a.set(e, r), r;
}

function _createSuper(e) {
  var t = _isNativeReflectConstruct();
  return function() {
    var a, r = (0, _getPrototypeOf2.default)(e);
    if (t) {
      var i = (0, _getPrototypeOf2.default)(this).constructor;
      a = Reflect.construct(r, arguments, i);
    } else a = r.apply(this, arguments);
    return (0, _possibleConstructorReturn2.default)(this, a);
  };
}

function _isNativeReflectConstruct() {
  if ("undefined" == typeof Reflect || !Reflect.construct) return !1;
  if (Reflect.construct.sham) return !1;
  if ("function" == typeof Proxy) return !0;
  try {
    return Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], (function() {}))), 
    !0;
  } catch (e) {
    return !1;
  }
}

require("./device-detail.scss");

var maxDescLength = 500, FormItem = _amosAntd.Form.Item, createForm = _amosAntd.Form.create(), Option = _amosFramework.Select.Option, deviceCategory = (0, 
_DeviceLang.enums2array)(_DeviceEnum.deviceCategoryEnum).map((function(e) {
  return _react.default.createElement(Option, {
    key: e.key,
    value: "".concat(e.key)
  }, e.label);
})), PrivateDeviceDetail = function(e) {
  (0, _inherits2.default)(a, e);
  var t = _createSuper(a);
  function a(e) {
    var r;
    return (0, _classCallCheck2.default)(this, a), r = t.call(this, e), (0, _defineProperty2.default)((0, 
    _assertThisInitialized2.default)(r), "getDeviceInfoById", (function(e) {
      var t = "".concat(_urlConsts.OltSerUrl.fetchDeviceById, "/").concat(e);
      _processor.AmosFetch.get(t).then((function(e) {
        if (e.ok) return e.json();
        throw new Error("从服务端获取数据失败！");
      })).then((function(e) {
        if ("SUCCESS" === e.result) {
          var t = e.dataList;
          r.setState({
            device: t,
            displayImgUrl: t.img
          });
        }
      }));
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(r), "getBase64", (function(e, t) {
      var a = new FileReader;
      a.addEventListener("load", (function() {
        return t(a.result);
      })), a.readAsDataURL(e);
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(r), "setImgFile", (function(e) {
      r.setState({
        imgFile: e
      });
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(r), "upLoadImgToSave", (function(e) {
      var t = {
        binaryImg: e,
        imgName: r.state.imgFile.name
      };
      r.saveDeviceParam(t);
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(r), "handleEditParam", (function(e) {
      !1 !== r.state.textEdit ? r.props.form.validateFields((function(e, t) {
        if (!e) {
          var a = r.state.deviceName;
          if (void 0 === a || "" !== a) {
            var i = new FileReader;
            i.addEventListener("load", (function() {
              return r.upLoadImgToSave(i.result);
            }));
            var l = r.state.device.img, n = r.state.imgFile;
            "{}" === JSON.stringify(n) || l === n.name ? r.saveDeviceParam() : i.readAsBinaryString(r.state.imgFile), 
            r.setState({
              textEdit: !r.state.textEdit
            });
          } else _amosFramework.message.success("设备名称不能为空！");
        }
      })) : r.setState({
        textEdit: !r.state.textEdit
      });
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(r), "saveDeviceParam", (function(e) {
      var t = r.props.carInfo.id, a = r.props.form.getFieldValue("model"), i = r.props.form.getFieldValue("type"), l = r.props.form.getFieldValue("descr"), n = r.props.form.getFieldValue("viewPath"), c = r.props.form.getFieldValue("deviceName"), s = r.props.form.getFieldValue("industryId"), d = {
        id: t,
        model: a,
        type: i,
        viewPath: n,
        desc: l,
        name: c
      };
      Number.isInteger(s) && (0, _extends2.default)(d, {
        industryId: s
      });
      var u = "".concat(_urlConsts.OltSerUrl.deviceBasicInfoUpdate);
      void 0 !== e && (0, _extends2.default)(d, e), _processor.AmosFetch.post(u, {
        data: d
      }).then((function(e) {
        if (e.ok) return e.json();
        throw new Error("从服务端获取数据失败！");
      })).then((function(e) {
        "SUCCESS" === e.result ? (-1 === e.dataList ? _amosFramework.message.danger("设备原型名称重复！") : _amosFramework.message.success("编辑成功！"), 
        r.setState({
          imgFile: {}
        })) : _amosFramework.message.danger("编辑失败！"), r.getDeviceInfoById(t);
      }));
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(r), "handleAddProperty", (function(e) {})), 
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(r), "handleChange", (function(e) {
      "uploading" !== e.file.status ? "done" === e.file.status && r.getBase64(e.file.originFileObj, (function(e) {
        return r.setState({
          imageUrl: e,
          loading: !1
        });
      })) : r.setState({
        loading: !0
      });
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(r), "beforeUpload", (function(e) {
      var t = "image/jpeg" === e.type;
      t || _amosFramework.message.error("You can only upload JPG file!");
      var a = e.size / 1024 / 1024 < 2;
      return a || _amosFramework.message.error("Image must smaller than 2MB!"), t && a;
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(r), "saveDeviceInterfaces", (function(e) {
      r.setState({
        deviceInterfaces: e
      });
    })), r.state = {
      textEdit: !1,
      loading: !1,
      interfaceList: [],
      device: {},
      displayImgUrl: "",
      imageUrl: "",
      imgFile: {},
      imgInfo: {},
      deviceInterfaces: []
    }, r;
  }
  return (0, _createClass2.default)(a, [ {
    key: "componentDidMount",
    value: function() {
      var e = this.props.carInfo.id;
      this.getDeviceInfoById(e);
    }
  }, {
    key: "render",
    value: function() {
      var e = this.props.form.getFieldProps, t = this.state, a = t.textEdit, r = t.device, i = t.deviceInterfaces, l = r.type, n = r.model, c = r.descr, s = r.name, d = r.viewPath, u = (r.img, 
      r.visibility), o = r.industryId, f = !1 === a ? "device-detail-title-button" : "device-detail-title-button-hidden", m = !1 === a ? "device-detail-title-button-hidden" : "device-detail-title-button", p = !1 === a ? "device-detail-title-content" : "device-detail-title-content-hidden", v = !1 === a ? "device-detail-title-content-hidden" : "device-detail-title-content", _ = this.props.equiptemplateIndustryOptions ? this.props.equiptemplateIndustryOptions : [], g = "";
      _.forEach((function(e, t, a) {
        e.value === o && (g = e.label ? e.label : "");
      }));
      var h = e("deviceName", {
        initialValue: s || "",
        rules: [ {
          required: !0,
          message: "名称不能为空！"
        }, {
          validator: (0, _validate.noSpaceLengthValidate)(null, !0, "名称不能为空！")
        } ]
      }), y = e("model", {
        initialValue: n || "",
        rules: [ {
          required: !0,
          message: "型号不能为空！"
        }, {
          validator: (0, _validate.noSpaceLengthValidate)(null, !0, "型号不能为空！")
        } ]
      }), I = e("type", {
        initialValue: "".concat(l),
        rules: [ {
          required: !0,
          message: "类别不能为空！"
        } ]
      }), E = e("industryId", {
        initialValue: "".concat(g),
        rules: [ {
          required: !0,
          message: "所属行业不能为空！"
        } ]
      }), b = (e("viewPath", {
        initialValue: d || "",
        rules: []
      }), e("descr", {
        initialValue: c || "",
        rules: [ {
          validator: (0, _validate.noSpaceLengthValidate)(maxDescLength, !1, null)
        } ]
      })), D = this.state.displayImgUrl, q = "" === D ? "".concat(_urlConsts.OltSerUrl.fetchDeviceImg, "/default.png") : "".concat(_urlConsts.OltSerUrl.fetchDeviceImg, "/").concat(D);
      return _react.default.createElement("div", {
        className: "device-detail"
      }, _react.default.createElement("div", {
        className: "device-detail-cell"
      }, _react.default.createElement("div", {
        className: "device-detail-title"
      }, _react.default.createElement("label", null, "设备信息"), 1 === u ? _react.default.createElement("span", null, _react.default.createElement("div", {
        onClick: this.handleEditParam,
        className: f
      }, _react.default.createElement("img", {
        src: require("./../../../assets/iot/img/list_edit.png"),
        alt: ""
      }), _react.default.createElement("label", null, "编辑")), _react.default.createElement("div", {
        onClick: this.handleEditParam,
        className: m
      }, _react.default.createElement("img", {
        src: require("./../../../assets/iot/img/list_edit.png"),
        alt: ""
      }), _react.default.createElement("label", null, "确认"))) : null), _react.default.createElement("div", {
        className: p
      }, _react.default.createElement("div", {
        className: "device-detail-basic-img"
      }, _react.default.createElement("img", {
        alt: "device",
        src: q,
        style: {
          "max-width": "300px",
          "max-height": "158px"
        }
      })), _react.default.createElement("div", {
        className: "device-detail-basic-info"
      }, _react.default.createElement("div", {
        style: {
          height: "30px"
        }
      }, _react.default.createElement("label", {
        className: "device-title-name"
      }, "名称:"), _react.default.createElement("label", {
        className: "device-title-value"
      }, s)), _react.default.createElement("div", {
        style: {
          height: "30px"
        }
      }, _react.default.createElement("label", {
        className: "device-title-name"
      }, "型号:"), _react.default.createElement("label", {
        className: "device-title-value"
      }, n)), _react.default.createElement("div", {
        style: {
          height: "30px"
        }
      }, _react.default.createElement("label", {
        className: "device-title-name"
      }, "类别:"), _react.default.createElement("label", {
        className: "device-title-value"
      }, (0, _DeviceLang.stringUtil)(_DeviceEnum.deviceCategoryEnum, l))), _react.default.createElement("div", {
        style: {
          height: "30px"
        }
      }, _react.default.createElement("label", {
        className: "device-title-name"
      }, "所属行业:"), _react.default.createElement("label", {
        className: "device-title-value"
      }, g)), _react.default.createElement("div", {
        className: "device-detail-descr"
      }, _react.default.createElement("label", {
        className: "device-title-descr-name"
      }, "说明:"), _react.default.createElement("label", {
        className: "device-title-descr-value"
      }, c)))), _react.default.createElement("div", {
        className: v
      }, _react.default.createElement("div", {
        className: "device-detail-edit-img"
      }, _react.default.createElement(_UpLoadImg.default, {
        setImgFile: this.setImgFile,
        defaultImg: q
      })), _react.default.createElement("div", {
        className: "device-detail-basic-info"
      }, _react.default.createElement(_amosAntd.Form, {
        horizontal: !0
      }, _react.default.createElement(FormItem, {
        label: "名称: ",
        labelCol: {
          span: 4
        },
        wrapperCol: {
          span: 6
        }
      }, _react.default.createElement(_amosAntd.Input, h)), _react.default.createElement(FormItem, {
        label: "型号: ",
        labelCol: {
          span: 4
        },
        wrapperCol: {
          span: 6
        }
      }, _react.default.createElement(_amosAntd.Input, y)), _react.default.createElement(FormItem, {
        label: "类别: ",
        style: {
          height: "42px"
        },
        labelCol: {
          span: 4
        },
        wrapperCol: {
          span: 6
        }
      }, _react.default.createElement(_amosFramework.Select, (0, _extends2.default)({
        style: {
          width: "100%",
          height: "32px"
        }
      }, I), deviceCategory)), _react.default.createElement(FormItem, {
        label: "所属行业: ",
        style: {
          height: "42px"
        },
        labelCol: {
          span: 4
        },
        wrapperCol: {
          span: 6
        }
      }, _react.default.createElement(_amosFramework.Select, (0, _extends2.default)({
        style: {
          width: "100%",
          height: "32px"
        }
      }, E, {
        placeholder: "请选择所属行业"
      }), _.map((function(e) {
        return _react.default.createElement(Option, {
          key: "".concat(e.value, "Option"),
          value: e.value
        }, e.label);
      })))), _react.default.createElement(FormItem, {
        label: "说明: ",
        labelCol: {
          span: 4
        },
        wrapperCol: {
          span: 6
        }
      }, _react.default.createElement(_amosAntd.Input, b)))))), _react.default.createElement("div", {
        className: "current-device-detail"
      }, _react.default.createElement(_DeviceProperty.default, {
        getDeviceInfoById: this.getDeviceInfoById,
        device: r
      }), _react.default.createElement(_DeviceInterface.default, {
        saveDeviceInterfaces: this.saveDeviceInterfaces,
        getDeviceInfoById: this.getDeviceInfoById,
        device: r
      }), _react.default.createElement(_DeviceCapacity.default, {
        deviceInterfaces: i,
        getDeviceInfoById: this.getDeviceInfoById,
        device: r
      })));
    }
  } ]), a;
}(_react.Component);

PrivateDeviceDetail.displayName = "PrivateDeviceDetail", PrivateDeviceDetail.propTypes = {
  form: _propTypes.default.any,
  callBack: _propTypes.default.func,
  carInfo: _propTypes.default.any,
  equiptemplateIndustryOptions: _propTypes.default.any
};

var _default = createForm(PrivateDeviceDetail);

exports.default = _default;