<template>
  <div
    :class="[
      'inputs',
      'ht-color-picker',
      formInputsDisplay == 'block'
        ? 'ht-form-inputs__block'
        : 'ht-form-inputs__inline',
    ]"
  >
    <!---->

    <template v-if="inputWriteable">
      <template v-if="isSubCellReadOnly">
        <div class="sub-table-cell-readonly" @click="handleCellClick">
          <span>
            {{ inputVal }}
          </span>
        </div>
      </template>
      <template v-else>
        <div v-if="!isMobile" class="ht-color-picker-content">
          <el-color-picker
            ref="colorPicker"
            v-model="inputVal"
          ></el-color-picker>
          <i class="el-icon-caret-bottom ht-color-picker-icon"></i>
          <!-- <i class="el-icon-caret-top ht-color-picker-icon"></i> -->
        </div>

        <div v-else>
          <div class="mobile-input-box">
            <van-cell
              class="mobile-cell"
              :name="inputName"
              :data-vv-as="name"
              title=" "
              :is-link="true"
              @click="handleShowPicker"
            >
              <template #default>
                <span v-if="inputVal" class="cell-text">
                  {{ inputVal }}
                </span>
                <span v-if="!inputVal" class="placeholder-class">
                  {{ mobilePlaceholder('请选择') }}
                </span>
                <van-icon
                  v-if="inputVal"
                  name="clear"
                  color="#bfbfbf"
                  class="cell-close"
                  @click.stop="clearSelect"
                />
              </template>
            </van-cell>
          </div>

          <van-popup
            v-model="showPicker"
            position="bottom"
            round
            class="custom-van-pop"
            get-container="body"
          >
            <div class="pop-header">
              <span class="cancel" @click="onCancel">取消</span>
              <span class="title">选择颜色</span>
              <span class="confirm" @click="onConfirm">确定</span>
            </div>
            <div class="pop-content">
              <sketch-picker v-model="colors" />

              <div class="custom-van-pop-input">
                <van-field
                  v-model="fieldColor"
                  class="custom-van-pop-input-field"
                  @input="onChangeColor"
                />
                <div class="custom-van-pop-input-clear" @click="onClear">
                  清空
                </div>
              </div>
            </div>
          </van-popup>
        </div>
      </template>
    </template>
    <!-- <ht-field-tail
      :field-name="inputName"
      :readonly="!inputWriteable"
      :input-value="inputVal"
    /> -->
    <div 
      v-else
      class="color-display" 
      :style="{ backgroundColor: inputVal }"
    ></div>
  </div>
</template>

<script>
  import { Sketch } from 'vue-color'
  import HtFieldTail from '../../FieldTail/index'
  import form from '@/mixins/form.js'
  import permission from '@/mixins/permission.js'
  import mobileMode from '@/mixins/mobileMode.js'
  import popupMode from '@/mixins/popupMode.js'
  import inputName from '@/mixins/inputName.js'
  import tableFieldReadOnly from '@/mixins/tableFieldReadOnly.js'
  export default {
    name: 'HtColorPicker',
    components: { 'sketch-picker': Sketch, HtFieldTail },
    mixins: [
      permission,
      inputName,
      form,
      mobileMode,
      popupMode('showPicker'),
      tableFieldReadOnly,
    ],
    props: {
      value: {
        type: String,
      },
    },
    data() {
      return {
        showPicker: false,
        colors: {
          hex: '',
        },
        fieldColor: '',
      }
    },
    computed: {
      inputVal: {
        get: function () {
          return this.value
        },
        set: function (newValue) {
          this.$emit('input', newValue)
        },
      },
    },
    watch: {
      colors: {
        handler(val) {
          if (val) {
            if (val.hex) {
              this.fieldColor = val.hex
            }
          }
        },

        deep: true,
      },
    },
    created() {},
    mounted() {},

    methods: {
      handleShowPicker() {
        this.colors = {
          hex: this.inputVal,
        }
        this.fieldColor = this.RgbToHex(this.inputVal)
        this.showPicker = true
      },
      clearSelect() {
        this.$emit('input', '')
      },
      //取消
      onCancel() {
        this.showPicker = false
      },
      //确定
      onConfirm() {
        this.$emit('input', this.RgbToHex(this.fieldColor))
        this.showPicker = false
      },
      //颜色改变
      onChangeColor(val) {
        this.fieldColor = val
      },
      //清空
      onClear() {
        this.fieldColor = ''
        this.colors = {
          hex: '',
        }
      },
      //hex 颜色转rgb
      hexToRgb(hexValue, opc = 1) {
        var rgx = /^#?([a-f\d])([a-f\d])([a-f\d])$/i
        var hex = hexValue.replace(rgx, function (m, r, g, b) {
          return r + r + g + g + b + b
        })
        var rgb = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex)
        if (!rgb) {
          return hexValue
        }
        var r = parseInt(rgb[1], 16)
        var g = parseInt(rgb[2], 16)
        var b = parseInt(rgb[3], 16)
        return 'rgba(' + r + ',' + g + ',' + b + ',' + opc + ')'
      },
      //rgb颜色转hex
      RgbToHex(value) {
        if (/rgba?/.test(value)) {
          var array = value.split(',')
          //不符合rgb或rgb规则直接return
          if (array.length < 3) return ''
          value = '#'
          for (var i = 0, color; (color = array[i++]); ) {
            if (i < 4) {
              //前三位转换成16进制
              color = parseInt(color.replace(/[^\d]/gi, ''), 10).toString(16)
              value += color.length == 1 ? '0' + color : color
            } else {
              //rgba的透明度转换成16进制
              color = color.replace(')', '')
              var colorA = parseInt(color * 255)
              var colorAHex = colorA.toString(16)
              value += colorAHex
            }
          }
          value = value.toUpperCase()
        }
        return value
      },
    },
  }
</script>
<style lang="scss" scoped>
  //@import url(); 引入公共css类
  .ht-color-picker {
    &-content {
      cursor: pointer;
      display: flex;
      align-items: center;
      width: 68px;
      height: 24px;
      background: #ffffff;
      border-radius: 4px 4px 4px 4px;
      opacity: 1;
      border: 1px solid #dcdee0;
      padding: 3px 4px;
    }
    &-icon {
      color: #8c8c8c;
      font-size: 16px;
      margin-left: -16px;
      z-index: 1;
      cursor: pointer;
    }
    ::v-deep {
      .el-color-picker {
        height: 26px;
      }
      .el-color-picker__trigger {
        width: auto;
        height: 26px;
        border: none;
        padding: 0;
        border-radius: 0;
        padding-right: 20px;
        z-index: 2;
      }
      .el-color-picker__color {
        width: 40px;
        height: 26px;

        border-radius: 4px 4px 4px 4px;
      }
      .el-icon-arrow-down:before {
        content: '';
      }
      .el-color-picker__mask {
        height: calc(100% - 2px);
        width: calc(100% - 2px);
      }
    }
  }
  .mobile-input-box {
    height: 32px;
    position: relative;
  }
  .van-cell {
    min-height: 32px;
    padding-top: 3px;
    padding-bottom: 0;
    padding-right: 0;
    &::after {
      display: none;
    }
    .van-cell__title {
      width: 1px;
      flex-grow: 0;
    }
  }
  .cell {
    position: absolute;
    top: 0;
    left: 0;
    display: flex;
    align-items: center;
    .van-cell__value {
      display: flex;
      align-items: center;
      justify-content: flex-end;
      .cell-text {
        color: #262626;
        font-size: 14px;
      }
      .cell-close {
        margin-left: 8px;
      }
    }
    .placeholder-class {
      color: #bfbfbf;
      font-size: 14px;
    }
  }
  .mobile-cell {
    padding: 0;
    display: flex;
    align-items: center;
    .van-cell__value {
      display: flex;
      align-items: center;
      justify-content: flex-end;
      .cell-text {
        color: #262626;
        font-size: 14px;
        text-align: right;
      }
      .cell-close {
        margin-left: 4px;
      }
    }
    .placeholder-class {
      color: #bfbfbf;
      font-size: 14px;
      text-align: right;
    }
    .right-arrow {
      transform: rotate(90deg);
    }
  }
  .custom-van-pop {
    max-height: 80%;
    display: flex;
    flex-direction: column;

    ::v-deep .van-field {
      padding-right: 12px !important;
      padding-top: 5px;
    }
    .pop-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 12px 15px;
      border-bottom: 1px solid #e6e6e6;
      .cancel {
        font-size: 14px;
        color: #8c8c8c;
      }
      .title {
        font-size: 17px;
        color: #1a1a1a;
        font-weight: bold;
      }
      .confirm {
        font-size: 14px;
        color: #409eff;
      }
    }
    .pop-content {
      margin: 20px;
      flex: 1;
      overflow: auto;
      -webkit-overflow-scrolling: touch;
    }
    &-input {
      display: flex;
      justify-content: center;
      align-items: center;
      &-clear {
        font-size: 14px;
        white-space: nowrap;
        color: #409eff;
      }
      &-field {
        border: 1px solid rgba(0, 0, 0, 0.1);
        margin-right: 5px;
        width: calc(100% - 5px);
      }
    }

    ::v-deep {
      .vc-sketch-field,
      .vc-sketch-presets,
      .vc-sketch-alpha-wrap {
        display: none !important;
      }
      .vc-sketch-hue-wrap {
        height: 15px;
        margin: 10px 0;
      }

      .vc-sketch-controls {
        display: flex;
        align-items: center;
      }
      .vc-sketch {
        padding: 0 !important;
        box-shadow: none;
        width: auto;
      }
    }
  }
  .color-display {
    width: 38px; // 可根据需求调整宽度
    height: 24px; // 可根据需求调整高度
    border: 1px solid #dcdee0; // 边框样式
    border-radius: 4px; // 圆角
    margin-left: 5px; // 左边距
  }
</style>
