<template>
  <div
    @click="toSet"
    class="components-product-item"
    :class="{
      'pitem-full': data.cargoCapacity == data.qty,
      'pitem-close': data.cargoState != 1,
      'pitem-fault': data.faultState == 1,
      'pitem-active': isActive,
      'pitem-main': data.state == 21
    }"
  >
    <div :style="'height: ' + rate + '%'" class="pitem-bg"></div>
    <div class="pitem-inner">
      <div class="kitbox ">
        <div class="box-flex-1">{{ wayerNo }}号位</div>
        <div>{{ data.qty }}/{{ data.cargoCapacity }}</div>
      </div>
      <template v-if="data.shelfStatus == 1">
        <div class="ellipsis tc pt12">{{ data.drugName }}</div>
        <div class="ellipsis tc pt10">{{ data.specif }}</div>
      </template>
      <div v-else class="ellipsis tc pt12 ">未设药品</div>
    </div>
    <div class="tag" v-if="data.state == 21">主</div>
    <div class="tag" v-else-if="data.state == 20">副</div>
    <img class="gouxuan" src="@/assets/images/gouxuan.png" alt="" />
  </div>
</template>
<script>
export default {
  props: ['data', 'index', 'bussuness', 'isView', 'old'], //bussuness quick快速补货  isView
  data() {
    return {
      isActive: false
    }
  },
  computed: {
    wayerNo() {
      if (this.data.state == 21) {
        var arr = this.data.merge.split(',')
        return arr[0] + '~' + arr[arr.length - 1]
      }
      return this.data.wayerNo
    },
    rate() {
      var { data } = this
      return ((data.qty || 0) / (data.cargoCapacity || 5)) * 100
    }
  },
  watch: {
    'data.isActive': {
      handler(n) {
        this.isActive = n
      },
      deep: true
    }
  },
  created() {
    this.isActive = this.data.isActive
  },
  methods: {
    toSet() {
      if (this.isView) {
        return
      }
      if (this.bussuness == 'quick') {
        this.isActive = !this.isActive
        this.$emit('onChange', this.isActive)
        return
      }
      if (this.data.state == 20) {
        this.$toast('不能操作副通道，如要修改请修改它的主通道')
        return
      }
      if (this.data.shelfStatus != 1) {
        if (this.old) {
          this.gotoUrl('/oldDrugList', {
            trackId: this.data.id,
            deviceId: this.data.idDevice,
            cargoCapacity: this.data.cargoCapacity
          })
        } else {
          this.gotoUrl('/drugList', {
            trackId: this.data.id,
            deviceId: this.data.idDevice,
            cargoCapacity: this.data.cargoCapacity
          })
        }
      } else {
        this.gotoUrl('./productDetail', {
          id: this.data.id
        })
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.components-product-item {
  overflow: hidden;
  width: 100px;
  height: 80px;
  background: rgb(238, 238, 238);
  border-radius: 10px;
  margin-left: 10px;
  padding: 5px;
  font-size: 14px;
  position: relative;
  &.pitem-full {
    color: #fff;
  }
  &:first-child {
    margin-left: 0;
  }
  .pitem-inner {
    position: relative;
    z-index: 1;
    background: transparent;
    line-height: 1.2;
  }
  .pitem-bg {
    background: #17b3ec;
    position: absolute;
    z-index: 0;
    width: 100%;
    bottom: 0;
    left: 0;
    border-bottom-left-radius: 10px;
    border-bottom-right-radius: 10px;
    &.orange {
      background: #ff9526;
    }
    &.red {
      background: #ff5e6c;
    }
  }
  .tag {
    color: red;
    position: absolute;
    right: 2px;
    bottom: 2px;
    font-size: 12px;
  }
  &.pitem-close {
    background-color: #ff9526;
    color: #fff;
    .pitem-bg {
      display: none;
    }
  }
  &.pitem-fault {
    background-color: #ff5e6c;
    color: #fff;
    .pitem-bg {
      display: none;
    }
  }
  .gouxuan {
    position: absolute;
    right: 0;
    bottom: 0;
    width: 20px;
    display: none;
  }
  &.pitem-active {
    border: 1px solid red;
    .gouxuan {
      display: block;
    }
  }
  &.pitem-main {
    width: 280px !important;
  }
}
</style>
