<template>
    <div class="app-container">
        <Form :formList="formList" :ruleFormData="ruleForm" :config="config" @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: 'AgentUpdateView',
    components: {Form},
    data() {
      return {
        ruleForm: {},
        formList: [],
        paramsData: {},
        config: {}
      }
    },
    created() {
      this.paramsData = this.$route && this.$route.query;
      if (this.paramsData && this.paramsData.id) {
        this.getFormDataList(this.paramsData.id);
      }
      if (this.paramsData && this.paramsData.readonly) {
        this.config = {
          buttonGroup: [{title: '返回', fnCode: 'goBack'}]
        };
      }
      this.fnFormList();
      this.getResNameList();
    },
    methods: {
      // formList集合
      fnFormList(objVal) {
        this.formList = [{
          config: {title: '配置更新策略', colSpan: 'disBlock', readonly: this.paramsData.readonly},
          controls: {
            id: {label: 'ID',hidden: true},
            sn: {label: '硬件SN', span: 12, type: 'input', disabled: true, hidden: this.paramsData && this.paramsData.id ? false : true},
            name: {label: '资源名称', span: 12, type: 'input', disabled: true, hidden: this.paramsData && this.paramsData.id ? false : true},
            ip: {label: '内网IP', span: 12, type: 'input', disabled: true, hidden: this.paramsData && this.paramsData.id ? false : true},
            type: {label: '状态', span: 12, type: 'select', disabled: true, hidden: this.paramsData && this.paramsData.id ? false : true},
            agent: {label: 'AGENT版本', span: 12, type: 'input', disabled: true, hidden: this.paramsData && this.paramsData.id ? false : true},
            groupName: {label: '定义更新频率', span: 12, type: 'select', required: true},
            date: {label: '定义更新时间', span: 12, type: 'date', required: true},
            fileType: {label: '文件格式', span: 12, type: 'radio', required: true,options: [{label:'平台文件地址', value: '1'}, {label: '外网HTTP(S)',value: '2'}],
              hidden: this.paramsData && this.paramsData.id ? true : false, warningTitle: '注意：当文件大小超过100M时，请选择【外网HTTP(S)】地址格式'},
            address: {label: '文件目录', span: 12, type: 'select', required: true, warningTitle: '您选择的文件已经超过100M，请更改文件地址格式选择'},
            includedDevices: {label: '生效服务器', span: 24,required: true, type: 'transfer',options: [],hidden: this.paramsData && this.paramsData.id ? true : false},
            lastResult: {label: '最后一次更新结果', span: 12, type: 'select', disabled: true, hidden: this.paramsData && this.paramsData.id ? false : true},
            lastTime: {label: '最后一次更新时间', span: 12, type: 'datetimme', disabled: true, hidden: this.paramsData && this.paramsData.id ? false : true},
          }
        }];
      },
      // 获取详情
      getFormDataList(id) {
        getGroup(id).then(val => {
          if (val && val.data) {
            if (val.data && val.data['includedDevices']) {
              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/agentUpdate")
              }).catch(() => {
                this.$modal.msgError("操作失败")
              });
              break;
            case 'cancle':
              this.$router.push("/resource/agentUpdate");
              break;
            default:
          }
        }
      }
    }
  }
</script>
<style>
</style>
