<template>
  <div @change="$emit('change', currentValue)" class="co-checklist" :class="{ 'is-limit': max <= currentValue.length }">
    <label class="co-checklist-title" v-text="title"></label>
    <x-cell v-for="(option, i) in options" :key="i">
      <label class="co-checklist-label" slot="title">
        <span
          :class="{'is-right': align === 'right'}"
          class="co-checkbox">
          <input
            class="co-checkbox-input"
            type="checkbox"
            v-model="currentValue"
            :disabled="option.disabled"
            :value="option.value || option">
          <span class="co-checkbox-core"></span>
        </span>
        <span class="co-checkbox-label" v-text="option.label || option"></span>
      </label>
    </x-cell>
  </div>
</template>

<script>
import XCell from 'CoUI/packages/cell/index.js';
if (process.env.NODE_ENV === 'component') {
  require('CoUI/packages/cell/style.css');
}

/**
 * co-checklist
 * @module components/checklist
 * @desc 复选框列表，依赖 cell 组件
 *
 * @param {(string[]|object[])} options - 选项数组，可以传入 [{label: 'label', value: 'value', disabled: true}] 或者 ['ab', 'cd', 'ef']
 * @param {string[]} value - 选中值的数组
 * @param {string} title - 标题
 * @param {number} [max] - 最多可选的个数
 * @param {string} [align=left] - checkbox 对齐位置，`left`, `right`
 *
 *
 * @example
 * <co-checklist :v-model="value" :options="['a', 'b', 'c']"></co-checklist>
 */
export default {
  name: 'co-checklist',

  props: {
    max: Number,
    title: String,
    align: String,
    options: {
      type: Array,
      required: true
    },
    value: Array
  },

  components: { XCell },

  data() {
    return {
      currentValue: this.value
    };
  },

  computed: {
    limit() {
      return this.max < this.currentValue.length;
    }
  },

  watch: {
    value(val) {
      this.currentValue = val;
    },

    currentValue(val) {
      if (this.limit) val.pop();
      this.$emit('input', val);
    }
  }
};
</script>

<style lang="css">
  @import "../../../src/style/var-ios9.css";

  @component-namespace co {
    @component checklist {

      .co-cell {
        padding: 0;
      }

      @descendent label {
        display: block;
        padding: 0 0.5rem;
      }

      @descendent title {
        color: $checklist-title-color;
        display: block;
        font-size: 0.6rem;
        margin: 0.4rem;
      }

      @when limit {
        .co-checkbox-core:not(:checked) {
          background-color: $color-grey;
          border-color: $color-grey;
        }
      }
    }

    @component checkbox {
      @when right {
        float: right;
      }

      @descendent label {
        vertical-align: middle;
        margin-left: 0.3rem;
      }

      @descendent input {
        display: none;

        &:checked {
          + .co-checkbox-core {
            background-color: $color-blue;
            border-color: $color-blue;

            &::after {
              border-color: $color-white;
              transform: rotate(45deg) scale(1);
            }
          }
        }

        &[disabled] + .co-checkbox-core {
          background-color: $color-grey;
          border-color: #ccc;
        }
      }

      @descendent core {
        display: inline-block;
        background-color: $color-white;
        border-radius: 100%;
        border: 0.05rem solid #ccc;
        position: relative;
        size: 1rem;
        vertical-align: middle;

        &::after {
          border: 2px solid transparent;
          border-left: 0;
          border-top: 0;
          content: " ";
          position: absolute 0.15rem * * 0.3rem;
          size: 0.2rem 0.4rem;
          transform: rotate(45deg) scale(0);
          transition: transform .2s;
        }
      }
    }
  }
</style>
