// packageF/storeManagement/components/base_set/base_set.js
var app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    goodsInfo: {
      type: Object,
      value: {}
    },
    categoryMenus: {
      type: Array,
      value: []
    },
    dispatchTemplates: {
      type: Array,
      value: []
    },
    dispatchTypes: {
      type: Array,
      value: []
    },
    goodsId:{
      type:Number,
      value:0
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    goodsData: {}, // 商品数据

    goodsImg: '',
    otherImgs: [],
    goodsDetail: '', // 商品详情
    goodsName: '', // 商品名称
    goodsCategory: [], // 商品分类
    goodsType: '1', // 商品类型 1实体商品 2虚拟商品
    needAddress: 0, // 是否需要填写收货地址 1是 0否
    goodsUnit: '', // 商品单位
    price: '', // 商品现价
    marketPrice: '', // 商品原价
    costPrice: '', // 商品成本
    stock: '', // 库存
    stockType: 0, // 减库存方式 0拍下减库存 1付款减库存 2不减库存
    refund: 0, // 能否退款 0否 1是

    viewMore: false,
    dispatchData: { // 运费设置 1统一邮费(params:邮费) 0邮费模板(type:模板参数)
      dispatch_type: 1,
      dispatch_price: '',
      dispatch_id: '',
    },
    distributes: [],
  },
  lifetimes: {
    attached() {
      this.data.goodsData = JSON.parse(JSON.stringify(this.data.goodsInfo));
      this.splitGoodsData(this.data.goodsData);
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    validatorData() {
      if (app._isTextEmpty(this.data.goodsImg)) {
        return app.tips('请选择商品图片');
      }
      if (app._isTextEmpty(this.data.goodsName)) {
        return app.tips('请输入商品名称');
      }
      if (this.data.goodsCategory.length <= 0) {
        return app.tips('请选择商品分类');
      }
      if (app._isTextEmpty(this.data.goodsUnit)) {
        return app.tips('请输入商品单位');
      }
      if (app._isTextEmpty(this.data.price)) {
        return app.tips('请输入商品现价');
      }
      if (app._isTextEmpty(this.data.stock)) {
        return app.tips('请输入商品库存');
      }
      let data = {
        goods_type: this.data.goodsType,
        status: 1, // 上架
        title: this.data.goodsName,
        thumb: this.data.goodsImg,
        thumb_url: this.data.otherImgs,
        sku: this.data.goodsUnit,
        content: this.data.goodsDetail,
        market_price: this.data.marketPrice,
        price: this.data.price,
        cost_price: this.data.costPrice,
        stock: this.data.stock,
        reduce_stock_method: this.data.stockType,
        no_refund: this.data.refund,
        need_address: this.data.needAddress,
        category: this.filterCategory(this.data.goodsCategory)
      };
      data.widgets = {
        dispatch: {
          ...this.data.dispatchData,
          dispatch_type_ids: this.data.distributes
        }
      };
      return data;
    },
    filterCategory (arr) {
      let parentid = [];
      let childid = [];
      let thirdid = [];
      arr.forEach((item, index) => {
        if (item.parentid) parentid.push(item.parentid);
        if (item.childid) childid.push(item.childid);
        if (item.thirdid) thirdid.push(item.thirdid);
      });
      return {parentid, childid, thirdid};
    },
    splitGoodsData(goodsData){
      let changeData={};
      changeData.goodsImg = goodsData.thumb_src;
      changeData.otherImgs = goodsData.thumb_url_src ? goodsData.thumb_url_src : [];
      changeData.goodsDetail = goodsData.content;
      changeData.goodsName = goodsData.title;
      changeData.goodsType = goodsData.type ? goodsData.type : 1;
      changeData.needAddress = goodsData.need_address ? goodsData.need_address : 0;
      changeData.goodsUnit = goodsData.sku;
      changeData.price = goodsData.price;
      changeData.marketPrice = goodsData.market_price;
      changeData.costPrice = goodsData.cost_price;
      changeData.stock = goodsData.stock;
      changeData.stockType = goodsData.reduce_stock_method ? goodsData.reduce_stock_method : 0;
      changeData.refund = goodsData.no_refund ? goodsData.no_refund : 0;
      changeData.goodsCategory = goodsData.has_many_goods_category ? goodsData.has_many_goods_category : [];
      // 配送设置
      if ( goodsData.widgets && goodsData.widgets.dispatch ) {
        let dispatch = goodsData.widgets.dispatch;
        changeData.dispatchData={};
        changeData.dispatchData.dispatch_type = dispatch.dispatch_type;
        changeData.dispatchData.dispatch_price = dispatch.dispatch_price;
        changeData.dispatchData.dispatch_id = dispatch.dispatch_id;
        changeData.distributes = dispatch.dispatch_type_ids;
      }
      this.setData(changeData);
    },
    viewMoreSet() {
      this.setData({
        viewMore: !this.data.viewMore
      });
    },
    updateGoodsImg(evt) {

      this.data.goodsImg = evt.detail[0] ? evt.detail[0] : '';
    },
    updateOtherImgs(evt) {
      this.data.otherImgs = evt.detail;
    },
    showGoodsDetailPop() {
      this.selectComponent('.goods-details-pop').showPopup();
    },
    saveGoodsDetail(event) {
      this.setData({
        goodsDetail: event.detail
      });
    },
    editCategory(evt) {
      let {
        manyGoodsCategory
      } = evt.detail;
      let goodsCategory = this.data.goodsCategory;
      let isRepeat = goodsCategory.some((item)=>{
        if(manyGoodsCategory.thirdid){
          return (item.parentid == manyGoodsCategory.parentid) && (item.childid == manyGoodsCategory.childid) && (item.thirdid == manyGoodsCategory.thirdid);
        }else {
          return (item.parentid == manyGoodsCategory.parentid) && (item.childid == manyGoodsCategory.childid);
        }
      });
      if(isRepeat) return app.tips("不能重复添加商品分类");
      goodsCategory.push(manyGoodsCategory);
      this.setData({
        goodsCategory
      });
    },
    // 删除分类
    removeCategory(evt) {
      let index = evt.currentTarget.dataset.index;
      let goodsCategory = this.data.goodsCategory;
      goodsCategory.splice(index, 1);
      this.setData({
        goodsCategory
      });
    },
    // 商品类型
    showGoodsTypePop() {
      this.selectComponent('.goods-type-pop').showPopup();
    },
    editGoodsType(evt) {
      this.setData({
        goodsType: evt.detail
      });
    },
    switchChange(evt) {
      let name = evt.currentTarget.dataset.name;
      this.setData({
        [name]: evt.detail
      });
    },
    // 减库存方式
    showStockTypePop() {
      this.selectComponent('.stock-type-pop').showPopup();
    },
    editStockType(evt) {
      this.setData({
        stockType: evt.detail
      });
    },
    // 运费
    showDispatchTypePop() {
      this.selectComponent('.dispatch-type-pop').showPopup();
    },
    editDispatch(evt) {
      this.setData({
        dispatchData: evt.detail
      });
    },
    showDistributeTypePop() {
      this.selectComponent('.distribute-type-pop').showPopup();
    },
    editDistribute(evt) {
      this.setData({
        distributes: evt.detail
      });
    }
  }
});