"use strict";
Object.defineProperty(exports, "__esModule", {
  value: !0
}), exports.default = void 0;
var _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 _interopRequireDefault(e) {
  return e && e.__esModule ? e : {
    default: e
  }
}

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

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

function _typeof(e) {
  return (_typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(e) {
    return typeof e
  } : function(e) {
    return e && "function" == typeof Symbol && e.constructor === Symbol && e !== Symbol.prototype ? "symbol" : typeof e
  })(e)
}

function _extends() {
  return (_extends = Object.assign || function(e) {
    for (var t = 1; t < arguments.length; t++) {
      var a, r = arguments[t];
      for (a in r) Object.prototype.hasOwnProperty.call(r, a) && (e[a] = r[a])
    }
    return e
  }).apply(this, arguments)
}

function _classCallCheck(e, t) {
  if (!(e instanceof t)) throw new TypeError("Cannot call a class as a function")
}

function _defineProperties(e, t) {
  for (var a = 0; a < t.length; a++) {
    var r = t[a];
    r.enumerable = r.enumerable || !1, r.configurable = !0, "value" in r && (r.writable = !0), Object.defineProperty(e, r.key, r)
  }
}

function _createClass(e, t, a) {
  return t && _defineProperties(e.prototype, t), a && _defineProperties(e, a), e
}

function _inherits(e, t) {
  if ("function" != typeof t && null !== t) throw new TypeError("Super expression must either be null or a function");
  e.prototype = Object.create(t && t.prototype, {
    constructor: {
      value: e,
      writable: !0,
      configurable: !0
    }
  }), t && _setPrototypeOf(e, t)
}

function _setPrototypeOf(e, t) {
  return (_setPrototypeOf = Object.setPrototypeOf || function(e, t) {
    return e.__proto__ = t, e
  })(e, t)
}

function _createSuper(a) {
  var r = _isNativeReflectConstruct();
  return function() {
    var e, t = _getPrototypeOf(a);
    return _possibleConstructorReturn(this, r ? (e = _getPrototypeOf(this).constructor, Reflect.construct(t, arguments, e)) : t.apply(this, arguments))
  }
}

function _possibleConstructorReturn(e, t) {
  return !t || "object" !== _typeof(t) && "function" != typeof t ? _assertThisInitialized(e) : t
}

function _assertThisInitialized(e) {
  if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
  return e
}

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

function _getPrototypeOf(e) {
  return (_getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function(e) {
    return e.__proto__ || Object.getPrototypeOf(e)
  })(e)
}

function _defineProperty(e, t, a) {
  return t in e ? Object.defineProperty(e, t, {
    value: a,
    enumerable: !0,
    configurable: !0,
    writable: !0
  }) : e[t] = a, e
}
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() {
    _inherits(a, _react.Component);
    var t = _createSuper(a);

    function a(e) {
      var o;
      return _classCallCheck(this, a), _defineProperty(_assertThisInitialized(o = t.call(this, e)), "getDeviceInfoById", function(e) {
        e = "".concat(_urlConsts.OltSerUrl.fetchDeviceById, "/").concat(e);
        _processor.AmosFetch.get(e).then(function(e) {
          if (e.ok) return e.json();
          throw new Error("从服务端获取数据失败！")
        }).then(function(e) {
          "SUCCESS" === e.result && (e = e.dataList, o.setState({
            device: e,
            displayImgUrl: e.img
          }))
        })
      }), _defineProperty(_assertThisInitialized(o), "getBase64", function(e, t) {
        var a = new FileReader;
        a.addEventListener("load", function() {
          return t(a.result)
        }), a.readAsDataURL(e)
      }), _defineProperty(_assertThisInitialized(o), "setImgFile", function(e) {
        o.setState({
          imgFile: e
        })
      }), _defineProperty(_assertThisInitialized(o), "upLoadImgToSave", function(e) {
        e = {
          binaryImg: e,
          imgName: o.state.imgFile.name
        };
        o.saveDeviceParam(e)
      }), _defineProperty(_assertThisInitialized(o), "handleEditParam", function(e) {
        !1 !== o.state.textEdit ? o.props.form.validateFields(function(e, t) {
          var a, r;
          e || (void 0 === (r = o.state.deviceName) || "" !== r ? ((a = new FileReader).addEventListener("load", function() {
            return o.upLoadImgToSave(a.result)
          }), e = o.state.device.img, r = o.state.imgFile, "{}" === JSON.stringify(r) || e === r.name ? o.saveDeviceParam() : a.readAsBinaryString(o.state.imgFile), o.setState({
            textEdit: !o.state.textEdit
          })) : _amosFramework.message.success("设备名称不能为空！"))
        }) : o.setState({
          textEdit: !o.state.textEdit
        })
      }), _defineProperty(_assertThisInitialized(o), "saveDeviceParam", function(e) {
        var t = o.props.carInfo.id,
          a = o.props.form.getFieldValue("model"),
          r = o.props.form.getFieldValue("type"),
          i = o.props.form.getFieldValue("descr"),
          l = o.props.form.getFieldValue("viewPath"),
          n = o.props.form.getFieldValue("deviceName"),
          c = o.props.form.getFieldValue("industryId"),
          n = {
            id: t,
            model: a,
            type: r,
            viewPath: l,
            desc: i,
            name: n
          };
        Number.isInteger(c) && _extends(n, {
          industryId: c
        });
        c = "".concat(_urlConsts.OltSerUrl.deviceBasicInfoUpdate);
        void 0 !== e && _extends(n, e), _processor.AmosFetch.post(c, {
          data: n
        }).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("编辑成功！"), o.setState({
            imgFile: {}
          })) : _amosFramework.message.danger("编辑失败！"), o.getDeviceInfoById(t)
        })
      }), _defineProperty(_assertThisInitialized(o), "handleAddProperty", function(e) {}), _defineProperty(_assertThisInitialized(o), "handleChange", function(e) {
        "uploading" !== e.file.status ? "done" === e.file.status && o.getBase64(e.file.originFileObj, function(e) {
          return o.setState({
            imageUrl: e,
            loading: !1
          })
        }) : o.setState({
          loading: !0
        })
      }), _defineProperty(_assertThisInitialized(o), "beforeUpload", function(e) {
        var t = "image/jpeg" === e.type;
        t || _amosFramework.message.error("You can only upload JPG file!");
        e = e.size / 1024 / 1024 < 2;
        return e || _amosFramework.message.error("Image must smaller than 2MB!"), t && e
      }), _defineProperty(_assertThisInitialized(o), "saveDeviceInterfaces", function(e) {
        o.setState({
          deviceInterfaces: e
        })
      }), o.state = {
        textEdit: !1,
        loading: !1,
        interfaceList: [],
        device: {},
        displayImgUrl: "",
        imageUrl: "",
        imgFile: {},
        imgInfo: {},
        deviceInterfaces: []
      }, o
    }
    return _createClass(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,
          o = r.name,
          s = r.viewPath,
          d = (r.img, r.visibility),
          u = 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",
          _ = !1 === a ? "device-detail-title-content-hidden" : "device-detail-title-content",
          v = this.props.equiptemplateIndustryOptions || [],
          y = "";
        v.forEach(function(e, t, a) {
          e.value === u && (y = e.label || "")
        });
        var g = e("deviceName", {
            initialValue: o || "",
            rules: [{
              required: !0,
              message: "名称不能为空！"
            }, {
              validator: (0, _validate.noSpaceLengthValidate)(null, !0, "名称不能为空！")
            }]
          }),
          h = e("model", {
            initialValue: n || "",
            rules: [{
              required: !0,
              message: "型号不能为空！"
            }, {
              validator: (0, _validate.noSpaceLengthValidate)(null, !0, "型号不能为空！")
            }]
          }),
          t = e("type", {
            initialValue: "".concat(l),
            rules: [{
              required: !0,
              message: "类别不能为空！"
            }]
          }),
          a = e("industryId", {
            initialValue: "".concat(y),
            rules: [{
              required: !0,
              message: "所属行业不能为空！"
            }]
          }),
          s = (e("viewPath", {
            initialValue: s || "",
            rules: []
          }), e("descr", {
            initialValue: c || "",
            rules: [{
              validator: (0, _validate.noSpaceLengthValidate)(maxDescLength, !1, null)
            }]
          })),
          e = this.state.displayImgUrl,
          e = "" === e ? "".concat(_urlConsts.OltSerUrl.fetchDeviceImg, "/default.png") : "".concat(_urlConsts.OltSerUrl.fetchDeviceImg, "/").concat(e);
        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 === d ? _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: e,
          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"
        }, o)), _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"
        }, y)), _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: _
        }, _react.default.createElement("div", {
          className: "device-detail-edit-img"
        }, _react.default.createElement(_UpLoadImg.default, {
          setImgFile: this.setImgFile,
          defaultImg: e
        })), _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, g)), _react.default.createElement(FormItem, {
          label: "型号: ",
          labelCol: {
            span: 4
          },
          wrapperCol: {
            span: 6
          }
        }, _react.default.createElement(_amosAntd.Input, h)), _react.default.createElement(FormItem, {
          label: "类别: ",
          style: {
            height: "42px"
          },
          labelCol: {
            span: 4
          },
          wrapperCol: {
            span: 6
          }
        }, _react.default.createElement(_amosFramework.Select, _extends({
          style: {
            width: "100%",
            height: "32px"
          }
        }, t), deviceCategory)), _react.default.createElement(FormItem, {
          label: "所属行业: ",
          style: {
            height: "42px"
          },
          labelCol: {
            span: 4
          },
          wrapperCol: {
            span: 6
          }
        }, _react.default.createElement(_amosFramework.Select, _extends({
          style: {
            width: "100%",
            height: "32px"
          }
        }, a, {
          placeholder: "请选择所属行业"
        }), v.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, s)))))), _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
  }();
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, module.exports = exports.default;
