<template>
  <el-dialog visible :close-on-click-modal="false" :before-close="closeDialog" :title="$t('优惠申请')">
    <el-form ref="form" :model="form" label-width="80px" :rules="rules">
      <el-form-item :label="$t('商品类型')">
        <span>{{ getProductAttrNameById(form.productType) }}</span>
      </el-form-item>
      <el-form-item :label="$t('商品名称')">
        <span>{{ form.prodTitleZh }}</span>
      </el-form-item>
      <el-form-item :label="$t('英文名称')">
        <span>{{ form.prodTitleEn }}</span>
      </el-form-item>
      <el-form-item :label="$t('线路')">
        {{ `【${getDictDataLabel(DICT_TYPE.TRANSPORT_TYPE, form.transportId)}】${ getChannelNameById(form.channelId) }从【${form.departureName}】发往【${form.objectiveName}】`}}
      </el-form-item>
      <el-form-item :label="$t('是否预付')">
        <dict-tag :type="DICT_TYPE.ECW_YESNO" :value="form.isPayAdvance" />
      </el-form-item>
      
      <el-row v-if="!form.charging">
        <el-col :span="12">
          <el-form-item :label="$t('旧运费')">
            {{form.orgSeaFreight}} {{ currentMap[form.orgSeaFreightCurrency] }} / {{ unitMap[form.orgSeaFreightVolume] }}
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('旧清关费')">
            {{form.orgClearanceFreight}} {{ currentMap[form.orgClearanceFreightCurrency] }} / {{ unitMap[form.orgClearanceFreightVolume] }}
          </el-form-item>
        </el-col>
      </el-row>
      <el-row v-if="!form.charging">
        <el-col :span="12">
          <el-form-item :label="$t('新运费')" style="width: 400px" prop="seaFreight">
            <el-input v-model="form.seaFreight" type="number" class="w-100" />
            {{ currentMap[form.orgSeaFreightCurrency] }} / {{ unitMap[form.orgSeaFreightVolume] }}
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('新清关费')" style="width: 400px" prop="clearanceFreight">
            <el-input v-model="form.clearanceFreight" type="number" class="w-100" />
            {{ currentMap[form.orgClearanceFreightCurrency] }} / {{ unitMap[form.orgClearanceFreightVolume] }}
          </el-form-item>
        </el-col>
      </el-row>
      
      <el-form-item :label="$t('旧全包价')" v-if="form.charging">
        {{form.orgSeaFreight}} {{ currentMap[form.orgSeaFreightCurrency] }} / {{ unitMap[form.orgSeaFreightVolume] }}
      </el-form-item>
      <el-form-item :label="$t('新全包价')" style="width: 400px" prop="clearanceFreight"  v-if="form.charging">
        <el-input v-model="form.seaFreight" type="number" class="w-100" />
        {{ currentMap[form.orgSeaFreightCurrency] }} / {{ unitMap[form.orgSeaFreightVolume] }}
      </el-form-item>
      
    </el-form>
    <div class="page-title">审批流程</div>
    <work-flow xmlkey="sheet_sale" v-model="ccIdArr" />
    <div v-if="!form.applyStatus">
      <el-button type="primary" @click="handleSubmit">提交</el-button>
    </div>
    <div v-else>
      <el-button type="primary" @click="$router.push('/bpm/process-instance/detail?id=' + form.formId)">
        <dict-tag :type="DICT_TYPE.APPLY_STATUS" :value="form.applyStatus" />
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import {createOfferSpecial, getOfferSpecial, getOfferSpecialByApproveId} from "@/api/ecw/offer"
import {DICT_TYPE, getDictDataLabel} from "@/utils/dict"
import DictSelector from "@/components/DictSelector"
import {getUnitList} from "@/api/ecw/unit"
import {getChannelList} from "@/api/ecw/channel"
import { getCurrencyList } from '@/api/ecw/currency'
import { getProductAttrList } from '@/api/ecw/productAttr'
import WorkFlow from '@/components/WorkFlow'
export default {
  name: "specialDiscount",
  props: {
    offerProdId: Number,
  },
  components: {
    DictSelector, WorkFlow
  },
  created() {
    // 查看详情，列表进来的
    if(this.offerProdId){
      this.getOfferSpecial()
    }

    getUnitList().then(res => this.unitList = res.data)
    getChannelList().then(res => this.channelList = res.data)
    getCurrencyList().then(res => this.currencyList = res.data)
    getProductAttrList().then(res => this.productAttrList = res.data)

  },
  data() {
    return {
      getDictDataLabel,
      DICT_TYPE,
      unitList:[],
      channelList:[],
      currencyList:[],
      productAttrList:[],
      ccIdArr: [],
      form: {
        type: 1
      },
      rules:{
        seaFreight      : {required: true, message: '运费不能为空'},
        clearanceFreight: {required: true, message: '清关费不能为空'}
      }
    }
  },
  watch:{
    ccIdArr(){
      this.$set(this.form, 'ccIds', this.ccIdArr.join(','))
    }
  },
  methods: {
    handleSubmit(){
      this.$refs.form.validate().then(res => {
        createOfferSpecial(Object.assign({}, this.form, {type: 1})).then(res => {
          this.$message.success(this.$t('提交成功'))
          this.$emit('success')
        })
      })
    },
    getOfferSpecial(){
      getOfferSpecial(this.offerProdId, {type: 1}).then(r => {
        this.form = r.data
      })
    },
    getProductAttrNameById(id){
      return this.productAttrList.find(e => e.id === id)?.attrName || ''
    },
    closeDialog(){
      this.$emit('close')
    }
  },
  computed: {
    // 根据渠道id显示渠道名
    getChannelNameById(){
      return channelId => {
        const s = this.channelList.find(item => item.channelId == channelId) ?. nameZh
        return s ? '【' + s + '】' : ''
      }
    },
    currentMap(){
      let map = {}
      this.currencyList.forEach(item => {
        map[item.id] = this.$l(item, 'title')
      })
      return map
    },
    unitMap(){
      let map = {}
      this.unitList.forEach(item => {
        map[item.id] = this.$l(item, 'title')
      })
      return map
    }
  }
}
</script>
