<script>
import Selector from "@/components/Selector/index.vue";

export default {
  name: "QuickSet.vue",
  props:{
    type:String,
    unitList:{
      type: Array,
      default(){
        return []
      }
    },
    currencyList:{
      type: Array,
      default(){
        return []
      }
    },
    value:{
      type: Object,
      default(){
        return {}
      }
    }
  },
  components: {Selector},
  data(){
    return {
      quickForm:{}, // 快速加价/减价表单
    }
  },
  computed:{
    getUnit(){
      return (field, unitField) => {
        // 非阶梯价
        if(this.value?.stepPrice != 1){
          return this.value[unitField]
        }
        // 后面是阶梯价的
        if(!this.value[field]?.length) return null
        return this.value[field][0][unitField]
      }
    }
  },
  methods:{
    handleConfirm(type){
      let amount = this.quickForm[type]
      if(!amount){
        return this.$message.error(this.$t('请输入加价金额'))
      }
      if(this.type == 'sea'){
        return this.batchSetSeaPrice(type, amount)
      }
      if(this.value?.priceType == 1){
        this.batchSetPrice("fullPriceStepList", 'all', amount)
      }else if(type == 'transport'){
        this.batchSetPrice("freightPriceStepList","transport", amount)
      }else if(type == 'clearance'){
        this.batchSetPrice("clearancePriceStepList", "clearance", amount)
      }
    },
    batchSetPrice(field, fieldPrefix, amount){
      if(!this.value[field]?.length) return
      this.value[field].forEach(item => {
        if(item[fieldPrefix + 'Price']){
          item[fieldPrefix + 'Price'] = (item[fieldPrefix + 'Price'] * 100 + amount * 100) / 100
        }
        // 特性价格
        if(item.specialList?.length){
          item.specialList.forEach(special => {
            console.log(special.specialDictType, special[fieldPrefix + 'Price'])
            if(special[fieldPrefix + 'Price']){
              special[fieldPrefix + 'Price'] = (special[fieldPrefix + 'Price'] * 100 + amount * 100) / 100
            }
          })
        }
        // 包装价格
        if(item.packagingList?.length){
          item.packagingList.forEach(packaging => {
            if(packaging['packagingPrice']){
              packaging['packagingPrice'] = (packaging['packagingPrice'] * 100 + amount * 100) / 100
            }
          })
        }
      })
    },
    batchSetSeaPrice(type, amount){
      console.log('快捷设置海运价格', type, amount)
      if(!this.value.stepPrice){
        if(!this.value.priceType){
          this.setSeaPrice(this.value,"transport", amount)
          this.setSeaPrice(this.value,"clearance",amount)
        }else{
          this.setSeaPrice(this.value,"all", amount)
        }
        return
      }
      this.value.priceStepList.forEach(item => {
        if(!this.value.priceType){
          this.setSeaPrice(item,"transport", amount)
          this.setSeaPrice(item,"clearance",amount)
        }else{
          this.setSeaPrice(item,"all", amount)
        }
      })
    },
    setSeaPrice(parent, fieldPrefix, amount){
      console.log(parent[`${fieldPrefix}Price`], ...arguments)
      parent[`${fieldPrefix}Price`] = (parent[fieldPrefix + 'Price'] * 100 + amount * 100) / 100
      if(!parent.specialList?.length) return
      parent.specialList.forEach(special => {
        console.log(special.specialDictType, special[fieldPrefix + 'Price'])
        if(special[fieldPrefix + 'Price']){
          special[fieldPrefix + 'Price'] = (special[fieldPrefix + 'Price'] * 100 + amount * 100) / 100
        }
      })
    }
  }
}
</script>

<template>
  <div>
    <h2 class="mt-5">{{$t('快捷设置')}}</h2>
    <div class="flex items-center" v-if="value.priceType != 1">
      <div class="w-100">{{$t('运费快捷加价')}}</div>
      <el-input-number :controls="false" v-model="quickForm.transport" class="w-100 ml-10 mr-10"></el-input-number>
      <selector disabled :value="getUnit('freightPriceStepList', 'transportPriceUnit')" :options="currencyList" :label-field="$l('title')" value-field="id" class="w-100" />
      / <selector disabled :value="getUnit('freightPriceStepList', 'transportVolumeUnit')" :options="unitList" :label-field="$l('title')" value-field="id" class="w-100" />
      <div class="ml-10">
        <el-button @click="handleConfirm('transport')" type="primary">{{$t('提交运费')}}</el-button>
      </div>
    </div>
    <div class="flex items-center mt-10" v-if="value.priceType != 1">
      <div class="w-100">{{$t('清关费快捷加价')}}</div>
      <el-input-number :controls="false" v-model="quickForm.clearance" class="w-100 ml-10 mr-10"></el-input-number>
      <selector disabled :value="getUnit('clearancePriceStepList', 'clearancePriceUnit')" :options="currencyList" :label-field="$l('title')" value-field="id" class="w-100" />
      / <selector disabled :value="getUnit('clearancePriceStepList', 'clearanceVolumeUnit')" :options="unitList" :label-field="$l('title')" value-field="id" class="w-100" />
      <div class="ml-10">
        <el-button @click="handleConfirm('clearance')" type="primary">{{$t('提交清关费')}}</el-button>
      </div>
    </div>
    <div class="flex items-center" v-if="value.priceType == 1">
      <div class="w-100">{{$t('全包价快捷加价')}}</div>
      <el-input-number v-model="quickForm.all" :controls="false" class="w-100 ml-10 mr-10"></el-input-number>
      <selector disabled :value="getUnit('fullPriceStepList', 'allPriceUnit')" :options="currencyList" :label-field="$l('title')" value-field="id" class="w-100" />
      / <selector disabled :value="getUnit('fullPriceStepList', 'allVolumeUnit')" :options="unitList" :label-field="$l('title')" value-field="id" class="w-100" />
      <div class="ml-10">
        <el-button @click="handleConfirm('all')" type="primary">{{$t('提交全包价')}}</el-button>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">

</style>