<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 {addGroup, getGroup, updateGroup, resNameList} from "@/api/disRevenue/resource"
  export default {
    name: 'GroupDetails',
    components: {Form},
    data() {
      return {
        ruleForm: {},
        formList: [],
        paramsData: {}
      }
    },
    created() {
      this.paramsData = this.$route && this.$route.query;
      if (this.paramsData && this.paramsData.id) {
        this.getFormDataList(this.paramsData.id);
      }
      this.fnFormList();
      this.getResNameList();
    },
    methods: {
      // formList集合
      fnFormList(objVal) {
        this.formList = [{
          config: {title: '基本信息'},
          controls: {
            id: {label: 'ID',hidden: true},
            groupName: {label: '名称', span: 24, type: 'input', rules: [{required: true, message: '请输入名称', trigger: 'blur'}]},
            description: {label: '描述', span: 24, type: 'textarea'},
            includedDevices: {label: '包含设备', span: 12, type: 'transfer',options: []}
          }
        }];
      },
      // 获取详情
      getFormDataList(id) {
        getGroup(id).then(val => {
          if (val && val.data) {
            val.data['includedDevices'] = val.data['includedDevices'].split(',');
            this.ruleForm = val.data;
          }
        }).catch(() => {
          this.$modal.msgError("操作失败")
        });
      },
      // 包含设备
      getResNameList() {
        resNameList().then(val => {
          this.formList[0].controls['includedDevices']['options']= val && val.map(item => {
            return Object.assign({label: item.resourceName, key: item.resourceName});
          });
        }).catch(() => {
          this.$modal.msgError("操作失败")
        });
      },
      // 监听事件
      callback(result, dataVal, formVal) {
        if (result && result.fnCode) {
          switch (result.fnCode) {
            case 'submit':
              dataVal['includedDevices'] = dataVal['includedDevices'].join();
              let fnType = addGroup;
              if (dataVal && dataVal.id) {
                fnType = updateGroup;
              }
              fnType(dataVal).then(response => {
                this.$modal.msgSuccess(response.msg);
                this.$router.push("/resource/group")
              }).catch(() => {
                this.$modal.msgError("操作失败")
              });
              break;
            case 'cancle':
              this.$router.push("/resource/group");
              break;
            default:
          }
        }
      }
    }
  }
</script>
<style>
</style>
