<template>
<div class="area-list">
  <template v-if="list && list.length > 0">
    <div v-for="(item, index) in list" :key="item.name">
      <div :class="`area-card ${index === curIndex ? 'on' : ''}`" @click="handleSelect(index)">
        <i-circle :trail-color="switchColor(item.value).trail" :stroke-color="switchColor(item.value).stroke" :percent="item.value" :size="size">
          <span style="font-size:0.8rem">{{item.value | pipe}}</span>
        </i-circle>
        <div>
          <p>{{item.name}}</p>
          <p v-if="item.output"><b><m-count :value="item.output"/></b><i>万元</i></p>
          <p v-if="item.area"><b><m-count :value="item.area" :decimal="0"/></b><i>公顷</i></p>
          <p v-if="item.water || item.farm"><b><m-count :value="item.water" :decimal="0"/></b> / <b><m-count :value="item.farm" :decimal="0"/></b><i>吨</i></p>
        </div>
      </div>
    </div>
  </template>
</div>
</template>

<script>
export default {
  name: 'AreaCard',
  props: {
    list: {
      type: Array,
      default() {
        return []
      }
    }
  },
  data() {
    return {
      curIndex: null,
    }
  },
  methods: {
    handleSelect(i) {
      if (this.curIndex === i) {
        this.curIndex = null
      } else {
        this.curIndex = i
      }
      this.$emit('select', this.curIndex)
    },
    switchColor(val) {
      if (val <= 20) {
        return {
          stroke: '#DBF6FF',
          trail: '#85CDFF',
        }
      } else if (val <= 35) {
        return {
          stroke: '#5BD5FF',
          trail: '#0076FF',
        }
      } else if (val <= 50) {
        return {
          stroke: '#8ED617',
          trail: '#3F772F',
        }
      } else if (val <= 65) {
        return {
          stroke: '#FFCE34',
          trail: '#E06536',
        }
      } else if (val <= 80) {
        return {
          stroke: '#FF6161',
          trail: '#A02929',
        }
      } else if (val <= 100) {
        return {
          stroke: '#8623EB',
          trail: '#C691FF',
        }
      } else {
        return {
          stroke: '#8623EB',
          trail: '#C691FF',
        }
      }
    }
  },
  filters: {
    pipe(val) {
      const result = val > 100 ? 100 : val
      return `${result}%`
    }
  },
  computed: {
    size() {
      return Math.floor(window.innerWidth / 100) * 2.5
    },
  },
}
</script>

<style lang="stylus" scoped>
.area-list
  width 100%
  display flex
  flex-wrap wrap
  padding 0.5rem 0.5rem 0
  >div
    width 12.5%
  .area-card
    display flex
    align-items center
    width 100%
    padding 0.5rem 0
    cursor pointer
    border 0.1rem solid transparent
    &.on
      border-color $color-map(0.5)
      box-shadow 0 0 1rem 0.1rem $color-map(1)
    >div
      color #ccc
      margin-left 0.5rem
      p
        line-height 1
        font-size 0.9rem
      span
        color $color-map(0.8)
      i
        font-size 0.8rem
</style>
