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

<script setup>
  import Form from '@/components/form/index.vue';
  import {listAllBusinessList, addTaskStatic, switchNameTree} from "@/api/disRevenue/earnManage"
  import {listAllSwitchName, getRegistList} from "@/api/disRevenue/resource"
  export default {
    name: 'busValueCount_Details',
    components: {Form},
    dicts: ['resource_type', 'caculate_type'],
    data() {
      return {
        ruleForm: {},
        formList: [],
        switchNameList: [],
        paramsData: {},
        busNameList: {},
      }
    },
    created() {
      this.paramsData = this.$route && this.$route.query;
      this.fnFormList();
      this.switchList();
    },
    methods: {
      // formList集合
      fnFormList(objVal) {
        this.formList = [{
          config: {title: '基本信息', colSpan: 'disBlock'},
          controls: {
            id: {label: 'ID',hidden: true},
            taskName: {label: '任务名称', span: 12, type: 'input',required: true},
            businessName: {label: '业务名称', span: 12, type: 'select', options:[]},
            resourceType: {label: '包含资源类型', span: 12, type: 'select', eventName: 'change', options: this.dict.type.resource_type, required: true},
            includedResourcesTree: {label: '包含资源', span: 12, type: 'treeSelect', options: [], searchable: true, multiple: true, required: true, hidden: this.ruleForm.resourceType && this.ruleForm.resourceType === '1' ? false : true},
            includedResources: {label: '包含资源', span: 12, type: 'cascader', options: [], required: true, hidden: this.ruleForm.resourceType && this.ruleForm.resourceType === '2' ? false : true},
            calculationType: {label: '计算类型', span: 12, type: 'select', options: this.dict.type.caculate_type, eventName: 'change', required: true},
            startTime: {label: '时间段', span: 12, type: 'month', hidden: this.ruleForm.calculationType && this.ruleForm.calculationType === '2' ? false : true},
            endTime: {label: '时间段', span: 12, type: 'datetimerange', hidden: this.ruleForm.calculationType && this.ruleForm.calculationType === '1' ? false : true},
          }
        }];
      },
      switchList() {
        listAllBusinessList().then(val => {
          this.formList[0].controls.businessName['options'] = val && val.data.map(item => {
            this.busNameList[item.businessName] = item;
            return Object.assign({label: item.businessName, value: item.businessName});
          });
        });
      },
      // 包含资源
      includeDataList(num) {
        if (num === '1') {
          getRegistList({resourceType: 1}).then(val => {
            this.formList[0].controls.includedResourcesTree['options'] = val && val.data.map(item => {
              return Object.assign({label: item.clientId, id: item.clientId});
            });
          });
        } else {
          this.$modal.loading();
          switchNameTree().then(val => {
            this.switchNameList = val && val;
            this.formList[0].controls.includedResources['options'] = val && val.data;
            this.$modal.closeLoading();
          }).catch(() => {
            this.$modal.closeLoading();
          });
        }
      },
      // 监听事件
      callback(result, dataVal, formVal) {
        if (result && result.fnCode) {
          switch (result.fnCode) {
            case 'resourceType':
              if (dataVal) {
                if (dataVal === '1') {
                  this.formList[0].controls.includedResourcesTree['hidden'] = false;
                  this.formList[0].controls.includedResources['hidden'] = true;
                } else if (dataVal === '2') {
                  this.formList[0].controls.includedResources['hidden'] = false;
                  this.formList[0].controls.includedResourcesTree['hidden'] = true;
                }
                this.includeDataList(dataVal);
              } else {
                this.formList[0].controls.includedResourcesTree['hidden'] = true;
                this.formList[0].controls.includedResources['hidden'] = true;
              }
              // if (dataVal) {}
              // this.includeDataList(dataVal);
              break;
            case 'calculationType':
              if (dataVal) {
                if (dataVal === '1') {
                  this.formList[0].controls.startTime['hidden'] = true;
                  this.formList[0].controls.endTime['hidden'] = false;
                } else if (dataVal === '2') {
                  this.formList[0].controls.startTime['hidden'] = false;
                  this.formList[0].controls.endTime['hidden'] = true;
                }
              } else {
                this.formList[0].controls.startTime['hidden'] = true;
                this.formList[0].controls.endTime['hidden'] = true;
              }
              break;
            case 'submit':
              dataVal['calculationMode'] = this.paramsData.calculationMode;
              if (dataVal.includedResources) {
                let newArr = [];
                dataVal.includedResources.forEach(item => {
                  newArr.push(item[1]);
                });
                dataVal.includedResources = newArr.toString();
              }
              if (dataVal.includedResourcesTree) {
                dataVal.includedResources = dataVal.includedResourcesTree.toString();
                delete dataVal.includedResourcesTree;
              }
              if (dataVal && dataVal.businessName) {
                dataVal['businessCode'] = this.busNameList[dataVal.businessName].id;
              }
              if (dataVal && dataVal.endTime) {
                dataVal.startTime = dataVal.endTime[0];
                dataVal.endTime = dataVal.endTime[1];
              }
              addTaskStatic(dataVal).then(response => {
                this.$modal.msgSuccess(response.msg);
                this.$router.push("/earnManage/busValueCount");
              }).catch(() => {
                this.$modal.msgError("操作失败")
              });
              break;
            case 'cancel':
              this.$router.push("/earnManage/busValueCount");
              break;
            default:
          }
        }
      }
    }
  }
</script>
<style>
</style>
