<template><page-meta :root-font-size="fontSize+'px'"></page-meta>
<seller-base :show="false"><view style="position: absolute;top:0;right:0;left:0;bottom:0" class="scroll-view-wrapper div container">
    <view class="div common-header-wrap">
        <view class="status-holder"></view>
        <view :style="'height:'+navHeight+'px'"></view>
        <view class="common-header-holder"></view>
        <view class="common-header-fixed">
        <title-header />
        <uni-nav-bar title="商品管理" class="common-header" left-icon="back" @clickLeft="goBack()">
        </uni-nav-bar>
    </view>
    </view>

    <!-- header -->
    <view class="div order-header">
        <view class="div seller-search">
            <view class="div common-search">
                <view class="div field-line-right">
                  <input class="field-input" v-model="keyword" placeholder="请输入您要搜索的商品" @confirm="getGoodsList()" />
                </view>
            </view>
        </view>
        <view class="ul">
            <view
                    class="li item"
                    v-for="item in orderNav"
                    v-bind:key="item.id"
                    v-bind:class="{ active: goods_type == item.id }"
                    v-on:click="setOrderNavActive(item.id)"
            >
                {{ item.name }}
            </view>
        </view>
    </view>
<view class="scroll-view div pb-30" style="position:relative">
    <scroll-view style="position: absolute;top:0;right:0;left:0;bottom:0" class="div" @scrolltolower="loadMore" scroll-y="true">
        <view class="div pb-30"  v-if="goodsList.length > 0">
        <view class="div mb-5 goods-item"  v-for="(item, index) in goodsList" :key="index">
            <view class="div goods-info">
                <view class="div p-img">
                    <image mode="aspectFit" class="img" :src="item.goods_image"/>
                </view>
                <view class="div p-info">
                    <view class="div name">{{item.goods_name}}</view>
                    <view class="div line">
                        <view class="div price">￥{{item.goods_price}}</view>
                        <view class="div stock">库存:{{item.goods_storage_sum}}</view>
                    </view>
                </view>
            </view>
            <view class="div goods-btn" v-if="item.goods_lock == 0">
                <view class="div common-btn btn-2" @click="goods_edit(item.goods_commonid,item.gc_id)">编辑</view>
                <view class="div common-btn btn-2 ml-5" @click="goods_image(item.goods_commonid)">图片</view>
                <view class="div common-btn btn-2 ml-5" v-if="item.goods_state === 0" @click="goods_show(item.goods_commonid)">上架</view>
                <view class="div common-btn btn-2 ml-5" v-if="item.goods_state === 1" @click="goods_unshow(item.goods_commonid)">下架</view>
                <view class="div common-btn btn-1 ml-5" @click="drop_goods(item.goods_commonid)">删除</view>
            </view>
            <view class="div goods-btn" v-else>
                <view class="div common-btn btn-3">锁定</view>
            </view>
        </view>
        </view>
        <empty-record v-else></empty-record>
    </scroll-view>
</view>
	<view class='common-add-btn-wrapper'><view class="div common-btn common-add-btn"  @click="goods_add">新增</view></view>
</view>
<uni-popup background-color="#fff" ref="confirm" type="dialog">
    <uni-popup-dialog :mode="dialog.mode" :title="dialog.title" :content="dialog.content" :placeholder="dialog.content" @confirm="confirmDialog" @close="closeDialog"></uni-popup-dialog>
</uni-popup>
</seller-base>
</template>

<script>
import {getFontSize} from '@/util/common'
import TitleHeader from '../../TitleHeader'
import { urlencode } from '@/util/common'

import SellerBase from '../SellerBase'

import EmptyRecord from '../../EmptyRecord'
import { getGoodsList, dropGoods, goodsShow, goodsUnshow } from '../../../api/sellerGoods'
import flexLine from '../../flexLine'
export default {
  name: 'Goodsonline',
  computed:{
  fontSize(){
    return getFontSize()
  },
  },
  data(){
    return {
        navHeight: 0,
        dialog:{},
        controller:'Sellergoodsonline',
      params: { 'page': 0, 'per_page': 10 },
      loading: false, // 是否加载更多
      isMore: true, // 是否有更多
      pageTotal: 1, // 总页数
      goodsList: [], // 商品列表
      keyword: '',
      goods_type: '',
      search_type: '',
      orderNav: [
        {
          'name': '出售中',
          'id': ''
        },
        {
          'name': '仓库中',
          'id': 'offline'
        },
        {
          'name': '待审核',
          'id': 'waitverify'
        },
        {
          'name': '违规商品',
          'id': 'lockup'
        }
      ]
    }
  },
  components:{
      TitleHeader,
      SellerBase,
      flexLine,
    EmptyRecord
  },
  onShow () {
	this.reload()
  },
  mounted(){
  // #ifdef  MP-WEIXIN
  this.navHeight = uni.getMenuButtonBoundingClientRect().height
  // #endif
  },
  methods:{
    closeDialog(){
    },
    confirmDialog(value){
      switch(this.dialog.condition){
        case 1:
        uni.showLoading({ title: '加载中' })
        goodsShow(this.controller,this.dialog.data).then(res => {
          uni.hideLoading()
          this.reload()
        }).catch(function (error) {
          uni.showToast({icon:'none',title: error.message})
        })
          break
        case 2:
        uni.showLoading({ title: '加载中' })
        goodsUnshow(this.controller,this.dialog.data).then(res => {
          uni.hideLoading()
          this.reload()
        }).catch(function (error) {
          uni.showToast({icon:'none',title: error.message})
        })
          break
        case 3:
        uni.showLoading({ title: '加载中' })
        dropGoods(this.controller,this.dialog.data).then(res => {
          uni.hideLoading()
          this.reload()
        }).catch(function (error) {
          uni.showToast({icon:'none',title: error.message})
        })
          break
      }
    },
    goBack(){uni.navigateBack({delta:1})},
    goods_add () {
      uni.navigateTo({url:'/pages/seller/goods/GoodsForm1'+'?'+urlencode({ action: 'add' })})
    },
    goods_image (commonid) {
      uni.navigateTo({url:'/pages/seller/goods/GoodsForm3'+'?'+urlencode({ commonid: commonid })})
    },
    goods_edit (commonid, class_id) {
      uni.navigateTo({url:'/pages/seller/goods/GoodsForm2'+'?'+urlencode({ commonid: commonid, class_id: class_id })})
    },
    setOrderNavActive (index) {
      this.goods_type = index
        if(index==''){
            this.controller='Sellergoodsonline'
        }else{
            this.controller='Sellergoodsoffline'
        }
      this.reload()
    },
    getGoodsList (ispush) {
      uni.showLoading({ title: '加载中' })
      let params = this.params
      getGoodsList(this.controller,params, this.keyword, this.goods_type, this.search_type).then(res => {
        uni.hideLoading()

        if (ispush) {
          this.goodsList = this.goodsList.concat(res.result.goods_list)
        } else {
          this.goodsList = res.result.goods_list
        }
        this.pageTotal = res.result.page_total
        if (res.result.hasmore) {
          this.isMore = true
        } else {
          this.isMore = false
        }
      }).catch(error=>{
          uni.hideLoading()
          uni.showToast({icon:'none',title: error.message})
      })
    },
    loadMore () {
      this.loading = true
      this.params.page = ++this.params.page
      if (this.isMore && this.params.page <= this.pageTotal) {
        this.loading = false
        this.getGoodsList(true)
      }
    },
    // 商品上架
    goods_show (goods_id) {
      this.dialog={condition:1,content:'您确定要上架此商品吗？',data:goods_id}
      this.$refs.confirm.open()
    },
    goods_unshow (goods_id) {
      this.dialog={condition:2,content:'您确定要下架此商品吗？',data:goods_id}
      this.$refs.confirm.open()
    },
    drop_goods (goods_id) {
      this.dialog={condition:3,content:'您确定要删除此商品吗？',data:goods_id}
      this.$refs.confirm.open()
    },
    reload () {
      // 重新加载数据
      this.params.page = 0
      this.isMore = true
      this.goodsList = []
      this.loadMore()
    }
  }
}
</script>

<style scoped lang="scss">
	.scroll-view-wrapper{display: flex;flex-direction: column;}
	.scroll-view{flex:1}
    .common-header{
        .btn{background: #000;color: #fff;box-shadow: 0px 2px 4px #d2d2d2;}
    }
    .order-header {
    .ul {
        list-style: none;
        width: auto;
        display: flex;
        justify-content: space-around;
        align-content: center;
        align-items: center;
        height: 100%;
        background: rgba(255, 255, 255, 1);
        border-bottom: 1px solid #e8eaed;
    .li {
        font-size:$subFontSize;
        color: #333;
        height: 100%;
        text-align: center;
        line-height: 2.2rem;
        border-bottom: 0.1rem solid transparent;
    &.active {
         color: $primaryColor;
         border-bottom-color: $primaryColor;
     }
    }
    }
    }
.goods-item{background:#fff;}
.goods-info{height:4rem;padding:.5rem $pageSpace;}
.goods-info .p-img{float:left}
.goods-info .p-img .img{width:4rem;height:4rem;border-radius:.4rem;}
.goods-info .p-info{margin-left:4.5rem;}
.goods-info .p-info .line{display: flex;}
.goods-info .p-info .name{height:2rem;line-height:1rem;font-size:$subFontSize;overflow:hidden;margin-bottom: 1rem}
.goods-info .p-info .price{color:red;line-height:1rem;font-size:$subFontSize;flex:1}
.goods-info .p-info .stock{line-height:1rem;font-size:$h6;color:#666}
.goods-btn{padding:.5rem;display: flex;flex-direction: row;justify-content: flex-end;align-items: stretch;}
.btn-1{border:1px solid $primaryColor;color:$primaryColor;min-width: 2rem}
.btn-2{border:1px solid #333;color:#333;min-width: 2rem}
.btn-3{border:1px solid #eee;color:#B3B6BD;min-width: 2rem}
.seller-search {display: flex;height: 1.6rem;padding: 0.35rem $pageSpace;justify-content: space-between;align-items: center;background: #fff;.slot{width:1.6rem;font-size:1.2rem} }
.seller-search .mint-cell{min-height:0}
.seller-search .mint-cell:after{background-color:#fff!important;}
.common-search{flex:1;height:1.4rem}
.common-search input {box-sizing: border-box;width: 100%;height: 1.6rem;border-radius: 1.6rem;background: #EDEDED url(../../../static/image/home/icon_search.png) no-repeat 0.6rem center;background-size:0.55rem;font-size:$subFontSize;color: #999;padding-left:1.6rem;border: 0;}
.right{width:2.6rem;height: 1.6rem;}
</style>
