<!--
@name: 价格-编辑
@author: 张泽贤
@time: 2022-08-29
@doc:
-->
<template>
  <p-spin class="spin-full" :spinning="spinning">
    <p-card class="full-card-panel">
      <a-space slot="title" style="display: flex; align-items: center; cursor: pointer">
        <a-icon type="arrow-left" style="color: #1990ff" @click="back" />
        {{ typeTitle }}
      </a-space>
      <p-button slot="extra" v-show="!type.includes('View')" type="primary" @click="onSave">保存</p-button>

      <p-tabs :animated="false" :activeKey="activeKey" @tabClick="activeKey = $event">
        <p-tab-pane key="1" tab="合同信息" :forceRender="true">
          <editForm :editId="editId" type="view" :options="options" :mextendedField_list="mextendedField_list" @setContractInfo="getContractInfo"></editForm>
        </p-tab-pane>
        <p-tab-pane key="2" :tab="tabtitle" :forceRender="true">
          <priceEditForm ref="priceEditForm" :type="type" :priceId="priceId" :options="options" :contractInfo="contractInfo"></priceEditForm>
        </p-tab-pane>
      </p-tabs>
    </p-card>
  </p-spin>
</template>

<script>
import departmenTree from '@/blocks/components/formConfig/index.departmenTree.vue';
import CascaderZone from '@/blocks/components/cascaderZone';

import { priceSave, priceUpdate } from '../js/api.js';
import editForm from './editForm.vue';
import priceEditForm from './priceEditForm.vue';

export default {
  components: { editForm, priceEditForm, PCascaderZone: CascaderZone, departmenTree },
  props: {
    type: {
      type: String,
      default: '',
    },
    editId: {
      type: String,
      default: '',
    },
    priceId: {
      type: String,
      default: '',
    },
    options: {
      type: Object,
      default: () => ({}),
    },
    mextendedField_list: {
      type: Array,
      default: () => [],
    },
  },
  computed: {
    typeTitle() {
      const dictionary = {
        freightAdd: '运费价格-新增',
        freightEdit: '运费价格-编辑',
        freightView: '运费价格-查看',
        freightCopy: '运费价格-复制',
        surchargeAdd: '附加费价格-新增',
        surchargeEdit: '附加费价格-编辑',
        surchargeView: '附加费价格-查看',
        surchargeCopy: '附加费价格-复制',
      };
      return dictionary[this.type] || this.type;
    },
    tabtitle() {
      let result = '合同';
      if (this.type.includes('freight')) {
        result += '运费价格';
      } else {
        result += '附加费价格';
      }
      return result;
    },
  },
  data() {
    return {
      spinning: false,
      activeKey: '2',

      // 合同信息
      contractInfo: {},
    };
  },
  methods: {
    back() {
      this.$emit('back');
    },

    onSave() {
      this.$refs.priceEditForm.$refs.ruleForm.validate((valid) => {
        if (!valid) return;
        if (this.type.includes('Edit')) this.onMethod(priceUpdate);
        else this.onMethod(priceSave);
      });
    },
    onMethod(method) {
      let { form, conditionList, ladderList } = this.$refs.priceEditForm;
      let list = ladderList.map((item) => {
        let reslut = { ...item };
        if (reslut.type === 'add') {
          delete reslut.type;
          delete reslut.id;
        }
        return reslut;
      });
      for (let index = 0; index < conditionList.length; index++) {
        const element = conditionList[index];
        if (!element.conditionType) {
          this.$message.error("请选择匹配条件");
          return false;
        }
      }
      let params = {
        ...form,
        conditionList: [...conditionList],
        ladderList: list,
      };
      params.contractNo = this.contractInfo.contractNo;
      params.isSurcharge = this.type.includes('freight') ? 0 : 1;
      params.shipperAddress = (params.shipperProvinceName || '') + (params.shipperCityName || '') + (params.shipperDistrictName || '');
      params.consigneeAddress = (params.consigneeProvinceName || '') + (params.consigneeCityName || '') + (params.consigneeDistrictName || '');

      this.spinning = true;
      method(params)
        .then((res) => {
          if (res.success) {
            this.$message.success(res.message);
            this.$emit('back');
          } else {
            this.$exception(res);
          }
        })
        .finally(() => {
          this.spinning = false;
        });
    },

    // 获取合同信息
    getContractInfo(contractInfo) {
      this.contractInfo = contractInfo;
    },
  },
};
</script>