<!--
@name: 结算管理-编辑页
@author: 张泽贤
@time: 2022-08-02
@doc: 
-->
<template>
  <p-spin :spinning="spinning">
    <p-card>
      <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>
      <div slot="extra" v-show="type !== 'view'">
        <p-button type="primary" @click="onSave">保存</p-button>
      </div>
    </p-card>
    <p-card>
      <p-form-model ref="ruleForm" class="plg-grid-from" :model="form" :label-col="{ span: 8 }" :wrapper-col="{ span: 16 }" :rules="rules">
        <a-divider orientation="left">基本信息</a-divider>
        <a-row>
          <a-col :span="6">
            <p-form-model-item label="结算编码" prop="accountWayCode">
              <div v-if="type === 'view'">{{ form.accountWayCode }}</div>
              <p-input v-model="form.accountWayCode" :disabled="type === 'edit'" v-else />
            </p-form-model-item>
          </a-col>
          <a-col :span="6">
            <p-form-model-item label="结算名称" prop="accountWayName">
              <div v-if="type === 'view'">{{ form.accountWayName }}</div>
              <p-input v-model="form.accountWayName" :disabled="type === 'edit'" v-else />
            </p-form-model-item>
          </a-col>
          <a-col :span="6">
            <p-form-model-item label="结算类型" prop="accountType">
              <div v-if="type === 'view'">{{ form.accountTypeName }}</div>
              <p-select v-model="form.accountType" :options="options.BMS_ACCOUNT_TYPE" allowClear v-else />
            </p-form-model-item>
          </a-col>
        </a-row>
      </p-form-model>
    </p-card>
  </p-spin>
</template>

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

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

      form: {},
      rules: {
        accountWayCode: [{ required: true, message: '结算编码不能为空', trigger: 'blur' }],
        accountWayName: [{ required: true, message: '结算名称不能为空', trigger: 'blur' }],
        accountType: [{ required: true, message: '结算类型不能为空', trigger: 'change' }],
      },
    }
  },
  mounted() {
    if (this.editId) {
      this.spinning = true
      View({ id: this.editId })
        .then((res) => {
          if (res.success) {
            this.form = { ...res.data }
          } else {
            this.$exception(res)
          }
        })
        .finally(() => {
          this.spinning = false
        })
    }
  },
  methods: {
    back() {
      this.$emit('back')
    },

    // 保存
    onSave() {
      this.$refs.ruleForm.validate((valid) => {
        if (!valid) return
        if (this.type === 'add' || this.type === 'copy') this.onMethod(Save)
        if (this.type === 'edit') this.onMethod(Update)

        return
      })
    },
    onMethod(method) {
      let params = { ...this.form }

      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>