<!--
@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 !== 'view'" type="primary" @click="onSave">保存</p-button>
      <editForm ref="editForm" :editId="editId" :type="type" :options="options" :mextendedField_list="mextendedField_list"></editForm>
    </p-card>
  </p-spin>
</template>

<script>
import editForm from './editForm.vue';
import { View, Save, Update, Copy } from '../js/api';

export default {
  components: { editForm },
  props: {
    type: {
      type: String,
      default: '',
    },
    editId: {
      type: String,
      default: '',
    },
    options: {
      type: Object,
      default: () => ({}),
    },
    mextendedField_list: {
      type: Array,
      default: () => [],
    },
  },
  computed: {
    typeTitle() {
      const dictionary = {
        add: '新增',
        edit: '编辑',
        view: '查看',
        copy: '复制',
      };
      return dictionary[this.type] || this.type;
    },
  },
  data() {
    return {
      spinning: false,

      form: {},
    };
  },
  methods: {
    back() {
      this.$emit('back');
    },

    // 保存
    onSave() {
      this.$refs.editForm.$refs.ruleForm.validate((valid) => {
        if (!valid) return;
        if (this.type === 'add') this.onMethod(Save);
        else if (this.type === 'copy') this.onMethod(Copy);
        else if (this.type === 'edit') this.onMethod(Update);
        return;
      });
    },
    onMethod(method) {
      let params = { ...this.$refs.editForm.form, contractMode: 2 };
      params.isHeavyBubble = params.isHeavyBubble ? 1 : 0;

      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;
        });
    },
  },
};
</script>

<style lang="less" scoped>
.border {
  border: 1px solid #e8e8e8;
}
</style>