<template>
  <div>
    <el-form
      :model="formData"
      :rules="rule"
      ref="recipForm"
      label-position="left"
      label-width="150px">
      <el-form-item
        label="Recipients:"
        prop="recipient"
        label-width="100px">
        <div class="flexcr" style="margin-top: -5px;">
          <el-input
            class="input-text"
            v-model="formData.recipient"
            placeholder=""
            maxlength="50"/>
          <el-button
            type="primary"
            icon="el-icon-plus"
            @click="addRecipient">
            Add
          </el-button>
        </div>
      </el-form-item>
      <el-form-item
        label="Added Recipients:">
        <div class="flexl">
          <div class="receip-item" v-for="(item, index) in emails" :key="index">
            <span>{{item}}</span>
            <i class="el-icon-close" @click="deleteRecipient(index)"></i>
          </div>
          <div v-if="emails.length > 0">
            <span class="link-button" @click="clearAllRecipient">Clear All</span>
          </div>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  export default {
    name: "EmailRecipient",
    props: {
      value: {
        type: Array,
        default: []
      }
    },
    data() {
      return {
        emails: [],
        formData: {
          recipient: ""
        },
        rule: {
          "recipient": {
            pattern: /^[a-zA-Z0-9]+[\S]+@[a-zA-Z0-9_-]+[\.][\Sa-zA-Z]+$/,
            trigger: 'blur',
            message: 'Please type a correct recipient'
          }
        }
      };
    },
    model: {
      prop: 'value',
      event: 'change'
    },
    watch: {
      value: {
        deep: true,
        handler(n, o) {
          if (!n) {
            this.emails = [];
            this.onChange()
          } else {
            this.emails = n;
          }
        }
      }
    },
    methods: {
      onChange() {
        this.$emit('change', this.emails);
      },
      addRecipient() {
        if (this.formData.recipient) {
          this.$refs["recipForm"].validate(result => {
            if (result) {
              this.emails.push(this.formData.recipient);
              this.formData.recipient = "";
              this.onChange();
            }
          });
        }
      },
      deleteRecipient(index) {
        this.emails.splice(index, 1);
        this.onChange();
      },
      clearAllRecipient() {
        this.$confirm('Clear all recipients?', 'Prompt', {
          confirmButtonText: 'Confirm',
          cancelButtonText: 'Cancel',
          type: 'warning'
        }).then(res => {
          this.emails = [];
          this.onChange();
        })
      }
    }
  }
</script>

<style scoped>
  .input-text {
    width: 100%;
    max-width: 200px;
    margin-top: 5px;
    margin-right: 10px;
    margin-bottom: 5px;
  }
  .link-button {
    color: #3179E4;
    font-size: 14px;
    cursor: pointer;
    margin: 0 -10px;
    padding: 0 10px;
    display: inline-block;
    white-space: nowrap;
    text-decoration: underline;
  }
  .receip-item {
    display: flex;
    align-items: center;
    font-size: 14px;
    line-height: 1em;
    padding-top: 10px;
  }
  .receip-item i {
    color: #666;
    padding: 0 10px;
    cursor: pointer;
  }
  .receip-item i:hover,
  .receip-item i:active,
  .link-button:hover {
    color: #ff5500;
  }
</style>