<template>
  <div class="app-container">
    <Form :formList="formList" :ruleFormData="ruleForm" @fnClick="callback"></Form>
    <!--    弹窗  -->
    <el-dialog title="选择包含资源" :visible.sync="open" width="900px" height="300px" append-to-body>
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="按业务名称选择" name="first">用户管理</el-tab-pane>
        <el-tab-pane label="按逻辑节点标识选择" name="second">配置管理</el-tab-pane>
      </el-tabs>
      <el-row :gutter="20">
        <splitpanes :horizontal="this.$store.getters.device === 'mobile'" class="default-theme">
          <pane size="16">
            <el-col>
              <div class="head-container">
                <el-tree :data="deptOptions" :props="defaultProps" :expand-on-click-node="false" ref="tree" node-key="id" default-expand-all highlight-current @node-click="handleNodeClick" />
              </div>
            </el-col>
          </pane>
          <pane size="84">
            <TableList ref="tabRef" :columns="columns" :config="{colHiddenCheck: true, colTopHiddenIcon: true, currentSel: true}" :queryParams="{total: 0}" :tableList="tableList" @fnClick="callback"></TableList>
          </pane>
        </splitpanes>
      </el-row>
      <div style="text-align: right;margin-right: 20px;margin-top: 20px;">
        <el-button type="primary" style="margin-left: 10px;" @click="submitPubilc">确定</el-button>
        <el-button @click="open = false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
  import Form from '@/components/form/index.vue';
  import TableList from "@/components/table/index.vue"
  import {deptTreeSelect } from "@/api/system/user"
  import { Splitpanes, Pane } from "splitpanes"
  import {listAllBusinessList, addTaskStatic, switchNameTree} from "@/api/disRevenue/earnManage"
  import {listAllSwitchName, getRegistList} from "@/api/disRevenue/resource"
  import "splitpanes/dist/splitpanes.css"
  export default {
    name: 'busValueCount_Details',
    components: {Form, TableList, Splitpanes, Pane},
    dicts: ['resource_type', 'caculate_type'],
    data() {
      return {
        activeName: 'first',
        // 所有树选项
        deptOptions: undefined,
        defaultProps: {
          children: "children",
          label: "label"
        },
        ruleForm: {},
        formList: [],
        switchNameList: [],
        paramsData: {},
        busNameList: {},
        open: false,
        tableList: [],
        // 列显隐信息
        columns: {
          id: { label: `ID`,width: '50'},
          clientId: { label: `ClientID`, minWidth: '200', visible: true },
          businessName: { label: `业务名称`,minWidth: '200',visible: true},
          logicalNodeId: { label: `逻辑节点标识`,minWidth: '320',visible: true},
        },
      }
    },
    created() {
      this.paramsData = this.$route && this.$route.query;
      this.fnFormList();
      this.switchList();
      this.getDeptTree();
    },
    methods: {
      handleClick(tab, event) {
        console.log(tab, event);
      },
      /** 查询部门下拉树结构 */
      getDeptTree() {
        deptTreeSelect().then(response => {
          this.deptOptions = response.data;
        })
      },
      // 节点单击事件
      handleNodeClick(data) {
        // this.queryParams.deptId = data.id;
        // this.handleQuery();
      },
      // formList集合
      fnFormList(objVal) {
        this.formList = [{
          config: {title: '基本信息'},
          controls: {
            id: {label: 'ID',hidden: true},
            taskName: {label: '任务名称', span: 18, type: 'input',required: true},
            businessName: {label: '业务名称', span: 18, type: 'select', options:[]},
            resourceType: {label: '包含资源类型', span: 18, type: 'select', eventName: 'change', options: this.dict.type.resource_type, required: true},
            includedResourcesTree: {label: '包含资源', span: 18, type: 'treeSelect', options: [], searchable: true, multiple: true, required: true, hidden: this.ruleForm.resourceType && this.ruleForm.resourceType === '1' ? false : true},
            changeClientId: {label: '选择ClientID', span: 3, type: 'button', eventName: 'change', hidden: this.ruleForm.resourceType && this.ruleForm.resourceType === '1' ? false : true},
            includedResources: {label: '包含资源', span: 18, type: 'cascader', options: [], required: true, hidden: this.ruleForm.resourceType && this.ruleForm.resourceType === '2' ? false : true},
            calculationType: {label: '计算类型', span: 18, type: 'select', options: this.dict.type.caculate_type, eventName: 'change', required: true},
            startTime: {label: '时间段', span: 18, type: 'month', hidden: this.ruleForm.calculationType && this.ruleForm.calculationType === '2' ? false : true},
            endTime: {label: '时间段', span: 18, 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();
          });
        }
      },
      submitPubilc() {
        this.open = false;
        this.getFormDataList(this.currentDataList.id, 'btnGain');
      },
      // 监听事件
      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.changeClientId['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.changeClientId['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 'changeClientId':
              this.open = 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>
