<template>
  <div>
    <el-dialog center width="80%" :before-close="()=>{$emit('update:showSpecialNeedsCons',false)}"
               :visible.sync="showSpecialNeedsCons">
      <div class="content">
        <h1>{{ consNum }} 特需</h1>
        <el-form label-width="180px" label-position="left">
          <el-form-item :label="$t('特殊要求')">
            <el-checkbox-group v-model="advanceType">
              <el-checkbox v-for="(item) in getDictDatas(DICT_TYPE.ORDER_SPECIAL_NEEDS)" :label="item.value">
                {{ item.label }}
              </el-checkbox>
            </el-checkbox-group>
          </el-form-item>
          <el-form-item :label="$t('特殊要求备注')">
            <el-input type="textarea" style="width: 300px;" v-model="remarks"></el-input>
          </el-form-item>
          <el-form-item v-for="(item,index) in  this.getDictDatas(this.DICT_TYPE.ORDER_SPECIAL_NEEDS)"
                        v-if="advanceType.indexOf(item.value) > -1" :key="item.value" :label="item.label + '预计金额'">
            <el-input-number style="width: 300px;" v-model.number="specialNeedsList[index].expectedFee" type="number"
                             :min="0" step-strictly></el-input-number>
            <el-select v-model="specialNeedsList[index].expectedFeeCurrency">
              <el-option v-for="item in JSON.parse(currency)" :key="item.id" :label="item.titleZh" :value="item.id" />
            </el-select>
            <div style="display: inline-block;margin-left: 20px;">
              <span style="margin-right: 10px;">{{ $t("付款类型") }}</span>
              <dict-selector :placeholder="$t('请选择付款类型')" v-model="specialNeedsList[index].payType"
                             :type="DICT_TYPE.PAYMENT_TYPE"></dict-selector>
            </div>
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submit">{{ $t("提交") }}</el-button>
        <el-button @click="$emit('update:showSpecialNeedsCons',false)">{{ $t("取消") }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getDictDatas, DICT_TYPE } from "@/utils/dict"
import { createConsSpecialNeed, getSpecialListByConsId } from "@/api/ecw/consSpecialNeed"

export default {
  name: "specialNeedsCons",
  props: {
    consId: {
      type: [String, Number]
    },
    consNum: [String, Number],
    showSpecialNeedsCons: Boolean,
    currency: String
  },
  data() {
    return {
      getDictDatas,
      DICT_TYPE,
      form: {},
      advanceType: [],
      specialNeedsList: [],
      remarks: "",
      echoList: []
    }
  },
  methods: {
    submit() {
      this.specialNeedsList.forEach(i => {
        i.remarks = this.remarks
      })
      let p = this.specialNeedsList.filter(i => this.advanceType.indexOf(i.advanceType) > -1)
      let p1 = []
      console.log(this.echoList)
      console.log(this.specialNeedsList)

      //获取新增的和修改的
      p.forEach(item => {
        if (this.echoList.find(i => item.id === i.id)) {
          p1.push(item)
        } else {
          item.needStatus = 0
          item.oper = "add"
          p1.push(item)
        }
      })
      //获取删除的
      this.echoList.forEach(item => {
        if (!(p.find(i => i.id === item.id))) {
          item.oper = "del"
          p1.push(item)
        }
      })
      let p2 = p1.filter(i => Number(i.expectedFee) >= 0)
      if (p2.every(i => !!i.payType)) {
        console.log(p1)
        createConsSpecialNeed(p1).then(r => {
          this.$emit("update:showSpecialNeedsCons", false)
          this.$emit("determine")
          this.$message.success("提交成功")
        })
      } else {
        let v = p2.map(i => i.advanceType)
        console.log(this.getDictDatas(this.DICT_TYPE.ORDER_SPECIAL_NEEDS), "this.getDictDatas(this.DICT_TYPE.ORDER_SPECIAL_NEEDS)")
        let v2 = this.getDictDatas(this.DICT_TYPE.ORDER_SPECIAL_NEEDS).filter(i => v.includes(i.value))
        this.$modal.notifyWarning(`请选择"${v2.map(i => i.label).join("，")}"的付款类型。`)
      }
    }
  },
  watch: {
    showSpecialNeedsCons(val) {
      if (val) {
        this.getDictDatas(this.DICT_TYPE.ORDER_SPECIAL_NEEDS).forEach(r => {
          this.specialNeedsList.push({
            advanceType: r.value,
            consId: this.consId,
            expectedFee: "",//预计金额
            expectedFeeCurrency: 1,//运输费币种
            payType: ""
          })
        })
        //特需回显
        getSpecialListByConsId(this.consId).then(r => {
          this.echoList = r.data
          if (r.data.length) {
            this.remarks = r.data[0].remarks
          }
          r.data.forEach(i => {
            this.advanceType.push(i.advanceType+'')
            let index = this.specialNeedsList.findIndex(item => i.advanceType == item.advanceType)
            this.specialNeedsList.splice(index, 1, {
              advanceType: i.advanceType+'',
              consId: this.consId,
              id: i.id,
              expectedFee: i.expectedFee,//预计金额
              expectedFeeCurrency: i.expectedFeeCurrency,//运输费币种
              payType: i.payType
            })
          })
        })
      } else {
        this.specialNeedsList = []
        this.advanceType = []
      }
    }
  }
}
</script>

<style scoped lang="scss">

</style>
