<template>
  <div class="Dialog" v-show="show">
    <div class="DialogTit">
      <div class="back" @click="hide()"><span class="iconfont" >&#xe720; </span>返回</div>
      <h1 class="name">{{ title }}</h1>
      <!-- <span class="success" @click="handle_selected()">完成</span> -->
      <span  v-if="type == 1" class="success">  </span>
      <span  v-if="type == 2" @click="handle_selected" class="success">确定</span>
    </div>

    <div class="optionsBox" v-show="type == 1">
      <div class="list">
        <ul>
          <li v-for="(item, i) in single.options" 
          :key="i" 
          :value="single.id_key.lenght == 0 ? item : item[single.id_key]" 
          :class="single.cur_id == (single.id_key.lenght == 0 ? item : item[single.id_key]) ? 'tick':'' "
          @click="single_click(single.id_key.lenght == 0 ? item : item[single.id_key])">
            {{ single.show_key.lenght == 0 ? item : item[single.show_key] }}
          </li>
        </ul>
      </div>
    </div>

    <div class="CheckBox" v-show="type == 2">
      <ul>
        <li>
          <van-checkbox-group v-model="mutable.selected_values"  >
            <van-checkbox style="border-bottom: 1px #e5e5e5 solid;padding: 0 0.38rem;width:100%;" v-for="(item, index) in mutable.options" :key="index" :name="mutable.id_key.lenght == 0 ? item : item[mutable.id_key]">
              {{ mutable.show_key.lenght == 0 ? item : item[mutable.show_key] }}
            </van-checkbox>
          </van-checkbox-group>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
export default {
  name: "TwoMenu",
  data() {
    return {
      // list: ["复选框1", "复选框2"],
      // result: ["复选框1"],

      show: false,
      type: 1, // 1.单选 2.复选
      handle_select: null, // function
      handle_hide: null, // function

      title: "",
      single: {
        options: [],
        cur_id: 0,
        show_key: "",
        id_key: "",
      },

      mutable: {
        options: [],
        selected_values: [],
        show_key: "",
        id_key: "",
      }
    };
  },
  methods: {
    showSingleSelect(list_options, current_select_id, title, show_key, id_key, handle_select, handle_hide) {
      this.show = true
      this.type = 1
      this.title = title
      this.single.options = list_options
      this.single.cur_id = current_select_id
      this.single.show_key = show_key
      this.single.id_key = id_key
      this.handle_select = handle_select
      this.handle_hide = handle_hide
    },
    // showMutableSelect(list_options, selected_values, title, show_key, id_key, handle_select) {
    //   this.show = true
    //   this.type = 2
    //   this.title = title
    //   this.mutable.options = list_options
    //   this.mutable.selected_values = selected_values
    //   this.mutable.show_key = show_key
    //   this.mutable.id_key = id_key
    //   this.handle_select = handle_select
    // },
    hide() {
      if (this.handle_hide != null && this.handle_hide != undefined) {
        this.handle_hide()
      }
      this.show = false
    },
    single_click: function(id) {
      this.single.cur_id = id
      this.finish_selected()
      this.hide()
    },
    finish_selected: function () {
      if (this.handle_select != null && this.handle_select != undefined) {
        this.handle_select(this.single.cur_id)
      }
    }
  }
};
</script>

<style style="stylesheet/scss" lang="scss" scoped>
.van-checkbox__icon--checked .van-icon{
  color: #fff;
    border-color:$main-color !important;
    background-color: $main-color !important;
}
.optionsBox {
  background: #fff;
  max-height: 10.6rem;
  min-height: 5rem;
  overflow-y: scroll;
  ul {
    li {
      height: 1rem;
      line-height: 1rem;
      border-bottom: 1px #e5e5e5 solid;
      padding: 0 0.38rem;
    }
    .tick {
      position: relative;
      &::before {
        content: "";
        display: inline-block;
        border: 2px solid $main-color;
        border-top-width: 0;
        border-right-width: 0;
        width: 0.3rem;
        height: 0.15rem;
        -webkit-transform: rotate(-50deg);
        position: absolute;
        top: 0.38rem;
        right: 0.44rem;
      }
    }
  }
}
.CheckBox {
  background: #fff;
  max-height: 10.6rem;
  min-height: 5rem;
  overflow-y: scroll; 
  ul {
    li {
      line-height: 1rem;
    }
  }
}
</style>

