<template>
  <div class="app-container">
    <Form :formList="formList" :ruleFormData="ruleForm" @fnClick="callback"></Form>
  </div>
</template>

<script setup name="Handle">
  import Form from '@/components/form/index.vue';
  import {addBusiness, countBusinessName, getBusinessCode} from "@/api/disRevenue/earnManage"
  export default {
    name: 'BusinessDetails',
    components: {Form},
    data() {
      return {
        loading: true,
        ruleForm: {
          id: '',
          businessName: ''
        },
        formList: [],
      }
    },
    created() {
      this.fnBusinessCode();
      this.fnFormList();
    },
    methods: {
      // formList集合
      fnFormList(objVal) {
        this.formList = [{
          config: {title: '基本信息'},
          controls: {
            businessName: {label: '业务名称', span: 12, type: 'input', rules: [{required: true, message: '请输入业务名称', trigger: 'blur'}]},
            id: {label: '业务代码', span: 12, type: 'input', style: 'display: block', disabled: true},
          }
        }];
      },
      // 业务编码
      fnBusinessCode() {
        getBusinessCode().then(val => {
          this.$set(this.ruleForm, 'id', val);
        }).catch(() => {
          this.$modal.msgError("操作失败")
        });
      },
      // 监听事件
      callback(result, dataVal, formVal) {
        if (result && result.fnCode) {
          switch (result.fnCode) {
            case 'submit':
              this.loading = true;
              countBusinessName(dataVal).then(response => {
                addBusiness(dataVal).then(response => {
                  this.$modal.msgSuccess(response.msg);
                  this.$router.push("/earnManage/business");
                  this.loading = false;
                }).catch(() => {
                  this.$modal.msgError("操作失败");
                  this.loading = false;
                });
              }).catch(error => {
                // this.$modal.msgError("存在重复的业务名称，请重新输入！");
                this.loading = false;
              });
              break;
            case 'cancel':
              this.$router.push("/earnManage/business");
              break;
            default:
          }
        }
      }
    }
  }
</script>
<style>
</style>
