<template>
  <div class="radioGroup">
    <template v-if="clueOptionList && clueOptionList.length">
      <template v-if="clueOptionList[0].answerType == 'dx'">
        <van-radio-group v-model="clueOptionList[0].searchValue" @change="radioGroupChange">
          <template v-for="(item, index) in clueOptionList[0].customerClueOptionList">
            <van-radio :name="item.customerClueOptionId" @click="radioClick" :key="index">
              {{ item.customerClueOption }}
            </van-radio>
            <radioGroup
              :clueOptionList="item.customerClueItemList"
              :parentOptionList="clueOptionList[0]"
              :parentId="item.customerClueOptionId"
              v-if="clueOptionList[0].searchValue == item.customerClueOptionId"></radioGroup>
          </template>
        </van-radio-group>
      </template>
      <template v-for="(item, index) in clueOptionList" v-if="clueOptionList.length">
        <template
          v-if="parentOptionList && parentOptionList.searchValue == item.itemOptionParentId">
          <template v-if="item.answerType == 'wb' || item.answerType == 'sz'">
            <span class="van-f-red" v-if="item.isMust == 0">*</span>
            {{ item.customerClueName }}
          </template>
          <!-- 回答类型:wb-文本，sz-数字，rq-日期，zp-照片，dx -->
          <template v-if="item.answerType == 'wb'">
            <van-field
              v-model="item.answerValue"
              autosize
              type="textarea"
              :placeholder="item.remark" />
          </template>
          <template v-if="item.answerType == 'sz'">
            <van-field
              v-model="item.answerValue"
              :placeholder="item.remark"
              type="number"></van-field>
          </template>
        </template>
      </template>
    </template>
  </div>
</template>
<script>
export default {
  name: 'radioGroup',
  props: {
    clueOptionList: {
      type: Array,
      default: () => [],
    },
    parentOptionList: {
      type: Object,
      default: () => {},
    },
    parentId: {
      type: [Number, null],
      default: null,
    },
  },
  data() {
    return {};
  },
  created() {
    // console.log(this.clueOptionList);
  },
  methods: {
    radioGroupChange(name) {
      if (!name) return;
      // console.log(name);
      // console.log(this.clueOptionList);
      // 如果选中的数据有父级，将父级数据修改为选中状态
      if (this.parentOptionList && this.parentId) {
        this.$set(this.parentOptionList, 'searchValue', this.parentId);
      }
      // 获取选中数据
      let activaRadio = this.clueOptionList[0].customerClueOptionList.find(
        (val) => val.customerClueOptionId == name
      );
      // 修改选中状态
      this.$set(activaRadio, 'value', 'Y');

      // 过滤未选中的数据
      let exceptItself = this.clueOptionList[0].customerClueOptionList.filter(
        (val) => val.customerClueOptionId !== name
      );
      // 删除未选中数据状态兄弟级和子级
      // console.log(exceptItself);
      this.toggleOtheChildren(exceptItself);
    },
    toggleOtheChildren(exceptItself) {
      if (!exceptItself) return;
      exceptItself.forEach((val) => {
        this.$set(val, 'value', 'N');
        if (val.customerClueItemList && val.customerClueItemList[0]) {
          this.$set(val.customerClueItemList[0], 'searchValue', null);
          if (
            val.customerClueItemList[0] &&
            val.customerClueItemList[0].customerClueOptionList.length
          ) {
            this.toggleOtheChildren(val.customerClueItemList[0].customerClueOptionList);
          }
        }
      });
    },
    radioClick(event) {
      // console.log(event);
    },
  },
};
</script>
<style lang="scss">
.radioGroup {
  font-size: 15px;
  .van-radio {
    padding: 5px 0;
  }
  .van-radio-group {
    padding-left: 20px;
  }
  .van-cell {
    padding: 10px 0;
    /* font-size: 14px; */
  }
  .van-cell::after {
    border: 0;
  }
  .van-field {
    border: 1px solid #cdc8c8;
    padding: 0;
    width: 97%;
    border-radius: 4px;
    overflow: hidden;
    background-color: unset;
    // height: 30px;
    // line-height: 30px;
    margin: 5px 0;
  }
  .van-field__control {
    padding: 0 10px;
  }
  .van-field__control {
    padding: 0 10px;
  }
  .van-radio__label {
    /* font-size: 14px; */
  }
}
</style>
