<template>
  <!-- #ifdef H5 -->
  <zj-page-layout :hasFooter="true">
    <template slot="header">
      <view class="search-container">
        <u-search
          shape="round"
          :showAction="false"
          placeholder="输入分类名称进行搜索"
          v-model="keyword"
          @search="searchData"
          @clear="searchData"
        >
        </u-search>
      </view>
    </template>

    <view class="main-container">
      <view class="left-list" v-if="type == 'M'">
        <block v-for="(item, index) in leftList" :key="index">
          <view
            class="item ellipsis"
            :class="leftCurrent == item.categoryId ? 'current' : ''"
            @tap="changeLeft(item.categoryId)"
            >{{ item.categoryName }}</view
          >
        </block>
      </view>
      <view class="right-list">
        <scroll-view scroll-y style="height: 100%">
          <view class="item" v-for="(item, index) in rightList" :key="index">
            <view class="name">{{ item.goodsName }}</view>
            <view class="text">代码：{{ item.goodsCode }}</view>
            <view class="bottom">
              <view class="price"
                >¥{{ item.price | priceFilter }}<text>/{{ item.goodsSalesUnit }}</text></view
              >
              <u-number-box
                min="0"
                :step="item.goodsSalesUnit == '米' ? 1 : 1"
                :name="item.goodsId"
                v-model="item.num"
                @change="changeNum"
              >
                <view slot="minus" class="ctrl">
                  <u-icon name="minus" color="#FFFFFF" size="12"></u-icon>
                </view>
                <text slot="input" class="input">
                  <u--input
                    :type="item.goodsSalesUnit == '米' ? 'digit' : 'number'"
                    placeholder="0"
                    inputAlign="center"
                    border="none"
                    v-model="item.num"
                    @blur="changeNum({ name: item.goodsId, value: item.num, unit: item.goodsSalesUnit })"
                  ></u--input>
                </text>
                <view slot="plus" class="ctrl">
                  <u-icon name="plus" color="#FFFFFF" size="12"></u-icon>
                </view>
              </u-number-box>
            </view>
          </view>
        </scroll-view>
        <Loading :type="2" :loadStatus="loadStatus" :dataList="rightList" />
      </view>
    </view>

    <u-transition class="dialog-mark" mode="fade" :show="isShowDialog" @tap="isShowDialog = false"></u-transition>
    <u-transition class="dialog-container" mode="slide-up" :show="isShowDialog">
      <view class="top">
        <view class="left">购物车</view>
        <view class="right" @tap="clearCart"><text class="iconfont icon-shanchu"></text>清空购物车</view>
      </view>
      <view class="list">
        <view class="item" v-for="(item, index) in cartList" :key="index">
          <view class="name">{{ item.goodsName }}</view>
          <view class="bottom">
            <view class="price"
              >¥{{ item.price | priceFilter }}<text>/{{ item.goodsSalesUnit }}</text></view
            >
            <u-number-box
              :step="item.goodsSalesUnit == '米' ? 1 : 1"
              :min="item.goodsSalesUnit == '米' ? 0.1 : 1"
              :name="item.goodsId"
              v-model="item.num"
              @change="changeCartNum"
              @overlimit="overlimitFun(index)"
            >
              <view slot="minus" class="ctrl">
                <u-icon name="minus" color="#FFFFFF" size="12"></u-icon>
              </view>
              <text slot="input" class="input">
                <u--input
                  :type="item.goodsSalesUnit == '米' ? 'digit' : 'number'"
                  placeholder="0"
                  inputAlign="center"
                  border="none"
                  v-model="item.num"
                  @blur="changeCartNum({ name: item.goodsId, value: item.num, unit: item.goodsSalesUnit })"
                ></u--input>
              </text>
              <view slot="plus" class="ctrl">
                <u-icon name="plus" color="#FFFFFF" size="12"></u-icon>
              </view>
            </u-number-box>
          </view>
        </view>
      </view>
    </u-transition>

    <template slot="footer">
      <view class="bottom-container">
        <view class="cart" @tap="isShowDialog = !isShowDialog">
          <text class="iconfont icon-gouwuche1"></text>
          <u-badge max="99" :value="totalNum" :offset="[1, 1]" absolute></u-badge>
        </view>
        <view class="price"><text>合计：</text>¥{{ totalPrice | priceFilter }}</view>
        <view class="btn">
          <u-button type="primary" text="提交" @click="submitData"></u-button>
        </view>
      </view>
    </template>
  </zj-page-layout>
  <!-- #endif -->

  <!-- #ifndef H5 -->
  <web-view
    :src="webViewHref(`/packageMaterial/pages/apply/product`, pam, crossPagePam)"
    @message="crossPage.$listener"
  ></web-view>
  <!-- #endif -->
</template>

<script>
// #ifdef H5
export default {
  data() {
    return {
      type: 'M',
      applyName: '',
      websitId: null,
      websitName: null,

      keyword: '',
		leftList: [],
		leftCurrent: 0,
		rightList: [],
      loadStatus: 0,
      isShowDialog: false,

      cartList: [],
    }
  },

  computed: {
    totalNum() {
      // let val = 0;
      // this.cartList.forEach(item => {
      //   val = val + item.num;
      // })
      return this.cartList.length;
    },
    totalPrice() {
      let val = 0;
      this.cartList.forEach(item => {
        val = val + (item.num * (item.price * 100) / 100);
      })
      return val;
    },
  },

  onLoad({type, applyName, websitId, websitName}) {
    this.type = type;
    this.applyName = applyName;
    this.websitId = websitId;
    this.websitName = websitName;

    uni.setNavigationBarTitle({
    　title: type == 'M' ? '选择辅材' : '选择配件'
    })
    if(type === 'M') {
      this.getLeftList();
    }else {
      this.getRightList();
    }
  },

  methods: {
    // 获取左侧分类
    getLeftList() {
    	this.$api.post('/material/salses/exist/stock/category', {
        websitId: this.websitId,
      }).then(res => {
    		this.leftList = res.data;
    		this.leftCurrent = res.data.length > 0 ? res.data[0].categoryId : 0;
    		this.getRightList();
    	})
    },

    // 获取右侧产品
    getRightList() {
    	this.loadStatus = 1;
      let url = '', params = {};
    	this.$api.post('/material/salses/exist/stock/goods', {
        websitId: this.websitId,
        goodsName: this.keyword,
        type: this.type,
        categoryId: this.type === 'M' ? this.leftCurrent : '',
    	}).then(res => {
    		this.loadStatus = 0;
        let list = res.data.map(item => {
          return {
            ...item,
            num: 0,
          }
        });
        for(let i = 0; i < list.length; i++) {
          for(let j = 0; j < this.cartList.length; j++) {
            if(list[i].goodsId == this.cartList[j].goodsId) {
              list[i].num = this.cartList[j].num;
            }
          }
        }
    		if(list.length < 1){
    			this.loadStatus = 2;
    		}
    		this.rightList = list;
    	}).catch(() => {
    		this.loadStatus = 2;
    	})
    },

    // 切换一级菜单
    changeLeft(pid) {
    	this.leftCurrent = pid;
    	this.getRightList();
    },

    // 搜索
    searchData() {
      this.getRightList();
    },

    changeNum(e) {
      let obj = this.rightList.find((item) => item.goodsId === e.name);
      if(e.unit && e.unit != '米') {
        e.value = Math.floor(e.value);
        obj.num = Math.floor(obj.num);
      }
      // 判断对象数组中是否存在该对象
      let index = this.cartList.findIndex((item) => item.goodsId === obj.goodsId);
      // 如果是0，就删除
      if(index !== -1 && e.value == 0) {
        this.cartList.splice(index, 1);
      }
      // 如果有就替换，没有就添加
      else if (index !== -1) {
      	this.cartList.splice(index, 1, obj);
      }
      else {
        if(e.value > 0) {
          this.cartList.push(obj);
        }
      }
    },

    changeCartNum(e) {
      let obj = this.cartList.find((item) => item.goodsId === e.name);
      if(e.unit && e.unit != '米') {
        e.value = Math.floor(e.value);
        obj.num = Math.floor(obj.num);
      }
      let index = this.cartList.findIndex((item) => item.goodsId === obj.goodsId);
      this.cartList.splice(index, 1, obj);

      this.getRightList();
    },

    overlimitFun(index) {
      this.$modal({
        content: '把该产品从购物车移除？'
      }).then(() => {
        this.cartList.splice(index, 1);
        this.getRightList();
      }).catch(() => {})
    },

    clearCart() {
      this.$modal({
        content: '确定清空购物车？'
      }).then(() => {
        this.cartList = [];
        this.getRightList();
      }).catch(() => {})
    },

    submitData() {
      if(this.cartList.length < 1) return this.$toast('请选择');
      this.$setStorage('materialApplyData', {
        type: this.type,
        applyName: this.applyName,
        websitId: this.websitId,
        websitName: this.websitName,
        goodsList: this.cartList
      });
      this.$navToPage({
        url: `/packageMaterial/pages/apply/order`
      })
    },

  },
}

// #endif
// #ifndef H5
export default {
  data() {
    return {
      pam: {},
    }
  },
  onLoad(pam) {
    this.pam = pam;
  }
}
// #endif
</script>

<style lang="scss" scoped>
.search-container {
  background: #ffffff;
  padding: 0 20rpx 20rpx;
  ::v-deep .u-search {
    height: 60rpx;
    input {
      background: #f7f8ff !important;
    }
    .u-search__content {
      background: #f7f8ff !important;
      height: 60rpx;
    }
  }
}
.main-container {
  display: flex;
  height: 100%;
  .left-list {
    width: 220rpx;
    height: 100%;
    overflow-y: scroll;
    box-sizing: border-box;
    flex-shrink: 0;
    .item {
      font-size: 28rpx;
      color: #333333;
      height: 88rpx;
      display: flex;
      align-items: center;
      padding: 0 20rpx;
      &.current {
        color: #ffffff;
        background: $theme-color;
      }
    }
  }
  .right-list {
    flex: 1;
    background: #ffffff;
    background: #ffffff;
    height: 100%;
    box-sizing: border-box;
    .item {
      padding: 20rpx;
      border-bottom: 1px solid #f5f5f5;
      .name {
        font-size: 32rpx;
        font-weight: 500;
      }
      .text {
        font-size: 28rpx;
        color: $sec-font;
        margin-top: 12rpx;
      }
      .bottom {
        display: flex;
        align-items: center;
        justify-content: space-between;
        margin-top: 12rpx;
        .price {
          font-size: 28rpx;
          font-weight: 500;
          color: $minor-color;
          text {
            font-weight: normal;
            color: $sec-font;
            margin-left: 8rpx;
          }
        }
        .input {
          width: 50rpx;
          text-align: center;
          padding: 0 10px;
        }
        .ctrl {
          width: 40rpx;
          height: 40rpx;
          background-color: $theme-color;
          border-radius: 50%;
          display: flex;
          justify-content: center;
          align-items: center;
        }
      }
    }
  }
}
.bottom-container {
  height: 100rpx;
  display: flex;
  align-items: center;
  padding: 0 20rpx;
  .cart {
    flex-shrink: 0;
    width: 100rpx;
    height: 100rpx;
    border-radius: 50%;
    background: $theme-color;
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-top: -40rpx;
    .iconfont {
      font-size: 48rpx;
      color: #ffffff;
      font-weight: 600;
    }
  }
  .price {
    margin-left: 20rpx;
    flex: 1;
    font-size: 32rpx;
    color: $minor-color;
    font-weight: 500;
    text {
      font-weight: normal;
      color: $sec-font;
    }
  }
  .btn {
    width: 180rpx;
    ::v-deep .u-button {
      height: 60rpx;
    }
  }
}
.dialog-mark {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba($color: #000000, $alpha: 0.6);
}
.dialog-container {
  width: 100%;
  box-sizing: border-box;
  background: #ffffff;
  position: fixed;
  bottom: 100rpx;
  left: 0;
  padding: 30rpx;
  .top {
    display: flex;
    align-items: center;
    justify-content: space-between;
    .left {
      font-size: 32rpx;
      font-weight: 500;
    }
    .right {
      display: flex;
      align-items: center;
      font-size: 28rpx;
      color: $sec-font;
      text {
        font-size: 32rpx;
        margin-right: 8rpx;
      }
    }
  }
  .list {
    max-height: 400rpx;
    overflow-y: scroll;
    margin-top: 20rpx;
    .item {
      padding: 20rpx 0;
      border-bottom: 1px solid #f5f5f5;
      &:last-child {
        border: none;
      }
      .name {
        font-size: 32rpx;
        font-weight: 500;
      }
      .text {
        font-size: 28rpx;
        color: $sec-font;
        margin-top: 12rpx;
      }
      .bottom {
        display: flex;
        align-items: center;
        justify-content: space-between;
        margin-top: 12rpx;
        .price {
          font-size: 28rpx;
          font-weight: 500;
          color: $minor-color;
          text {
            font-weight: normal;
            color: $sec-font;
            margin-left: 8rpx;
          }
        }
        .input {
          width: 50rpx;
          text-align: center;
          padding: 0 10px;
        }
        .ctrl {
          width: 40rpx;
          height: 40rpx;
          background-color: $theme-color;
          border-radius: 50%;
          display: flex;
          justify-content: center;
          align-items: center;
        }
      }
    }
  }
}
</style>
