<template>
  <div class="app-container">
    <!-- 选择商品类目 start -->
    <el-card class="box-card" v-if="!isShowGoodsDetails">
      <div slot="header" class="clearfix">
        <p class="card-header-title"><span class="blue-block-goods"></span>确认商品所在目录</p>
      </div>
      <div>
        <div>
          <p class="goods-type-tip">为商品设置正确的类目，能让商品快速的被搜索到</p>
        </div>
        <div class="goods-type-options" v-loading="loading">
          <el-cascader-panel v-model="SSQList" :props='props' @change="SSQChange" ref="ssqCascader"></el-cascader-panel>
        </div>
        <el-button type="primary" class="next-step" :disabled="isNextStep" @click="nextStep">下一步</el-button>
      </div>
    </el-card>
    <!-- 选择商品类目  end  -->
    <!-- 添加商品 start -->
    <el-card class="box-card good-details-body" v-if="isShowGoodsDetails">
      <div class="floor-nav" id="floorNavList">
        <ul class="nav-list">
          <li class="nav-list-item" :class="{'floor-item-active': isFIActive === index}" v-for="(item, index) in floorNav" :key="item.id" @click="setFloorNavMountClick(index)">{{ item.name }}</li>
        </ul>
      </div>
      <div class="floor-cont" ref="scrollview">
        <div class="floor-item">
          <div class="floor-item-box">
            <p class="card-header-title floor-item-box-title"><span class="blue-block-goods blue-block-goods-title"></span>商品信息</p>
            <Goodsinfomation ref="goodsInfo" :goodsinfodata='goodsinfodata'/>
          </div>
        </div>

        <div class="floor-item">
          <div class="floor-item-box">
            <p class="card-header-title floor-item-box-title"><span class="blue-block-goods blue-block-goods-title"></span>商品规格</p>
          </div>
        </div>

        <div class="floor-item">
          <div class="floor-item-box">
            <p class="card-header-title floor-item-box-title"><span class="blue-block-goods blue-block-goods-title"></span>商品售价</p>
          </div>
        </div>

        <div class="floor-item">
          <div class="floor-item-box">
            <p class="card-header-title floor-item-box-title"><span class="blue-block-goods blue-block-goods-title"></span>商品详情</p>
          </div>
        </div>

        <div class="floor-item">
          <div class="floor-item-box">
            <p class="card-header-title floor-item-box-title"><span class="blue-block-goods blue-block-goods-title"></span>售后及服务</p>
            <Goodsaftersale ref="goodsaftersale" :goodsaftersale="goodsaftersale" />
            <div class="submit-type-con">
              <el-radio-group v-model="submitType" size="small">
                <el-radio :label="1" border>放入仓库</el-radio>
                <el-radio :label="2" border>立即上架</el-radio>
              </el-radio-group>
            </div>
          </div>
        </div>

      </div>
      <div class="submit-cont">
        <el-button type="primary" style="padding: 10px 30px;" @click="save">提 交</el-button>
      </div>
    </el-card>
    <!-- 添加商品  end  -->
  </div>
</template>

<script>
  import { getAreaList } from '@/api/module/retreat/address';
  import Goodsinfomation from './components/goodsinfomation'
  import Goodsaftersale from './components/goodsaftersale'
  // let TIMER = null;
  export default {
    data() {
      return {
        loading: false,
        SSQList: [], // 省市区
        SSQStr: '', // 省市区 拼接
        props: {
          expandTrigger: 'click',
          lazy: true,
          lazyLoad: this.lazyLoad,
          value: "id",
          label: 'name',
          leaf: 'leaf'
        },
        isNextStep: true,
        isShowGoodsDetails: false,
        isFIActive: 0,
        /**/
        floorNav: [
          { id: 1, name: '商品信息' },
          { id: 2, name: '商品规格' },
          { id: 3, name: '商品售价' },
          { id: 4, name: '商品详情' },
          { id: 5, name: '售后及服务' },
        ],
        /* 商品信息 */
        goodsinfodata: {
          categoryList: [],
          categoryStr: ''
        },
        /* 售后及服务 */
        goodsaftersale: {

        },
        submitType: 1,


        TIMER: null,
      } // return end
    },
    components: {
      Goodsinfomation,
      Goodsaftersale
    },

    created() {

    },
    mounted() {

    },

    methods: {
      // 只有点击完最后一级，才会有值
      SSQChange() {
        if(this.SSQList && this.SSQList.length > 0) {
          this.isNextStep = false
        }else {
          this.SSQList = []
          this.isNextStep = true
        }
      },
      lazyLoad(node, resolve) {
        this.SSQList = [];
        this.SSQStr = '';
        this.getArea(node, resolve);
        if(this.SSQList && this.SSQList.length > 0) {
          this.isNextStep = false
        }else {
          this.isNextStep = true
        }
      },
      getArea(node, resolve) {
        const level = node.level;
        let limboNode = {};
        if(level === 0) {
          limboNode = {pid: 0}
        }
        if(level === 1) {
          limboNode = { pid: node.value };
        }
        if(level === 2) {
          limboNode = { pid: node.value };
        }
        this.loading = true;
        getAreaList(limboNode).then(res => {

          let result = {};
         // debugger
          if (level === 0) {
            result = res.data
            result.forEach(item => {
              item.value = item.id;
              item.label = item.name;
              item.children=[];
              item.leaf = 0;  // 可以控制 是否有下级   值为true都不行，必须等于0
            })
          }

          if (level === 1) {
            result = res.data
            result.forEach(item => {
              item.value = item.id;
              item.label = item.name
              item.children=[];
              //这句代码表示当点击最后一级的时候 label后面不会转圈圈 并把相关值赋值到选择器上
              item.leaf = 0
            })
          }

          if (level === 2) {
            result = res.data
            result.forEach(item => {
              item.value = item.id;
              item.label = item.name
              item.leaf = 1;
            })
          }

          // result = []
          resolve(result)
          this.loading = false;
        });
      },
      // 添加商品后，下一步 操作
      nextStep() {
        // 注意数据格式 [1,2,3] 数组，且里面是数字类型
        let ssqLabelList = this.$refs['ssqCascader'].getCheckedNodes()[0].pathLabels;
        this.goodsinfodata.categoryList = this.SSQList;
        this.goodsinfodata.categoryStr = ssqLabelList[0] + '/' + ssqLabelList[1] + '/' + ssqLabelList[2];
        this.isShowGoodsDetails = true;
      },

      //
      // getGoodsAfterSaleData(data) {
      //   console.log("售后子组件给的：",data);
      // },
      //
      save() {
        // 此处变量 有顺序 校验时，电梯至某处
        // 商品信息 是否填写完整
        let isGoodsInfoMsg = this.$refs['goodsInfo'].validateGoodsInfoForm();
        // 售后服务 是否填写完整
        let isGoodsAfterSaleMsg = this.$refs['goodsaftersale'].validateGoodsAfterSaleForm();
        //console.log(212,this.$refs.goodsaftersale.goodsAfterSaleForm) // 主动获取子组件数据
        //console.log(2222,isGoodsAfterSaleMsg,isGoodsInfoMsg);
        if(!isGoodsInfoMsg) {
          this.$message({type:'error',message:'商品信息未填写完整'});
          this.setFloorNavMountClick(0);
          return
        }
        if(!isGoodsAfterSaleMsg) {
          this.$message({type:'error',message:'售后及服务未填写完整'});
          this.setFloorNavMountClick(4);
          return
        }
      },

      /*  添加商品详细 */
      /* 设置楼层导航事件驱动方法* @params Number index  楼层下标 */
      setFloorNavMountClick(index) {
        let _this = this
        _this.isFIActive = index;
        //debugger
        clearInterval(_this.TIMER)
        let floor_item = document.getElementsByClassName('floor-item'),
            floor_offsetTop = floor_item[index].offsetTop - floor_item[0].offsetTop,
            window_scrollTop = _this.$refs.scrollview.scrollTop,
            timer = { step: 45, times: 20, FLOOR_OFFSETTOP: floor_offsetTop };

        console.log(3333,floor_item[0].offsetTop,floor_item[1].offsetTop,floor_item[2].offsetTop,floor_item[3].offsetTop,floor_item[4].offsetTop,document.body.scrollTop,document.documentElement.scrollTop);
        //debugger
        if (window_scrollTop > floor_offsetTop) {
          _this.setFloorScrollArrowUp(timer)
        } else if (window_scrollTop === floor_offsetTop) {
          return false
        } else {
          _this.setFloorScrollArrowDown(timer)
        }
      },
      /* 设置楼层向上滚动* @params Object timer 定时器配置 */
      setFloorScrollArrowUp(timer) {
        //debugger
        let _this = this
        clearInterval(_this.TIMER)
        _this.TIMER = setInterval(() => {
          const window_scrollTop = _this.$refs.scrollview.scrollTop
          if (window_scrollTop <= timer.FLOOR_OFFSETTOP) {
            _this.$refs.scrollview.scrollTop = timer.FLOOR_OFFSETTOP
            clearInterval(_this.TIMER)
          } else {
            _this.$refs.scrollview.scrollTop = window_scrollTop - timer.step
          }
        }, timer.times)
      },
      /* 设置楼层向下滚动@params Object timer 定时器配置 */
      setFloorScrollArrowDown(timer) {
        let floor_item = document.getElementsByClassName('floor-cont');
        let fuck = 0;
        let _this = this
        clearInterval(_this.TIMER)


        fuck = timer.FLOOR_OFFSETTOP
        /**/
        // if(_this.isFIActive === 4) {
        //   console.log(280,_this.$refs.scrollview.scrollTop);
        //   timer.FLOOR_OFFSETTOP = timer.FLOOR_OFFSETTOP - 333;
        // }
        console.log(999,floor_item[0].offsetHeight); //offsetHeight
        /**/

        _this.TIMER = setInterval(() => {
          const window_scrollTop = _this.$refs.scrollview.scrollTop
          if (window_scrollTop >= timer.FLOOR_OFFSETTOP) {
            _this.$refs.scrollview.scrollTop = timer.FLOOR_OFFSETTOP
            clearInterval(_this.TIMER)
          } else {
            _this.$refs.scrollview.scrollTop = window_scrollTop + timer.step
          }
        }, timer.times)
      },

    } // methods end
  }
</script>

<style scoped lang="scss" type="text/css">
  p {
    margin: 0;
    padding: 0;
  }
  /* el-card title hxz 样式统一 */
  .card-header-title {
    display: flex;
    flex-direction: row;
    justify-content: start;
    align-items: center;
    font-size: 18px;
    color: #333;
    font-weight: 400;
    height: 24px;
  }
  .blue-block-goods {
    width: 5px;
    height: 24px;
    background: #3A84FF;
    margin: 0 7px 0 0;
    border-radius: 2px;
  }
  /* el-card title hxz 样式统一 end */
  .goods-type-tip {
    width: 100%;
    background: #F1F7FD;
    border: 1px solid #3A84FF;
    font-size: 14px;
    font-weight: 400;
    color: #3A84FF;
    line-height: 1em;
    padding: 14px 15px;
  }
  .goods-type-options {
    margin: 25px 0;
  }
  /*------------ 联级面板样式重置  start ------------*/
  /deep/.el-cascader-panel.is-bordered {
    border: none;
  }
  /deep/.el-cascader-menu {
    min-width: 280px;
    border: none;
    box-shadow: 0px 4px 9px 1px rgba(51, 51, 51, 0.09);
    border-radius: 2px;
    overflow: hidden;
  }
  /deep/.el-cascader-menu__wrap {
    width: 280px;
    height: 500px;
    overflow: auto;
    margin: 5px 0!important;
  }
  /* 很重要,不要问我为什么 */
  /deep/.el-scrollbar:hover>.el-scrollbar__bar, /deep/.el-scrollbar:active>.el-scrollbar__bar, /deep/.el-scrollbar:focus>.el-scrollbar__bar {
    opacity: 0;
  }
  /*------------ 联级面板样式重置  end ---------------*/
  .next-step {
    padding: 10px 25px;
    margin: 0 auto;
    display: block;
  }

  /* 电梯效果 样式 */
  .floor-nav {
    width: 100%;
  }
  .floor-nav .nav-list {
    padding: 0;
    margin:0;
    display: flex;
    justify-content: start;
    flex-direction: row;
    border-bottom:1px solid #eee;
  }
  .floor-nav .nav-list .nav-list-item {
    font-size: 18px;
    font-weight: 400;
    color: #333333;
    padding: 0 25px 15px;
    list-style: none;
    vertical-align: middle;
    align-self: center;
    border-bottom: 2px solid #fff;
    cursor: pointer;
  }
  .floor-nav .nav-list .floor-item-active,
  .floor-nav .nav-list .nav-list-item:hover {
    color: #3A84FF;
    font-weight: bold;
    border-bottom: 2px solid #3A84FF;
  }

  .floor-item-box-title {
    background: #F1F1F6;
    height: 45px;
  }
  .blue-block-goods-title {
    margin: 0 20px 0 0;
  }
  /deep/.good-details-body .el-card__body  {
    height: 100%;
    padding: 15px 20px 0px 20px;
    overflow: hidden;
  }

  .floor-cont{
    height: calc(100% - 98px);
    overflow: auto;
  }

  .floor-item {
    padding: 0 20px 0 0;
    margin: 15px auto;
    min-height: 300px;
    color: #333;
  }
  .submit-type-con {
    width: 236px;
    margin: 0 auto 20px;
  }
  .submit-cont {
    width: calc(100% + 40px);
    padding: 12px 0 8px;
    margin-left: -20px;
    display: flex;
    flex-direction: row;
    justify-content: center;
    align-items: center;
    /*align-content: center;*/
    box-shadow: 0px -8px 9px 1px rgba(51, 51, 51, 0.06);
  }
</style>
