<template>
  <el-form ref="pushform" :model="pushform" :rules="pushrules">
    <div class="group-check-cnt">
      <el-checkbox
        v-model="checkAll"
        :indeterminate="isIndeterminate"
        @change="handleCheckAllChange"
      >全部</el-checkbox>
      <el-checkbox-group
        v-model="checkedVal"
        class="check-group"
        @change="handleCheckedChange"
      >
        <el-row :gutter="16">
          <el-col
            v-for="(item, index) in groupArrNew"
            :key="item.value"
            :span="12"
          >
            <el-checkbox
              :label="item.value"
              :class="item.show && item.type == 'range' ? 'myCheck' : ''"
              @change="(v) => handleCheckedChangeOne(v, index)"
            >
              {{ item.label }}
              <span
                v-if="checkedVal.includes(item.value)"
                @click.prevent="handCollapseClick(index)"
              >
                <i
                  v-if="item.type == 'range' && !item.show"
                  class="el-icon-caret-bottom collapse"
                />
                <i
                  v-if="item.type == 'range' && item.show"
                  class="el-icon-caret-top collapse"
                />
              </span>
              <div
                v-if="
                  item.type == 'range' &&
                    (item.show || item.defaultShow) &&
                    checkedVal.includes(item.value)
                "
                class="item-box"
                @click.prevent="() => {}"
              >
                <el-form-item
                  label=""
                  :prop="item.key1"
                  :title="pushform[item.key1]"
                >
                  <el-inputNumber
                    v-model="pushform[item.key1]"
                    size="small"
                    :controls="false"
                    @blur="getCurrentValue"
                  />
                </el-form-item>
                <span class="range">~</span>
                <el-form-item label="" :prop="item.key2">
                  <el-inputNumber
                    v-model="pushform[item.key2]"
                    size="small"
                    :controls="false"
                    @blur="getCurrentValue"
                  />
                </el-form-item>
              </div>
              <!-- <el-inputNumber :controls="false"></el-inputNumber> -->
            </el-checkbox>
          </el-col>
        </el-row>
      </el-checkbox-group>
    </div>
  </el-form>
</template>

<script>
import { Message } from 'element-ui'
export default {
  props: {
    groupArr: {
      type: Array,
      required: true
    },
    contiKeys: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      checkedVal: [],
      isIndeterminate: false,
      checkAll: false,
      pushform: {},
      pushrules: {
        // campaignName: [{ required: true, message: "任务名称不能为空" }],
      },
      groupArrNew: []
    }
  },
  watch: {
    groupArr: {
      handler: function(val, oldVal) {
        this.groupArrNew = val.map((item) => {
          this.pushrules[item.key1] = [
            {
              required: true,
              trigger: 'blur',
              message: '请输入',
              validator: (rule, value, callback) => {
                // console.log(rule,value)
                // if (1) {
                //   callback(new Error(""));
                // } else {
                //   callback();
                // }
              }
            }
          ]
          this.pushrules[item.key2] = [
            {
              required: true,
              trigger: 'blur',
              message: '请输入',
              validator: (rule, value, callback) => {
                // console.log(this.pushform[rule.field])
                // if (1) {
                //   callback(new Error(""));
                // } else {
                //   callback();
                // }
              }
            }
          ]
          this.pushform[item.key1] = undefined
          this.pushform[item.key2] = undefined
          return {
            ...item,
            show: false
          }
        })
      },
      immediate: true
    }
  },

  methods: {
    clearData() {
      this.checkedVal = []
      this.checkAll = false
      this.pushform = {}
      this.pushrules = {}
      this.groupArrNew = []
    },
    handCollapseClick(index) {
      this.groupArrNew[index].show = !this.groupArrNew[index].show
      this.groupArrNew[index].defaultShow = false
    },
    handleCheckedChangeOne(v, index) {
      console.log(v, index)
      if (v) {
        this.groupArrNew[index].defaultShow = true
        this.groupArrNew[index].show = true
      }
    },
    handleCheckedChange(value) {
      console.log(value)
      for (const key in this.pushform) {
        const newkey = key.split('-')[0]
        if (!value.includes(newkey)) {
          this.pushform[key] = undefined
          const index = this.groupArrNew.findIndex(
            (item) => item.value == newkey
          )
          if (index > -1) {
            this.groupArrNew[index].show = false
          }
        }
      }
      const checkedCount = value.length
      this.checkAll = checkedCount === this.groupArrNew.length
      this.isIndeterminate =
        checkedCount > 0 && checkedCount < this.groupArrNew.length
      let valid = false
      console.log(this.contiKeys, this.checkedVal, this.pushform)
      this.checkedVal.map((item) => {
        if (this.contiKeys.includes(item)) {
          if (
            !this.pushform[`${item}-1`] &&
            this.pushform[`${item}-1`] != 0 &&
            !this.pushform[`${item}-2`] &&
            this.pushform[`${item}-2`] != 0
          ) {
            valid = true
          }
        }
      })
      if (valid) {
        Message.warning('业务指标区间值不能为空!')
        return
      }
      this.$emit('onGroupChange', {
        val1: this.checkedVal,
        val2: this.pushform
      })
    },
    handleCheckAllChange(val) {
      console.log(val)
      this.checkedVal = val ? this.groupArrNew?.map((ot) => ot.value) : []
      for (const key in this.pushform) {
        const newkey = key.split('-')[0]
        if (!this.checkedVal.includes(newkey)) {
          this.pushform[key] = undefined
          const index = this.groupArrNew.findIndex(
            (item) => item.value == newkey
          )
          if (index > -1) {
            this.groupArrNew[index].show = false
          }
        }
      }
      this.isIndeterminate = false
      let valid = false
      this.checkedVal.map((item) => {
        if (this.contiKeys.includes(item)) {
          if (
            !this.pushform[`${item}-1`] &&
            this.pushform[`${item}-1`] != 0 &&
            !this.pushform[`${item}-2`] &&
            this.pushform[`${item}-2`] != 0
          ) {
            valid = true
          }
        }
      })
      if (valid) {
        Message.warning('业务指标区间值不能为空!')
        return
      }
      this.$emit('onGroupChange', {
        val1: this.checkedVal,
        val2: this.pushform
      })
    },
    getCurrentValue(e) {
      // console.log(e,e.target.value,key)
      // this.pushform[key] = e.target.value;
      // console.log(this.pushform)
      let valid = false
      console.log(this.checkedVal, this.pushform)
      this.checkedVal.map((item) => {
        if (this.contiKeys.includes(item)) {
          if (
            !this.pushform[`${item}-1`] &&
            this.pushform[`${item}-1`] != 0 &&
            !this.pushform[`${item}-2`] &&
            this.pushform[`${item}-2`] != 0
          ) {
            valid = true
          }
        }
      })
      if (valid) {
        Message.warning('业务指标区间值不能为空!')
        return
      }
      this.$emit('onGroupChange', {
        val1: this.checkedVal,
        val2: this.pushform
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.group-check-cnt {
  display: flex;
  flex-wrap: wrap;
  position: relative;
  > .el-checkbox {
    margin-right: 30px;
  }
  ::v-deep .el-checkbox__label {
    color: #3d3d3d;
    font-size: 16px;
    font-weight: 400;
    padding-left: 6px;
    line-height: 20px;
    position: relative;
  }
  .quesiton {
    display: block;
    font-size: 13px;
    color: #b9b9b9;
    position: absolute;
    right: -14px;
    top: -6px;
  }
  .myCheck {
    ::v-deep .el-checkbox__input {
      position: relative;
      top: -4px;
    }
  }
}
.item-box {
  display: flex;
  align-items: center;
  ::v-deep .el-form-item {
    margin-bottom: 0;
    width: 60px;
  }
  ::v-deep .el-input-number.is-without-controls .el-input__inner {
    width: 60px;
    height: 27px;
    padding: 0 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 12px;
  }
  ::v-deep .el-form-item__content {
    line-height: 27px;
  }
  .range {
    margin: 0 5px;
  }
}
.collapse {
  color: #999;
}
.check-group {
}
</style>
