<template>
  <div class="zSelect">
    <div>
      <div class="checkbox">
        <van-checkbox-group v-model="zSelectValuec" @change="checkboxclick">
          <van-checkbox
            :disabled="disabled"
            :name="item[id] + ''"
            v-for="(item, index) in zCheckboxcolumns"
            :key="index"
            shape="square"
            >{{ item.customOption }}
          </van-checkbox>
        </van-checkbox-group>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'zSelect',
  props: {
    zCheckboxcolumns: [],
    textc: '',
    rules: false,
    answerType: '',
    collectionType: '',
    disabled: false,
    id: '',
  },
  data() {
    return {
      zSelect: false,
      zSelectValuec: [],
    };
  },
  watch: {
    zCheckboxcolumns: {
      // 返显
      handler(val) {
        this.zSelectValuec = [];
        for (let i = 0; i < val.length; i++) {
          if (val[i].answerValue == 'Y') {
            this.zSelectValuec.push(String(val[i][this.id]));
          }
        }
      },
      immediate: true,
    },
  },
  activated() {
    // this.checkboxvalFn();
  },
  methods: {
    checkboxvalFn() {
      if (this.checkboxval == '' || this.checkboxval == undefined) {
        this.zSelectValuec = [];
      } else {
        this.zSelectValuec = this.checkboxval.split(',');
      }
    },
    checkboxclick(value) {
      var typeval = [...this.zCheckboxcolumns];
      typeval.forEach((item) => {
        if (value.includes(item[this.id] + '')) {
          item.value = 'Y';
        } else {
          item.value = 'N';
        }
      });
      let datalist = {
        id: this.textc,
        value: typeval,
      };
      this.zSelectValuec = value;
      this.$emit('zSelectVal', datalist);
      this.zSelect = false;
    },
  },
};
</script>

<style scoped>
.van-f-red {
  color: red;
  width: 4px;
  display: inline-block;
}

.zSelect .z-cell {
  padding: 10px 16px 0 16px;
  font-size: 16px;
}

/*.checkbox{padding: 10px 16px 0 16px;background-color: white;}*/
.checkbox .van-checkbox {
  padding-bottom: 10px;
}
</style>
<style lang="scss">
.zSelect {
  .checkbox .van-checkbox__icon .van-icon {
    border: 1px solid #333 !important;
  }

  .checkbox .van-checkbox__icon--checked .van-icon {
    border: 1px solid #1989fa !important;
    color: #1989fa;
    background-color: #fff;
  }
}
</style>
