// packageF/storeManagement/components/baseInfo_popup/dispatch_type_pop/dispatch_type_pop.js
var app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    defaultData:{
      type:Object,
      value:[]
    },
    templates:{
      type:Array,
      value:[]
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    isShowPopup: false,
    showTemplate: false,
    dispatchData: {},

    radio: 0,
    dispatchPrice: '',
    columns: [],
    checkTemplate: {},

    templateName:"",
    defautlIndex:0
  },
  lifetimes:{
    attached(){
      let dispatchData = this.properties.defaultData;
      let defautlIndex=0;
      let templateName="默认模版";
      if(dispatchData.dispatch_id){
        if(this.data.templates){
          this.data.templates.forEach((item,key)=>{
            if(dispatchData.dispatch_id==item.id){
              defautlIndex = key;
              templateName = item.dispatch_name;
            }
          });
        }
      }
      this.setData({dispatchData,defautlIndex,templateName});
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    showPopup(){
      this.setData({
        isShowPopup:!this.data.isShowPopup
      });
    },
    changeType(evt){
      this.setData({
        'dispatchData.dispatch_type':evt.detail
      });
    },
    checkDisTemplate () {
      this.setData({
        isShowPopup:false,
        showTemplate:true
      });
    },
    // 运费模板
    confirmTemplate (evt) {
      let check = evt.detail.value;
      this.setData({
        templateName:check.dispatch_name,
        checkTemplate:check,
        'dispatchData.dispatch_id':check.id
      });
      this.cancelTemplate();
    },
    cancelTemplate () {
      this.setData({
        isShowPopup:true,
        showTemplate:false
      });
    },
    onClick(event) {
      const { name } = event.currentTarget.dataset;
      this.setData({
        'dispatchData.dispatch_type': name,
      });
    },
    changeText(evt){
      let value = evt.detail.value;
      this.data.dispatchData.dispatch_price = value;
    },
    confirmType (event) {
      if (this.data.dispatchData.dispatch_type == 1 && this.data.dispatchData.dispatch_price=='') {
        return app.tips('请填写统一邮费');
      }
      this.showPopup();
      this.triggerEvent("confirmType", this.data.dispatchData);
    },
  }
});
