、、<template>
  <div class="app-container">
    <Form ref="formRef" :formList="formList" :ruleFormData="ruleForm" :config="this.paramsData && this.paramsData.readonly ? {labelWidth: '140px', buttonGroup: []} : {labelWidth: '140px'}" @fnClick="callback"></Form>
    <el-button v-if="this.paramsData && this.paramsData.readonly" style="float: right;margin-top: 12px;margin-left: 10px;" @click="callback({fnCode: 'cancel'})">返回</el-button>
  </div>
</template>

<script setup>
  import Form from '@/components/form/index.vue';
  import {getPolicy, addPolicy, updatePolicy} from "@/api/disRevenue/resource"
  import {getAllBusScriptName} from "@/api/disRevenue/earnManage"
  export default {
    name: 'severScriptStratDetails',
    components: {Form},
    dicts: ['policy_method', 'policy_status'],
    props: {
      open: {
        type: String,
        default: () => {}
      },
      dialogRowData: {
        type: Object,
        default: (() => {})
      }
    },
    watch: {
      open: {
        handler(val) {
          if (val === 'type_false') {
            // 清空form
            this.$refs['formRef'].$refs.ruleForm.resetFields();
          }
        },
        deep: true,
        immediate: true
      },
    },
    data() {
      return {
        loading: false,
        ruleForm: {},
        formList: [],
        paramsData: {},
        scriptList: {},
      }
    },
    created() {
      this.paramsData = this.$route && this.$route.query;
      if (this.open) {
        this.paramsData = {};
        this.$set(this.ruleForm, 'deployDevice', this.dialogRowData.clientId);
      }
      if (this.paramsData && this.paramsData.id) {
        this.getFormDataList(this.paramsData.id);
      }
      this.fnFormList();
      this.getBusScriptNames();
    },
    methods: {
      // formList集合
      fnFormList(objVal) {
        this.formList = [{
          config: {title: '',labelWidth: '140px', readonly: this.paramsData && this.paramsData.readonly, colSpan: 'disBlock m0Auto'},
          controls: {
            id: {label: 'ID',hidden: true},
            policyName: {label: '策略名称', span: 18, type: 'input', required: true},
            description: {label: '描述', span: 18, type: 'textarea'},
            taskName: {label: '关联业务下发任务', span: 18, type: 'input', hidden: !(this.paramsData && this.paramsData.readonly)},
            deployDevice: {label: '部署设备', span: 18, type: 'textarea',rows: 15, required: true},
            scriptName: {label: '脚本名称', span: 18, type: 'select', eventName: 'change', options: [], required: true},
            scriptPath: {label: '脚本文件地址', span: 18, type: 'input', disabled: true, required: true},
            defaultParams: {label: '脚本参数', span: 18, type: 'input'},
            executionMethod: {label: '执行方式', span: 18, type: 'select', eventName:'change', options: this.dict.type.policy_method},
            scheduledTime: {label: '定时时间', span: 18, type: 'datetime', required: true, hidden: true},
            policyStatus: {label: '策略状态', span: 18, type: 'select', options: this.dict.type.policy_status, hidden: !(this.paramsData && this.paramsData.readonly)},
            deployTime: {label: '下发策略时间', span: 18, type: 'datetime', hidden: !(this.paramsData && this.paramsData.readonly)},
            createBy: {label: '创建人', span: 18, type: 'input', hidden: !(this.paramsData && this.paramsData.readonly)},
            createTime: {label: '创建时间', span: 18, type: 'datetime', hidden: !(this.paramsData && this.paramsData.readonly)},
            updateTime: {label: '修改时间', span: 18, type: 'datetime', hidden: !(this.paramsData && this.paramsData.readonly)},
          }
        }];
      },
      // 脚本名称
      getBusScriptNames() {
        getAllBusScriptName().then(val => {
          this.formList[0].controls.scriptName['options'] = val && val.data.map(item => {
            this.scriptList[item.id] = item;
            return Object.assign({label: item.scriptName, value: item.id});
          });
        });
      },
      // 获取详情
      getFormDataList(id) {
        getPolicy(id).then(val => {
          if (val && val.data) {
            if (val.data.executionMethod === 1) {
              this.formList[0].controls.scheduledTime['hidden'] = false;
            }
            if (this.paramsData && this.paramsData.readonly) {
              val.data.deployDevice = val.data.deployDevice.replace(/\n/g, '<br>');
            }
            val.data.executionMethod = val && val.data.executionMethod.toString();
            val.data['scriptName'] = Number(val.data['scriptName']);
            this.ruleForm = val && val.data;
          }
        }).catch(() => {
        });
      },
      // 监听事件
      callback(result, dataVal, formVal) {
        if (result && result.fnCode) {
          switch (result.fnCode) {
            case 'scriptName':
              this.ruleForm = Object.assign({}, this.ruleForm, this.$refs.formRef.$refs.ruleForm.model);
              if (dataVal) {
                let dataListVal = this.scriptList[dataVal];
                this.$set(this.ruleForm, 'scriptPath', dataListVal['scriptPath']);
                this.$set(this.ruleForm, 'defaultParams', dataListVal['defaultParams']);
              }
              break;
            case 'executionMethod':
              if (dataVal && dataVal === '1') {
                this.formList[0].controls.scheduledTime['hidden'] = false;
              } else {
                this.formList[0].controls.scheduledTime['hidden'] = true;
              }
              break;
            case 'submit':
              let fnType = addPolicy;
              if (dataVal && dataVal.id) {
                fnType = updatePolicy;
              }
              if(this.loading) return;
              this.loading = true;
              fnType(dataVal).then(response => {
                this.$modal.msgSuccess(response.msg);
                if (this.open) {
                  this.$emit("dialogResult", {open: false});
                } else {
                  this.$router.push("/resource/serverScriptStrat");
                }
                this.loading = false;
              }).catch(() => {
                this.$modal.msgError("操作失败")
              });
              break;
            case 'cancel':
              if (this.open) {
                this.$emit("dialogResult", {open: false});
              } else {
                this.$router.push("/resource/serverScriptStrat");
              }
              break;
            default:
          }
        }
      }
    }
  }
</script>
<style>
</style>
