<template>
  <view class="container">
    <VNavigationBar background-color="#fff" title="配件出库" title-color="#333"></VNavigationBar>
    <view class="search">
      <view class="searchBox">
        <input class="searchInput" placeholder="查询配件名称" placeholder-style="font-size: 28rpx" type="text">
        <text>搜索</text>
      </view>
    </view>
    <view class="tabs">
      <view v-for="(item, index) in tabs" :key="index" :class="{'active': item.value === active}" class="tab-item">
        {{ item.name }}
      </view>
    </view>
    <view class="listBox">
      <view class="list">
        <view v-for="(item, index) in repairList" :key="index" class="listItem">
          <view class="repairName">{{ item.name }}</view>
          <view class="repairBottom">
            <text class="repairDesc">单位：
              <text class="repairUnit">{{ item.unit }}</text>
            </text>
            <view class="repairBtns">
              <u-icon name="minus-circle-fill" size="24" @click="delNum(item)"></u-icon>
              <text class="repairNum">{{ item.num }}</text>
              <u-icon color="#0174F6" name="plus-circle-fill" size="24" @click="addNum(item)"></u-icon>
            </view>
          </view>
        </view>
      </view>
    </view>
    <view class="footer">
      <text class="label">共选择：</text>
      <text class="repairNum">{{ repairCount }}个配件</text>
      <view class="submit" @click="submit">确认出库</view>
    </view>
  </view>
</template>

<script>
import VNavigationBar from "@/components/VNavigationBar.vue";

export default {
  components: {VNavigationBar},
  onLoad() {
    this.init()
  },
  data() {
    return {
      tabs: [
        {name: '全部', value: ''},
        {name: '机油', value: '1'},
        {name: '机油', value: '2'},
        {name: '机油', value: '3'},
        {name: '机油', value: '4'},
        {name: '机油', value: '5'},
      ],
      repairList: [],
      selectedRepairList: [
        {name: '炫驰全合成机油S7 4L/ALL', num: 3, unit: '桶', id: 3}
      ],
      active: ''
    };
  },
  computed: {
    repairCount() {
      return this.selectedRepairList.reduce((val, item) => {
        return item.num + val
      }, 0)
    }
  },
  methods: {
    init() {
      // 接口返回
      const result = [{name: '炫驰全合成机油S7 4L/ALL', num: 0, unit: '桶', id: 1},
        {name: '炫驰全合成机油S7 4L/ALL', num: 0, unit: '桶', id: 2},
        {name: '炫驰全合成机油S7 4L/ALL', num: 0, unit: '桶', id: 3},
        {name: '炫驰全合成机油S7 4L/ALL', num: 0, unit: '桶', id: 4},
        {name: '炫驰全合成机油S7 4L/ALL', num: 0, unit: '桶', id: 5}]
      // 初始化数据
      this.repairList = result.map(m => {
        if (this.selectedRepairList && this.selectedRepairList.length > 0) {
          const find = this.selectedRepairList.find(f => f.id === m.id)
          if (find) {
            m.num = find.num
          }
        }
        return m
      })
    },
    addNum(repair) {
      this.$set(repair, 'num', repair.num + 1)
      const find = this.selectedRepairList.find(f => f.id === repair.id)
      if (find) {
        find.num = repair.num
      } else {
        this.selectedRepairList.push(JSON.parse(JSON.stringify(repair)))
      }
      console.log('repair', repair)
    },
    delNum(repair) {
      if (repair.num <= 0) {
        return
      }
      this.$set(repair, 'num', repair.num - 1)
      const findIndex = this.selectedRepairList.findIndex(f => f.id === repair.id)
      if (findIndex > -1 && repair.num <= 0) {
        this.selectedRepairList.splice(findIndex, 1)
      } else if (repair.num > 0) {
        this.$set(this.selectedRepairList[findIndex], 'num', repair.num)
      }
    },
    submit() {
      console.log('this.selectedRepairList', this.selectedRepairList)
    }
  }
}
</script>

<style lang="scss">
.container {
  height: 100%;
  background-color: #F3F5F7;
  display: flex;
  flex-direction: column;
}

.search {
  padding: 0 40rpx;
  background-color: #fff;

  & > .searchBox {
    height: 84rpx;
    background: #F3F5F7;
    border-radius: 12rpx 12rpx 12rpx 12rpx;
    margin: 0 auto;
    padding: 0 30rpx;
    font-size: 28rpx;
    color: #0174F6;

    display: flex;
    align-items: center;
  }

  .searchInput {
    flex: 1;
    width: 0;
    color: #333;
  }
}

.tabs {
  background-color: #fff;
  padding: 30rpx 40rpx;
  margin: 0 auto;
  display: flex;
  align-items: center;
  column-gap: 30rpx;
  overflow: auto;

  .tab-item {
    flex-shrink: 0;
    padding: 16rpx 30rpx;
    font-size: 28rpx;
    color: #113A68;
    background: #F2F2F7;
    border-radius: 30rpx 30rpx 30rpx 30rpx;

    &.active {
      background: #0174F6;
      color: #fff;
    }

  }
}

.listBox {
  padding: 30rpx 32rpx;
  flex: 1;
  height: 0;

  .list {
    background-color: #fff;
    padding: 0 30rpx;
    height: 100%;
    overflow: auto;
  }

  .listItem {
    padding: 30rpx 0;
    border-bottom: 2rpx solid #DDDDDD;
    &:last-child {
      border-bottom: none;
    }
    .repairName {
      font-size: 32rpx;
      color: #333333;
      margin-bottom: 20rpx;
    }

    .repairBottom {
      display: flex;
      align-items: center;
      justify-content: space-between;
    }

    .repairDesc {
      font-size: 28rpx;
      color: #858BA0;
    }

    .repairUnit {
      color: #333333;
    }

    .repairBtns {
      display: flex;
      align-items: center;
      column-gap: 10rpx;
    }
  }
}

.footer {
  padding: 14rpx 32rpx;
  background-color: #fff;
  display: flex;
  align-items: center;

  .repairNum {
    flex: 1;
    width: 0;
    margin-right: 10rpx;
  }

  .submit {
    width: 208rpx;
    height: 72rpx;
    background: #0174F6;
    border-radius: 38rpx 38rpx 38rpx 38rpx;
    text-align: center;
    line-height: 72rpx;
    font-size: 32rpx;
    color: #FFFFFF;
  }
}
</style>
