<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, getRegistList} 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: '', readonly: this.paramsData.readonly},
          controls: {
            id: {label: 'ID',hidden: true},
            hardwareSn: {label: '硬件SN', span: 18, type: 'input', disabled: true, hidden: this.paramsData && this.paramsData.readonly ? false : true},
            // resourceName: {label: '资源名称', span: 18, type: 'input', disabled: true, hidden: this.paramsData && this.paramsData.id ? false : true},
            // internalIp: {label: '内网IP', span: 18, type: 'input', disabled: true, hidden: this.paramsData && this.paramsData.id ? false : true},
            clientId: {label: 'ClientID', span: 18, type: 'input', disabled: true, hidden: this.paramsData && this.paramsData.readonly ? false : true},
            managePublicIp: {label: '管理网-公网IP', span: 18, type: 'input', disabled: true, hidden: this.paramsData && this.paramsData.readonly ? false : true},
            status: {label: '状态', span: 18, type: 'select', options: this.dict.type.rm_register_online_state, disabled: true, hidden: this.paramsData && this.paramsData.readonly ? false : true},
            agentVersion: {label: 'AGENT版本', span: 18, type: 'input', disabled: true, hidden: this.paramsData && this.paramsData.readonly ? false : true},
            method: {label: '更新方式', span: 18, type: 'select', options: this.dict.type.policy_method, eventName: 'change', required: true, disabled: this.paramsData && this.paramsData.id ? true : false,},
            scheduledUpdateTime: {label: '定时执行时间', span: 18, type: 'datetime', required: true, hidden: objVal && objVal.method === '1' ? false: true, disabled: this.paramsData && this.paramsData.id ? true : false,},
            // fileUrlType: {label: '文件格式', span: 18, type: 'radio', required: true,options: [{label: '外网HTTP(S)',value: '1'}], warningTitle: '注意：当文件大小超过100M时，请选择【外网HTTP(S)】地址格式'},
            fileUrl: {label: '文件地址', span: 18, type: 'input', required: true, placeholder: '请输入外网地址', warningTitle: '如：http://www.tr.com/server-1.1.1.jar'},
            fileMd5: {label: '文件MD5', span: 18, type: 'input', required: true},
            deployDevice: {label: '部署设备', span: 18, type: 'textarea', rows:15, placeholder: '请粘贴/输入ClientID列表，如\nClientID1\nClientID2\n...', required: true, disabled: this.paramsData && this.paramsData.id ? true : false, hidden: this.paramsData && this.paramsData.readonly ? true : false},
            clientAllId: {label: '加载全部ClientID', span: 3, type: 'button', style: 'vertical-align: top', hidden: this.paramsData && this.paramsData.readonly ? true : this.paramsData && this.paramsData.id ? true : false},
            // includeIds: {label: '生效服务器', span: 24,required: true, type: 'transfer',options: [],hidden: this.paramsData && this.paramsData.id ? true : false},
            lastUpdateResult: {label: '最后一次更新结果', span: 18, type: 'select', options: this.dict.type.agent_update_result, disabled: true, hidden: this.paramsData && this.paramsData.readonly ? false : true},
            lastUpdateTime: {label: '最后一次更新时间', span: 18, type: 'datetime', disabled: true, hidden: this.paramsData && this.paramsData.readonly ? false : true},
          }
        }];
      },
      // 获取详情
      getFormDataList(id) {
        getAgentManage(id).then(val => {
          if (val && val.data) {
            val.data['method'] = val.data && val.data['method'] || val.data['method'] === 0 ? val.data['method'].toString() : val.data['method'];
            // 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 'clientAllId':
              this.$set(this.ruleForm, 'deployDevice', '');
              getRegistList({resourceType: 1}).then(res => {
                let str = '';
                if (res && res.data) {
                  res && res.data.map(item => {
                    str+= item.clientId + '\n';
                  });
                }
                this.$set(this.ruleForm, 'deployDevice', str);
                // this.formList[0].controls.clientId['options'] = res && res.data.map(item => {
                //   return Object.assign({label: item.clientId, value: item.id});
                // });
              });
              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 'cancel':
              this.$router.push("/resource/agentUpdate");
              break;
            default:
          }
        }
      }
    }
  }
</script>
<style>
</style>
