"use strict";

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

exports.__esModule = true;
exports.default = void 0;

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));

var _actionSheet = _interopRequireDefault(require("../action-sheet"));

var _toast = _interopRequireDefault(require("../toast"));

// import './index.css'
var ks = window.ks && window.ks.default;

var __vue_render__ = function __vue_render__() {
  var _vm = this;

  var _h = _vm.$createElement;

  var _c = _vm._self._c || _h;

  return _c('div', {
    staticClass: "van-uploader"
  }, [_c('div', {
    staticClass: "van-uploader__wrapper"
  }, [_vm._l(_vm.value, function (item, index) {
    return _c('div', {
      key: index,
      staticClass: "van-uploader__preview"
    }, [_c('div', {
      staticClass: "van-image van-uploader__preview-image",
      staticStyle: {
        "overflow": "hidden",
        "border-radius": "4px"
      }
    }, [_c('img', {
      staticClass: "van-image__img",
      staticStyle: {
        "object-fit": "cover"
      },
      attrs: {
        "src": item.url
      }
    })]), _vm._v(" "), _c('i', {
      staticClass: "van-icon van-icon-clear van-uploader__preview-delete",
      on: {
        "click": function click($event) {
          return _vm.onDelete(index);
        }
      }
    })]);
  }), _vm._v(" "), _vm.maxLength && _vm.value.length < _vm.maxLength || !_vm.maxLength ? _c('div', {
    staticClass: "van-uploader__upload",
    on: {
      "click": _vm.onPreview
    }
  }, [_c('img', {
    staticClass: "van-uploader__icon",
    attrs: {
      "src": "https://erphostjs.kye-erp.com/kuasheng-h5/assets/ks-upload/upload.png"
    }
  })]) : _vm._e()], 2), _vm._v(" "), _c('Action-sheet', {
    attrs: {
      "actions": _vm.actions,
      "get-container": "body",
      "cancel-text": "取消"
    },
    on: {
      "select": _vm.onSelect,
      "cancel": _vm.onCancel
    },
    model: {
      value: _vm.show,
      callback: function callback($v) {
        _vm.show = $v;
      },
      expression: "show"
    }
  })], 1);
};

var __vue_staticRenderFns__ = [];
var _default2 = {
  render: __vue_render__,
  staticRenderFns: __vue_staticRenderFns__,
  name: "ks-upload",
  components: {
    ActionSheet: _actionSheet.default
  },
  props: {
    value: {
      type: Array,
      default: function _default() {
        return [];
      }
    },
    bizId: String,
    bizCode: String,
    maxLength: {
      type: Number,
      default: 6
    },
    allowRecordVideo: {
      type: Boolean,
      default: false
    },
    selectType: {
      type: Number,
      default: 1 // 0:选择图片和视频 1:只选择图片 2:只选择视频

    },
    quality: {
      type: Number,
      default: 80
    }
  },
  data: function data() {
    return {
      show: false,
      actions: [{
        name: "从相册选择"
      }, {
        name: "拍照"
      }]
    };
  },
  methods: {
    onPreview: function onPreview() {
      this.show = true;
    },
    onCancel: function onCancel() {
      this.show = false;
    },
    onSelect: function onSelect(item) {
      var _this = this;

      return (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee() {
        var fileList, params;
        return _regenerator.default.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                fileList = null;
                _context.prev = 1;
                params = {
                  totalCount: _this.maxLength,
                  allowRecordVideo: _this.allowRecordVideo,
                  selectType: _this.selectType,
                  quality: _this.quality
                };
                console.log('从相册选择参数', params);

                if (!(item.name === '从相册选择')) {
                  _context.next = 10;
                  break;
                }

                _context.next = 7;
                return ks.pickAssets(params);

              case 7:
                fileList = _context.sent;
                _context.next = 14;
                break;

              case 10:
                if (!(item.name === '拍照')) {
                  _context.next = 14;
                  break;
                }

                _context.next = 13;
                return ks.takeByCamera();

              case 13:
                fileList = _context.sent;

              case 14:
                console.log('从相册选择图片', fileList);

                if (fileList && fileList.length) {
                  _this.upload(fileList);
                }

                _context.next = 21;
                break;

              case 18:
                _context.prev = 18;
                _context.t0 = _context["catch"](1);
                (0, _toast.default)(_context.t0);

              case 21:
              case "end":
                return _context.stop();
            }
          }
        }, _callee, null, [[1, 18]]);
      }))();
    },
    onDelete: function onDelete(index) {
      var _this2 = this;

      var list = JSON.parse(JSON.stringify(this.value));
      var data = list[index];
      list.splice(index, 1);
      this.$emit('input', list);
      this.$http('file.deleteByBizAndId', 10124, data).then(function (res) {
        _this2.$emit('deleteSuccess');
      }).catch(function (err) {
        console.log(err);
      });
    },
    upload: function upload(list) {
      var _this3 = this;

      return (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2() {
        var res;
        return _regenerator.default.wrap(function _callee2$(_context2) {
          while (1) {
            switch (_context2.prev = _context2.next) {
              case 0:
                _context2.next = 2;
                return ks;

              case 2:
                _context2.t0 = _context2.sent;

                if (!_context2.t0) {
                  _context2.next = 5;
                  break;
                }

                _context2.t0 = ks.uploadAssets({
                  filePaths: list.map(function (item) {
                    return item.filePath;
                  }),
                  fileNames: list.map(function (item) {
                    return item.fileName;
                  }),
                  bizCode: _this3.bizCode,
                  bizId: _this3.bizId
                });

              case 5:
                res = _context2.t0;

                _this3.$emit('input', res.reverse());

                _this3.$emit('uploadSuccess');

                _this3.onCancel();

              case 9:
              case "end":
                return _context2.stop();
            }
          }
        }, _callee2);
      }))();
    }
  }
};
exports.default = _default2;