import entrust,{EntrustType} from '../../../utils/request/entrust/index';

import notice from '@/utils/notice/index';

import toast from '@/utils/tool/toast/index';

import typeData from '../data/type';

export default {

    data(){
      return {
          type:'',
          checkId:''
      }
    },

    computed:{
      typeItem(){
          return typeData[this.type] || typeData.default;
      }
    },

    methods:{
        fetch(obj){
            this.$request({
                url:'address/getAddress',
                page:obj,
                token:true,
                entrust: entrust.getEntrust(EntrustType.address)
            }).then((data)=>{
                return obj.success(data.data);
            }).catch(obj.fail);
        },

        routerAddAddress(){
            return this.$router.navigateTo('add-address');
        },

        routerUpdateAddress(item){
            return this.$router.navigateTo({
                name:'add-address',
                params:{
                    id:item.id,
                    type:'save'
                },
                memoryParams:{
                    item:item
                }
            });
        },
        checkedAddress(item){
            if(!this.typeItem.select) return ;
            if (item.id) {
                notice.trigger('select-address',{
                    item:item
                });
                return this.$router.navigateBack();
            }
        }
    },

    onLoad(){
        this.type = this.$params.type || 'default';
        this.checkId = this.$params.id;
    },

    onShow(){
        if (this.stroageToastInfo) {
            toast.success(this.stroageToastInfo);
            this.stroageToastInfo = null;
        }
    },

    created(){

        notice.on('address',(data)=>{

            if (data.toast) {
                this.stroageToastInfo = data.msg;
            }

            setTimeout(()=>{
               return this.flatReload(3,true);
            });

        },'address');
    },

    beforeDestroy(){
        notice.off('address','address');
    }
}