<template>
  <div class="prod-params-json">
    <div class="left-cmds">
      <el-form :model="form.data.params" v-if="paramsList && paramsList.length > 0" ref="loginForm" label-width="120px">
        <!--" 不校验的判断　-->
        <el-form-item
          v-for="paramsItem in paramsList"
          :key="paramsItem.funId"
          :label="paramsItem.funName +　'：'"
          :prop="paramsItem.funKey"
        >
          <!-- :rules="retrunRules(paramsItem)"  验证 -->
          <el-select
            style="width: 85%;"
            v-model="form.data.params[paramsItem.funKey]"
            v-if="paramsItem.funValidType === 'ENUM'"
            clearable
          >
            <el-option
              v-for="(keys, valus) in strtoJson(paramsItem.funObj)"
              :key="valus"
              :label="keys"
              :value="valus"
            ></el-option>
          </el-select>
          <el-input-number
            style="width: 85%;"
            v-model="form.data.params[paramsItem.funKey]"
            v-else-if="paramsItem.funValidType === 'RANGE' && (paramsItem.funDataType === 'FLOAT' || paramsItem.funDataType === 'INT32')"
            :max="Number(paramsItem.funValMax) || 0"
            :min="Number(paramsItem.funValMin) || 0"
            clearable
          ></el-input-number>

          <el-input-number
            style="width: 85%;"
            clearable
            v-model="form.data.params[paramsItem.funKey]"
            v-else-if="paramsItem.funValidType === 'NOT' && (paramsItem.funDataType === 'FLOAT' || paramsItem.funDataType === 'INT32')"
          ></el-input-number>
          <el-input
            clearable
            style="width: 85%;"
            v-else-if="paramsItem.funDataType === 'TEXT'"
            v-model="form.data.params[paramsItem.funKey]"
          />
        </el-form-item>
      </el-form>
      <span v-else>暂无数据</span>
    </div>
    <div class="right-json">
      <div class="option-wrap">
        <el-button type="text" @click="copeText">复制</el-button>
      </div>
      <pre>{{form}}</pre>
    </div>
  </div>
</template>
<script>
export default {
  name: "ParamsJson",
  props: {
    other: {
      type: Object,
      default: {
        action: '',
        prodPK: '',
        cmdKey: ''
      }
    },
    paramsList: {
      type: Array,
      default: []
    }
  },
  data() {
    return {
      form: {
        action: "",
        msgId: "1",
        pk: "",
        devId: "devId",
        data: {
          cmd: "cmd1",
          params: {}
        }
      }
    };
  },
  created() {
    if (this.paramsList) {
      this.form.action = this.other.action;
      this.form.pk = this.other.prodPK;
      this.form.data.cmd = this.other.cmdKey;
      this.handelList();
    }
  },
  methods: {
    copeText() {
      this.copeFu(JSON.stringify(this.form), this);
    },
    strtoJson(str) {
      return str ? JSON.parse(str.replace(/\\\"/g, '"')) : {};
    },
    handelList() {
      var result = {};
      this.paramsList.forEach(v => {
        if (!result[v.funKey]) {
          result[v.funKey] = "";
        }
      });
      this.form.data.params = result;
      this.$forceUpdate();
    }
  }
};
</script>
<style lang="scss">
.prod-params-json {
  width: 100%;
  display: flex;
  flex-wrap: wrap;
  .left-cmds {
    width: 50%;
    display: flex;
    justify-content: center;
  }
  .right-json {
    width: 50%;
    display: flex;
    flex-wrap: wrap;
    .option-wrap {
      width: 100%;
      height: 25px;
      display: flex;
      justify-content: flex-end;
      padding-right: 50px;
    }
    > pre {
      height: calc(100% - 30px);
    }
  }
}
</style>
