<template>
  <el-dialog
    class="dialog-ocpi"
    :visible="visible"
    :before-close="e => hideDialog(false)"
    :close-on-click-modal="false"
    title="CREATE OCPI CONNECTION">
    <el-form
      ref="ocpiForm"
      :model="form"
      :rules="rules"
      label-width="120px"
      label-position="right"
      v-loading="initial">
      <el-form-item
        prop="url"
        class="form-item"
        label="Party URL:">
        <el-input
          v-model="form.url"
          class="flex-item"
          maxlength="100"/>
      </el-form-item>
      <el-form-item
        prop="token"
        class="form-item"
        label="Party Token:">
        <el-input
          class="flex-item"
          type="textarea"
          maxlength="300"
          :autosize="autosize"
          v-model="form.token"/>
      </el-form-item>
      <div class="flexcc" style="padding-top: 20px;">
        <el-button
          class="button"
          @click="hideDialog(false)">
          CANCEL
        </el-button>
        <el-button
          class="button"
          type="primary"
          :loading="loading"
          @click="onFormSave">
          INITIALIZE
        </el-button>
      </div>
    </el-form>
  </el-dialog>
</template>

<script>
import ocpi from '../../../http/api/ocpi.js';
export default {
  name: "DialogOperators",
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      initial: false,
      loading: false,
      form: {
        url: "",
        token: ""
      },
      rules: {
        url: {
          required: true,
          message: "Url is required",
          trigger: "blur"
        },
        token: {
          required: true,
          message: "Token is required",
          trigger: "blur"
        }
      },
      autosize: {
        minRows: 3,
        maxRows: 6
      }
    };
  },
  mounted() {
    
  },
  methods: {
    hideDialog(success) {
      this.init();
      this.$emit("hide", success || false);
    },
    init() {
      this.form = {
        url: "",
        token: ""
      }
      this.loading = false;
      this.$nextTick(() => {
        if (this.$refs['ocpiForm']) {
          this.$refs['ocpiForm'].clearValidate();
        }
      })
    },
    onFormSave() {
      this.$refs['ocpiForm'].validate((valid) => {
        if (valid) {
          this.onReceive();
        }
      })
    },
    onReceive() {
      this.loading = true;
      ocpi.initRoamingOperators(this.form).then(res => {
        this.$message({
          type: 'success',
          message: "Initialize successfully"
        });
        this.hideDialog(true);
      }).catch(err => {
        this.loading = false;
        this.$message({
          type: 'error',
          message: err
        })
      });
    }
  }
}
</script>

<style scoped>
.dialog-ocpi {
  display: flex;
  align-items: center;
  flex-direction: column;
  justify-content: center;
}
.dialog-ocpi >>> .el-dialog {
  width: 100%;
  max-width: 550px;
  margin-top: 0 !important;
}
.dialog-ocpi >>> .el-dialog__body {
  padding: 10px 20px 30px;
}
.dialog-ocpi >>> .el-form {
  padding-right: 10px;
}
.form-row {
  display: flex;
  flex-wrap: wrap;
  padding-bottom: 5px;
}
.form-item {
  flex: 1;
  margin-left: 10px;
  /*margin-bottom: 10px; TOP*/
}
.form-item >>> label {
  /*padding: 0; TOP*/
}
.flex-item {
  width: 100%;
  min-width: 200px;
  max-width: 420px;
}
.flex-item ::v-deep .el-textarea__inner {
  font-family: sans-serif;
}
@media screen and (max-width: 500px) {
  .flex-item {
    width: 100%;
    max-width: none;
  }
}
</style>