<template>
  <el-dialog
    class="dialog-site-label"
    :visible="visible"
    :before-close="e => hideDialog(false)"
    :title="isEdit ? 'Edit Label' : 'Add Label'">
    <el-form
      ref="slbForm"
      :model="form"
      :rules="rules"
      label-position="top"
      v-loading="initial">
      <div class="form-row">
        <el-form-item
          prop="siteLabelName"
          class="form-item"
          label="Label Name:">
          <el-input
            v-model="form.siteLabelName"
            class="flex-item"
            maxlength="30"/>
        </el-form-item>
      </div>
      <div class="form-row">
        <el-form-item
          prop="siteLabelBackground"
          class="form-item colorPanel"
          label="Color:">
          <el-radio-group
            v-model="form.siteLabelBackground"
            class="label-color-view">
            <el-radio-button
              v-for="(item,index) in labelColors"
              :label="item"
              :key="index"
              class="label-color-item"
              :style='"background-color:" + item + ";"'>
              {{index}}
            </el-radio-button>
          </el-radio-group>
        </el-form-item>
      </div>
      <div class="flexcc" style="margin-top: 50px;">
        <el-button
          class="button"
          type="primary"
          :loading="loading"
          @click="onFormSave">
          <span style="padding: 0 20px;">SAVE</span>
        </el-button>
      </div>
    </el-form>
  </el-dialog>
</template>

<script>
import api from '../../http/api/site.js'

export default {
  name: "SiteLabelDetail",
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    isEdit: {
      type: Boolean,
      default: false
    },
    id: {
      type: String|Number,
      default: ''
    }
  },
  data() {
    return {
      initial: false,
      loading: false,
      form: {
        siteLabelId: "",
        siteLabelName: "",
        siteLabelBackground: ""
      },
      rules: {
        siteLabelName: {
          required: true,
          message: "Label name is required",
          trigger: "blur"
        }, 
        siteLabelBackground: {
          required: true,
          trigger: "change",
          message: "Please select a color"
        }
      },
      labelColors: [
        "#001489", "#0025f9", "#417df5", "#24B4F3", "#74E1FE",
        "#127946", "#009E81", "#14CE14", "#15e815", "#ABDC00",  
        "#bf3f00", "#df3700", "#ff5500", "#ef9801", "#F9B70D", 
        "#9F0511", "#B41519", "#C92A17", "#E63726", "#EB725F",
        "#542038", "#A73EA7", "#CE4DCE", "#AC7EEB", "#CAA9DC"
      ]
    };
  },
  watch: {
    id(n, o) {
      if (this.visible && n) {
        this.$nextTick(() => {
          this.getLabelInfo();
        })
      }
    }
  },
  methods: {
    init() {
      this.form = {
        siteLabelId: "",
        siteLabelName: "",
        siteLabelBackground: ""
      }
      this.loading = false;
      this.$nextTick(() => {
        if (this.$refs['slbForm']) {
          this.$refs['slbForm'].clearValidate();
        }
      })
    },
    hideDialog(success) {
      this.init();
      this.$emit("hide", success || false);
    },
    getLabelInfo() {
      this.initial = true;
      api.viewSiteLabel(this.id).then(res => {
        if (res.data) {
          this.form = res.data
        }
      }).catch(err => {
        this.$message({
          type: 'error',
          message: err
        })
      }).finally(() => {
        this.initial = false;
      })
    },
    onFormSave() {
      this.$refs['slbForm'].validate((valid) => {
        if (valid) {
          this.isEdit ? this.updateLabel() : this.addLabel();
        }
      })
    },
    addLabel() {
      this.loading = true;
      api.addSiteLabel(this.form).then(res => {
        this.$message({
          type: 'success',
          message: "Add successfully"
        });
        this.hideDialog(true);
      }).catch(err => {
        this.loading = false;
        this.$message({
          type: 'error',
          message: err
        })
      });
    },
    updateLabel() {
      this.loading = true;
      api.updateSiteLabel(this.form).then(res => {
        this.$message({
          type: 'success',
          message: "Update successfully"
        });
        this.hideDialog(true);
      }).catch(err => {
        this.loading = false;
        this.$message({
          type: 'error',
          message: err
        })
      });
    }
  }
}
</script>

<style scoped>
.dialog-site-label >>> .el-dialog {
  width: 100%;
  max-width: 480px;
}
.dialog-site-label >>> .el-form {
  padding-right: 10px;
}
.form-row {
  display: flex;
  flex-wrap: wrap;
  padding: 0 0 10px;
}
.form-item {
  flex: 1;
  margin-left: 10px;
  margin-bottom: 10px;
}
.form-item.colorPanel {
  overflow-x: auto;
}
.form-item.colorPanel >>> .el-form-item__label {
  left: 0;
  padding: 0;
  position: sticky;
}
.form-item.colorPanel >>> .el-form-item__error {
  top: 0 !important;
  display: flex;
  text-align: left;
  line-height: normal;
  position: relative !important;
  margin-left: 0px !important;
}
.form-item >>> .el-form-item__content {
  text-align: center;
}
.flex-item {
  min-width: 200px;
  max-width: 450px;
}
.flex-item2 {
  width: 100%;
  min-width: 200px;
}
@media screen and (max-width: 500px) {
  .flex-item {
    width: 100%;
    max-width: none;
  }
}
.label-color-view {
  width: 410px;
}
.label-color-item {
  margin: 1px;
  border: none !important;
  border-radius: 2px;
}
.label-color-item >>> .el-radio-button__inner {
  width: 80px;
  height: 40px;
  user-select: none;
  border: none !important;
  color: transparent !important;
  box-shadow: none !important;
  background: transparent !important;
}
.label-color-item.is-active::before {
  color: #fff;
  width: 80px;
  height: 40px;
  content: "\e6da";
  font-size: 22px;
  font-weight: bold;
  text-align: center;
  line-height: 40px;
  position: absolute;
  font-family: element-icons;
}
</style>