import priceUnit from '@/utils/tool/price';
export default {

    data(){
        return {
            ruleModal:false,
            buyType:1,
            buyNumber:1,
            rules:{},
            rulesPrice:'0',
            rulesOption:[]
        }
    },

    computed:{
        rulesPriceNumber(){
           let prices = this.rulesPrice.toString().split('-');

            prices = prices.map((item)=>{

              return priceUnit.toFixed(parseFloat(item || 0) * this.buyNumber);
            });

            return prices.join('-');
        },
        priceNumber(){
            return priceUnit.toFixed((this.item.sales_price || 0) * this.buyNumber);
        }
    },

    methods:{

        changeBuyNumber(item){
          if(this.buyNumber !== item.value){
              this.buyNumber = item.value;
          }
        },

        triggerChangeRule(item,index){

            let pItem = this.rulesOption[index];

            if (pItem && this.rules[pItem.key] !== item.value) {
                this.$set(this.rules,pItem.key,item.value);
                if (this.rulesWatch[pItem.key]) return this.triggerRuleFormat(pItem.key);
            }

        },

        // 构造 rulesWatch
        createdRulesWatch(rulesOption){

            rulesOption.map((item,index)=>{
                if (item.watch && item.watch.key) {
                    if (typeof item.watch.key === 'string') item.watch.key=[item.watch.key];

                    item.watch.key.map((item)=>{
                        if (this.rulesWatch[item] === undefined) this.rulesWatch[item] = [];
                        this.rulesWatch[item].push(index);
                        this.triggerRuleFormat(item);
                    });

                }
            });
        },

        // 触发配置
        triggerRuleFormat(key){
            if (this.rulesWatch[key]) {
                this.rulesWatch[key].map((item)=>{
                    item = this.rulesOption[item];
                    if (item) {
                        return this[item.watch.trigger || 'defaultRuleFormat'](item,this.rules);
                    }
                });
            }
        },

        // 设置 规格
        setRule(item){
            this.rulesOption = item;
            // 构造watch
            this.createdRulesWatch(item);
        },

        // 针对规格价格计算
        rulePriceFormat(item,rules){

            if (item.watch) {

                // 如果价格为真
                let value = rules[item.watch.key];

                let price = this.item.sales_price;

                if (value === undefined) {

                    let maxValue = 0;
                    let minValue = 999999;
                    item.data.map((item)=>{
                        maxValue = Math.max(maxValue,item.value);
                        minValue = Math.min(minValue,item.value);
                    });

                    this.rulesPrice = priceUnit.toFixed(price * minValue) + '-' + priceUnit.toFixed(price * maxValue);

                } else {
                    // 设置价格
                    this.rulesPrice = priceUnit.toFixed(this.item.sales_price * value);
                }

            }

        },

        // 默认规格计算
        defaultRuleFormat(item,rules){

            // 创建替换的字符串
            item.info = (item.watch.format || '').replace(/\${([^}]+)}/ig,(a,key)=>{
                return rules[key] || (item.watch.default && item.watch.default[key] !== undefined ?item.watch.default[key]: '');
            });

        },

        openRuleModal(){
            if (!this.ruleModal) {
                this.ruleModal = true;
            }
        },
        cancelRuleModal(){
            if (this.ruleModal) {
                this.ruleModal = false;
            }
        },

        triggerBuyType(type = 1){
            if (this.buyType !== type) {
                this.buyType = type;
            }
            return this.openRuleModal();
        }
    },

    created(){
        this.rulesWatch = {};
    }

}