<template>
  <el-dialog
    class="dialog-ocpi"
    :visible="visible"
    :before-close="e => hideDialog(false)"
    :close-on-click-modal="false"
    :title="(isEdit ? 'EDIT' : '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="Address:">
        <el-input
          v-model="form.address"
          class="flex-item"
          readonly/>
      </el-form-item>
      <el-form-item
        prop="token"
        class="form-item"
        label="Token:">
        <el-input
          :value="getToken"
          class="flex-item"
          placeholder="Please click on the right button to refresh the token"
          readonly>
          <el-button
            slot="append"
            icon="el-icon-refresh"
            :loading="loading.token"
            title="Refresh Token"
            @click="getConfigTokens"/>
        </el-input>
        <div class="token-tips" v-if="!showToken">Please save your token safe by yourself. It will only be displayed once.</div>
      </el-form-item>
      <el-form-item
        prop="remarks"
        class="form-item"
        label="Remarks:">
        <el-input
          v-model="form.remarks"
          class="flex-item"
          maxlength="200"/>
      </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.save"
          @click="onFormSave">
          <span style="padding: 0 10px;">SAVE</span>
        </el-button>
      </div>
    </el-form>
  </el-dialog>
</template>

<script>
import ocpi from '../../../http/api/ocpi.js';
import setting from '../../../settings.js';
export default {
  name: "DialogConfigs",
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    isEdit: {
      type: Boolean,
      default: false
    },
    info: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      initial: false,
      loading: {
        token: false,
        save: false
      },
      form: {
        credentialTokenId: "",
        address: "",
        token: "",
        remarks: ""
      },
      rules: {
        remarks: {
          required: true,
          message: "Remarks is required",
          trigger: "blur"
        }
      },
      showToken: setting.enableOCPIToken,
      refreshToken: false
    };
  },
  watch: {
    visible(n, o) {
      if (n) {
        if (this.info.credentialTokenId) {
          this.form = this.info;
          this.isEdit = true;
        } else {
          this.isEdit = false;
          this.initial = true;
          this.getConfigAddress();
          this.getConfigTokens();
        }
      } else {
        this.refreshToken = false;
      }
    }
  },
  computed: {
    getToken() {
      if (this.showToken || this.refreshToken) {
        return this.form.token;
      } /*else if (this.form.token) {
        const tk1 = this.form.token.substring(0, 6);
        const tk2 = this.form.token.substring(this.form.token.length - 5);
        return tk1 + "********************" + tk2;
      }*/ else {
        return "";
      }
    }
  },
  mounted() {
    
  },
  methods: {
    hideDialog(success) {
      this.init();
      this.$emit("hide", success || false);
    },
    init() {
      this.form = {
        credentialTokenId: "",
        token: "",
        remarks: ""
      }
      this.isEdit = false;
      this.loading.save = false;
      this.$nextTick(() => {
        if (this.$refs['ocpiForm']) {
          this.$refs['ocpiForm'].clearValidate();
        }
      })
    },
    getConfigAddress() {
      ocpi.getSendConfigUrls().then(res => {
        this.initial = false;
        if (res.data) {
          this.form.address = res.data
        }
      }).catch(err => {
        this.initial = false;
        this.$message({
          type: 'error',
          message: err
        })
      })
    },
    getConfigTokens() {
      this.loading.token = true;
      ocpi.getSendConfigTokens().then(res => {
        this.loading.token = false;
        if (res.data) {
          this.refreshToken = true;
          this.form.token = res.data
        }
      }).catch(err => {
        this.loading.token = false;
        this.$message({
          type: 'error',
          message: err
        })
      })
    },
    onFormSave() {
      this.$refs['ocpiForm'].validate((valid) => {
        if (valid) {
          this.isEdit ? this.onUpdate() : this.onCreate();
        }
      })
    },
    onCreate() {
      this.loading.save = true;
      ocpi.saveSendConfig(this.form).then(res => {
        this.$message({
          type: 'success',
          message: "Send successfully"
        });
        this.hideDialog(true);
      }).catch(err => {
        this.loading.save = false;
        this.$message({
          type: 'error',
          message: err
        })
      })
    },
    onUpdate() {
      this.loading.save = true;
      ocpi.updateSendConfig(this.form).then(res => {
        this.$message({
          type: 'success',
          message: "Update successfully"
        });
        this.hideDialog(true);
      }).catch(err => {
        this.loading.save = 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: 600px;
  margin-top: 0 !important;
}
.dialog-ocpi >>> .el-dialog__body {
  padding: 10px 20px 30px;
}
.dialog-ocpi >>> .el-form {
  padding-right: 10px;
}
.dialog-ocpi >>> .el-input-group__append {
  width: auto;
  height: 40px;
  text-align: center;
  line-height: 40px;
}
.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: 500px;
}
@media screen and (max-width: 500px) {
  .flex-item {
    width: 100%;
    max-width: none;
  }
}
.token-tips {
  color: #000;
  font-size: 13px;
  padding-top: 10px;
  font-weight: bold;
  line-height: 16px;
}
</style>