<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 {addAgentManage, getAgentManage, updateAgentManage, resNameList} from "@/api/disRevenue/resource"
  export default {
    name: 'AgentUpdateView',
    components: {Form},
    dicts: ['policy_method', 'rm_register_online_state', 'agent_update_result'],
    data() {
      return {
        ruleForm: {fileUrlType: '1'},
        formList: [],
        paramsData: {},
        config: {},
        includeList: {}
      }
    },
    created() {
      this.paramsData = this.$route && this.$route.query;
      if (this.paramsData && this.paramsData.id) {
        this.getFormDataList(this.paramsData.id);
      } else {
        this.fnFormList();
        this.getResNameList();
      }
      if (this.paramsData && this.paramsData.readonly) {
        this.config = {
          buttonGroup: [{title: '返回', fnCode: 'goBack'}]
        };
      }
    },
    methods: {
      // formList集合
      fnFormList(objVal) {
        this.formList = [{
          config: {title: '配置更新策略', colSpan: 'disBlock', readonly: this.paramsData.readonly},
          controls: {
            id: {label: 'ID',hidden: true},
            hardwareSn: {label: '硬件SN', span: 12, type: 'input', disabled: true, hidden: this.paramsData && this.paramsData.id ? false : true},
            resourceName: {label: '资源名称', span: 12, type: 'input', disabled: true, hidden: this.paramsData && this.paramsData.id ? false : true},
            internalIp: {label: '内网IP', span: 12, type: 'input', disabled: true, hidden: this.paramsData && this.paramsData.id ? false : true},
            status: {label: '状态', span: 12, type: 'select', options: this.dict.type.rm_register_online_state, disabled: true, hidden: this.paramsData && this.paramsData.id ? false : true},
            agentVersion: {label: 'AGENT版本', span: 12, type: 'input', disabled: true, hidden: this.paramsData && this.paramsData.id ? false : true},
            method: {label: '更新方式', span: 12, type: 'select', options: this.dict.type.policy_method, eventName: 'change', required: true},
            scheduledUpdateTime: {label: '更新时间', span: 12, type: 'datetime', required: true, hidden: objVal && objVal.method === '1' ? false: true},
            fileUrlType: {label: '文件格式', span: 12, type: 'radio', required: true,options: [{label: '外网HTTP(S)',value: '1'}], warningTitle: '注意：当文件大小超过100M时，请选择【外网HTTP(S)】地址格式'},
            fileUrl: {label: '文件地址', span: 12, type: 'input', required: true, placeholder: '请输入外网地址'},
            includeIds: {label: '生效服务器', span: 24,required: true, type: 'transfer',options: [],hidden: this.paramsData && this.paramsData.id ? true : false},
            lastUpdateResult: {label: '最后一次更新结果', span: 12, type: 'select', options: this.dict.type.agent_update_result, disabled: true, hidden: this.paramsData && this.paramsData.id ? false : true},
            lastUpdateTime: {label: '最后一次更新时间', span: 12, type: 'datetime', disabled: true, hidden: this.paramsData && this.paramsData.id ? false : true},
          }
        }];
      },
      // 获取详情
      getFormDataList(id) {
        getAgentManage(id).then(val => {
          if (val && val.data) {
            val.data['method'] = val.data['method'].toString();
            val.data['fileUrlType'] = val.data['fileUrlType'].toString();
            this.fnFormList(val.data);
            this.ruleForm = val.data;
          }
        }).catch(() => {
          // this.$modal.msgError("操作失败")
        });
      },
      // 包含设备
      getResNameList() {
        resNameList().then(val => {
          this.formList[0].controls['includeIds']['options']= val && val.map(item => {
            this.includeList[item.id] = item;
            return Object.assign({label: item.resourceName, key: item.id});
          });
        }).catch(() => {
          this.$modal.msgError("操作失败")
        });
      },
      // 监听事件
      callback(result, dataVal, formVal) {
        if (result && result.fnCode) {
          switch (result.fnCode) {
            case 'method':
              if (dataVal === '1') {
                this.formList[0].controls.scheduledUpdateTime['hidden'] = false;
              } else {
                this.formList[0].controls.scheduledUpdateTime['hidden'] = true;
              }
              break;
            case 'submit':
              if (dataVal && !dataVal.id) {
                dataVal['includeNames'] = dataVal && dataVal['includeIds'].map(id => this.includeList[id].resourceName);
                dataVal['includeIds'] = dataVal['includeIds'].join();
                dataVal['includeNames'] = dataVal['includeNames'].join();
              }
              let fnType = addAgentManage;
              if (dataVal && dataVal.id) {
                fnType = updateAgentManage;
              }
              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>
