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

<script setup>
  import Form from '@/components/form/index.vue';
  import {addBusScript, getBusScript, updateBusScript} from "@/api/disRevenue/earnManage"
  export default {
    name: 'BusinessScriptDetails',
    components: {Form},
    data() {
      return {
        loading: true,
        ruleForm: {
          id: '',
          businessName: ''
        },
        formList: [],
        config: {},
        paramsData: {}
      }
    },
    created() {
      this.paramsData = this.$route && this.$route.query;
      if (this.paramsData && this.paramsData.readonly) {
        this.config = {
          buttonGroup: [{title: '返回', fnCode: 'goBack'}]
        };
      }
      if (this.paramsData && this.paramsData.id) {
        this.getFormDataList(this.paramsData.id);
      }
      this.fnFormList();
    },
    methods: {
      // formList集合
      fnFormList(objVal) {
        this.formList = [{
          config: {title: '基本信息', colSpan: this.paramsData && this.paramsData.readonly ? '' : 'disBlock', readonly: this.paramsData && this.paramsData.readonly},
          controls: {
            id: {label: 'ID',hidden: true},
            scriptName: {label: '脚本名称', span: 12, type: 'input', required: true},
            scriptPath: {label: '脚本文件地址', span: 12, type: 'input', required: true},
            defaultParams: {label: '脚本默认参数', span: 12, type: 'input'},
            createBy: {label: '创建人', span: 12, type: 'input', hidden: !(this.paramsData && this.paramsData.readonly)},
            createTime: {label: '创建时间', span: 12, type: 'datetime', hidden: !(this.paramsData && this.paramsData.readonly)},
            updateTime: {label: '修改时间', span: 12, type: 'datetime', hidden: !(this.paramsData && this.paramsData.readonly)},
          }
        }];
      },
      // 获取详情
      getFormDataList(id) {
        getBusScript(id).then(val => {
          if (val && val.data) {
            this.ruleForm = val.data;
          }
        }).catch(() => {
          // this.$modal.msgError("操作失败")
        });
      },
      // 监听事件
      callback(result, dataVal, formVal) {
        if (result && result.fnCode) {
          switch (result.fnCode) {
            case 'submit':
              let fnType = addBusScript;
              if (dataVal && dataVal.id) {
                fnType = updateBusScript;
              }
              fnType(dataVal).then(response => {
                this.$modal.msgSuccess(response.msg);
                this.$router.push("/earnManage/businessScript");
                this.loading = false;
              }).catch(() => {
                this.$modal.msgError("操作失败");
                this.loading = false;
              });
              break;
            case 'cancel':
              this.$router.push("/earnManage/businessScript");
              break;
            default:
          }
        }
      }
    }
  }
</script>
<style>
</style>
