<template>
  <transition name="el-zoom-in-top" @after-leave="doDestroy">
    <div
      class="el-color-dropdown color-container"
      v-show="showPopper">
      <div class="el-color-dropdown__main-wrapper">
        
        <sv-panel ref="sl" :color="color"></sv-panel>
        <hue-slider ref="hue" :color="color" vertical></hue-slider>
      </div>
      <div class="el-color-result">
          <div class="line_1">
             <el-button
                  size="mini"
                  type="text"
                  class="el-color-dropdown__link-btn"
                  @click="$emit('clear')">
                  {{ t('el.colorpicker.clear') }}
                </el-button>
                  <el-button
                  plain
                  size="mini"
                  class="el-color-dropdown__btn"
                  @click="confirmValue">
                  {{ t('el.colorpicker.confirm') }}
                </el-button>
          </div>
          <div class="current-color" :style="{backgroundColor:currentColor}"></div>
           <div class="color-text" style="margin-top:13px !important;">
             <span style="margin-right:8px;">R:</span>
              <el-input
                v-model="R"
                :validate-event="false"
                 @keyup.native.enter="handleR"
                @blur="handleR"
                size="mini">
              </el-input>
           </div>
           <div class="color-text">
             <span style="margin-right:8px;">G:</span>
              <el-input
                v-model="G"
                :validate-event="false"
                 @keyup.native.enter="handleG"
                @blur="handleG"
                size="mini">
              </el-input>
           </div>
           <div class="color-text">
             <span style="margin-right:8px;">B:</span>
              <el-input
                v-model="B"
                :validate-event="false"
                @keyup.native.enter="handleB"
                @blur="handleB"
                size="mini">
              </el-input>
           </div>
           <div class="color-text">
             <span style="margin-right:18px;">#</span>
               <el-input
            v-model="custormColor"
            @keyup.native.enter="handleConfirm"
            @blur="handleConfirm"
            :validate-event="false"
            size="mini">
          </el-input>
           </div>
        </div>
      <alpha-slider v-if="showAlpha" ref="alpha" :color="color"></alpha-slider>
      <predefine v-if="predefine" :color="color" :colors="predefine"></predefine>
      <!-- <div class="el-color-dropdown__btns">
        <span class="el-color-dropdown__value">
          <el-input
            v-model="customInput"
            @keyup.native.enter="handleConfirm"
            @blur="handleConfirm"
            :validate-event="false"
            size="mini">
          </el-input>
        </span>
        <el-button
          size="mini"
          type="text"
          class="el-color-dropdown__link-btn"
          @click="$emit('clear')">
          {{ t('el.colorpicker.clear') }}
        </el-button>
        <el-button
          plain
          size="mini"
          class="el-color-dropdown__btn"
          @click="confirmValue">
          {{ t('el.colorpicker.confirm') }}
        </el-button>
      </div> -->
    </div>
  </transition>
</template>

<script>
  import SvPanel from './sv-panel';
  import HueSlider from './hue-slider';
  import AlphaSlider from './alpha-slider';
  import Predefine from './predefine';
  import Popper from 'rx-element-ui/src/utils/vue-popper';
  import Locale from 'rx-element-ui/src/mixins/locale';
  import ElInput from 'rx-element-ui/packages/input';
  import ElButton from 'rx-element-ui/packages/button';
  import './style.css';
  export default {
    name: 'el-color-picker-dropdown',

    mixins: [Popper, Locale],

    components: {
      SvPanel,
      HueSlider,
      AlphaSlider,
      ElInput,
      ElButton,
      Predefine
    },

    props: {
      color: {
        required: true
      },
      showAlpha: Boolean,
      predefine: Array
    },

    data() {
      return {
        customInput: '',
        custormColor: '',
        R: '',
        G: '',
        B: ''
      };
    },

    computed: {
      currentColor() {
        const parent = this.$parent;
        return !parent.value && !parent.showPanelColor ? '' : parent.color.value;
      }
    },

    methods: {
      confirmValue() {
        this.$emit('pick');
      },

      handleConfirm() {
        this.color.fromString(this.customInput);
        console.log(this.color);
        let hexcolor2 = '';
        let color = this.colorToRGB(this.customInput);
        this.R = color[0];
        this.G = color[1];
        this.B = color[2];
        hexcolor2 = this.customInput.replace(/#/, '');
        this.custormColor = hexcolor2;
      },

      handleR() {
        console.log(this.R, this.G, this.B);
        let hex = this.rgbToHex(this.R + ',' + this.G + ',' + this.B);
        this.custormColor = hex;
        this.color.fromString('#' + hex);
        console.log(hex);
      },

      handleG() {
        console.log(this.R, this.G, this.B);
        let hex = this.rgbToHex(this.R + ',' + this.G + ',' + this.B);
        this.custormColor = hex;
        this.color.fromString('#' + hex);
        console.log(hex);
      },

      handleB() {
        console.log(this.R, this.G, this.B);
        let hex = this.rgbToHex(this.R + ',' + this.G + ',' + this.B);
        this.custormColor = hex;
        this.color.fromString('#' + hex);
        console.log(hex);
      },

      colorToRGB(color) {
        let scolor = color.toLowerCase();
        let reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
        if (reg.test(scolor)) {
          if (scolor.length === 4) {
            let scolor2 = '#';
            for (let i = 1 ; i < 4;i += 1) {
              scolor2 += scolor.slice(i, i + 1).concat(scolor.slice(i, i + 1));
            }
            scolor = scolor2;
          }
          let fex = [];
          for (let i = 1 ;i < 7; i += 2) {
            fex.push(parseInt('0x' + scolor.slice(i, i + 2), 16));
          }
          return fex;
        }
      },

      rgbToHex(color) {
        let fex = 'rgb(' + color + ')';
        let result = '';
        console.log(/^(rgb|RGB)/.test(fex), fex);
        if (/^(rgb|RGB)/.test(fex)) {
          let acolor = color.replace(/(?:\(|\)|rgb|RGB)*/g, '').split(',');
          let strHex = '';
          for (let i = 0;i < acolor.length; i++) {
            let hex = Number(acolor[i]).toString(16);
            if (hex.length < 2) {
              hex = '0' + hex;
            }
            strHex += hex;
          }
          return strHex;
        }
        return result;
      }
    },

    mounted() {
      this.$parent.popperElm = this.popperElm = this.$el;
      this.referenceElm = this.$parent.$el;
    },

    watch: {
      showPopper(val) {
        if (val === true) {
          this.$nextTick(() => {
            const { sl, hue, alpha } = this.$refs;
            sl && sl.update();
            hue && hue.update();
            alpha && alpha.update();
          });
        }
      },

      currentColor: {
        immediate: true,
        handler(val) {
          let hexcolor2 = val.replace(/#/, '');
          this.customInput = val;
          this.custormColor = hexcolor2;
          let color = this.colorToRGB(val);
          this.R = color[0];
          this.G = color[1];
          this.B = color[2];
          console.log(color);
        }
      }
    }
  };
</script>

