<style>
  /*-----------------设置部分--------------*/
  .diy-teacher .diy-component-edit .diy-teacher-label {
    width: 85px;
  }

  .diy-teacher .diy-component-edit .cat-item {
    border: 1px solid #e2e2e2;
    margin-bottom: 5px;
    padding: 15px;
    max-width: 400px;
  }

  .diy-teacher .diy-component-edit .goods-list {
    flex-wrap: wrap;
  }

  .diy-teacher .diy-component-edit .goods-item,
  .diy-teacher .diy-component-edit .goods-add {
    width: 50px;
    height: 50px;
    position: relative;
    margin-right: 15px;
    margin-bottom: 15px;
  }

  .diy-teacher .diy-component-edit .goods-add .el-button {
    width: 100%;
    height: 100%;
    border-radius: 0;
    padding: 0;
  }

  .diy-teacher .diy-component-edit .goods-pic {
    width: 100%;
    height: 100%;
    background-size: cover;
    background-position: center;
  }

  .diy-teacher .diy-component-edit .goods-delete {
    position: absolute;
    left: calc(100% - 10px);
    top: -10px;
    width: 20px;
    height: 20px;
    line-height: 20px;
    padding: 0 0;
    visibility: hidden;
    z-index: 1;
  }

  .diy-teacher .diy-component-edit .goods-item:hover .goods-delete {
    visibility: visible;
  }

  .diy-teacher .diy-component-edit .cat-item-options {
    position: relative;
  }

  .diy-teacher .diy-component-edit .cat-item-options .el-button {
    height: 25px;
    line-height: 25px;
    width: 25px;
    padding: 0;
    text-align: center;
    border: none;
    border-radius: 0;
    position: absolute;
    margin-left: 0;
    top: -16px;
    right: -40px;
  }

  /*-----------------预览部分--------------*/

  .diy-teacher .diy-component-preview .cat-list-left {
    width: 160px;
  }

  .diy-teacher .diy-component-preview .cat-item {
    height: 104px;
    padding: 0 10px;
    text-align: center;
    max-width: 100%;
    white-space: nowrap;
  }

  .diy-teacher .diy-component-preview .cat-list-left .cat-name {
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .diy-teacher .diy-component-preview .cat-item.active {
    color: #ff4544;
  }

  .diy-teacher .diy-component-preview .cat-list-top .cat-item {
    margin: 0 20px;
  }

  .diy-teacher .diy-component-preview .cat-list-top {
    overflow-x: auto;
  }

  .diy-teacher .diy-component-preview .cat-list-top.cat-style-1 .cat-item {
    border-bottom: 4px solid transparent;
  }

  .diy-teacher .diy-component-preview .cat-list-top.cat-style-2 .cat-name {
    background: #ff4544;
    color: #fff;
    border-radius: 100px;
    padding: 0 18px;
  }

  .diy-teacher .diy-component-preview .cat-list-top .cat-item.active {
    border-bottom-color: #ff4544;
  }

  .diy-teacher .diy-component-preview .cat-list-left .cat-item {
    border-left: 2px solid transparent;
  }

  .diy-teacher .diy-component-preview .cat-list-left .cat-item.active {
    border-left-color: #ff4544;
  }

  .diy-teacher .diy-component-preview .goods-list {
    padding: 11px;
  }

  .diy-teacher .diy-component-preview .goods-item {
    padding: 11px;
  }

  .diy-teacher .diy-component-preview .goods-pic {
    background-size: cover;
    background-position: center;
    width: 99.8%;
    height: 700px;
    background-color: #f6f6f6;
    background-repeat: no-repeat;
    position: relative;
    border-radius: 10px 10px 0 0;
  }

  .diy-teacher .diy-component-preview .goods-pic-3-2 {
    height: 471px;
  }

  .diy-teacher .diy-component-preview .goods-name {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    margin-bottom: 10px;
    text-align: center;
  }

  .diy-teacher .diy-component-preview .goods-name-static {
    height: 94px;
  }

  .diy-teacher .diy-component-preview .goods-price {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    color: #ff4544;
    line-height: 48px;
  }

  .diy-teacher .diy-component-preview .goods-list-style--1 .goods-item,
  .diy-teacher .diy-component-preview .goods-list-style-1 .goods-item {
    width: 100%;
  }

  .diy-teacher .diy-component-preview .goods-list-style-2 .goods-item {
    width: 50%;
  }

  .diy-teacher .diy-component-preview .goods-list-style-3 .goods-item {
    width: 33.333333%;
  }

  .diy-teacher .diy-component-preview .goods-list-style-0 .goods-item {
    width: 249px;
  }

  .diy-teacher .diy-component-preview .goods-list-style--1 .goods-pic {
    width: 200px;
    height: 200px;
    border-radius: 10px 0 0 10px;
  }

  .diy-teacher .diy-component-preview .goods-list-style-2 .goods-pic {
    height: 342px;
    border-radius: 10px 10px 0 0;
  }

  .diy-teacher .diy-component-preview .goods-list-style-0 .goods-pic,
  .diy-teacher .diy-component-preview .goods-list-style-3 .goods-pic {
    width: 80px;
    height: 80px;
    border-radius: 50%;
    margin-top: 20px;
  }

  .diy-teacher .diy-component-preview .goods-pic-fill-0 {
    background-size: contain;
  }

  .diy-teacher .diy-component-preview .buy-btn {
    border-color: #ff4544;
    color: #ff4544;
    padding: 0 20px;
    height: 48px;
    line-height: 50px;
    font-size: 24px;
  }

  .diy-teacher .diy-component-preview .buy-btn.el-button--primary {
    background-color: #ff4544;
    color: #fff;
  }

  .diy-teacher .diy-component-preview .goods-tag {
    position: absolute;
    top: 0;
    left: 0;
    width: 64px;
    height: 64px;
    z-index: 1;
    background-size: 100% 100%;
    background-position: center;
    background-repeat: no-repeat;
  }

  .diy-teacher hr {
    border: none;
    height: 1px;
    background-color: #e2e2e2;
  }

  .diy-teacher .diy-component-preview .goods-item .buy-btn.is-round {
    border-radius: 24px;
  }

  .diy-teacher .goods-item.goods-cat-list {
    border-top: 1px solid #e2e2e2;
  }

  .diy-teacher .goods-item.goods-cat-list:first-of-type {
    border-top: 0;
  }

  .diy-teacher .cat-list {
    max-height: 500px;
    overflow: auto;
  }

  .goodsDialog .el-dialog__body {
    padding: 10px 20px;
  }

</style>
<template>
  <div :class="{'active':lsData.isCked}">
    <div class="diy-component-options" v-if="lsData.isCked">
      <el-button type="primary" icon="el-icon-delete" style="left: -25px; top: 0px;" @click="delPlugin()"></el-button>
      <el-button type="primary" icon="el-icon-document-copy" style="left: -25px; top: 30px;"></el-button>
      <el-button type="primary" icon="el-icon-arrow-up" v-if="index>0" @click="resetSord(0)"
        style="right: -25px; top: 0;"></el-button>
      <el-button type="primary" icon="el-icon-arrow-down" v-if="index!=dataLeng-1" @click="resetSord(1)"
        style="right: -25px; top: 30px;"></el-button>
    </div>
    <div class="diy-teacher">
      <div class="diy-component-preview"
        :style="{'backgroundColor':data.backgroundColor,'borderRadius':data.SearchFilletPX+'px'}">
        <div :flex="cMainFlex" :style="{marginLeft:data.PaddingLeft+'px',
        marginRight:data.PaddingRight+'px',marginTop:data.PaddingTop+'px',
        marginBottom:data.PaddingBottom+'px'}">
          <div class="goods-list" :class="'goods-list-style-'+data.listStyle">
            <div v-for="(cat,catIndex) in cCatList" :key="catIndex">
              <div :style="cGoodsListStyle" flex>
                <div v-for="(goods,goodsIndex) in cCatGoodsList(cat, catIndex)" class="goods-item" :key="goodsIndex"
                  :class="data.catPosition==='left'?'goods-cat-list':''">
                  <div style="display: inline-block;" :style="cGoodsItemWidth">
                    <div :style="cGoodsItemStyle" :flex="cGoodsItemFlex"
                      style="position: relative;border:1px solid rgb(226, 226, 226);display: flex;flex-direction: column;align-items: center">
                      <div class="goods-pic"
                        :class="'goods-pic-'+data.goodsCoverProportion+' goods-pic-fill-'+data.fill"
                        :style="'background-image: url('+getIconLink(goods.picUrl)+')'">
                      </div>
                      <div :style="cGoodsItemInfoStyle">
                        <div class="goods-name" :class="data.listStyle===-1?'goods-name-static':''">
                          <template>{{goods.name}}</template>
                        </div>
                        <div flex="box:last">
                          <div style="text-align: center">
                            <template>{{goods.nationality}}</template>
                          </div>
                          <div>
                            <template v-if="cShowBuyBtn">
                              <template v-if="data.buyBtn==='cart'">
                                <i style="font-size: 48px;color: #ff4544;" class="el-icon-shopping-cart-1"></i>
                              </template>
                              <template v-if="data.buyBtn==='add'">
                                <i style="font-size: 48px;color: #ff4544;" class="el-icon-circle-plus-outline"></i>
                              </template>
                              <template v-if="data.buyBtn==='text'">
                                <div :style="cButtonStyle" style="font-size: 24px;border: 1px solid;color: #ffffff;">
                                  {{data.buyBtnText}}</div>
                              </template>
                            </template>
                          </div>
                        </div>
                        <template v-if="goods.lableNameList">
                          <div style="text-align: center;font-size:12px" v-if='goods.lableNameList.length>0'>
                            <span v-for="(gitem,gindex) in goods.lableNameList"
                              :key="gindex">{{gitem}}{{goods.lableNameList.length==gindex+1?'':','}}</span>
                          </div>
                          <div style="text-align: center;font-size:12px" v-if='goods.lableNameList.length==0'>
                            <template>教育</template>
                          </div>
                        </template>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="diy-component-edit" v-if="lsData.isCked">
        <el-form label-width='150px' @submit.native.prevent>
          <template>
            <el-form-item label="老师列表">
              <draggable v-model="data.TeacherList" class="goods-list" style="display:flex;">
                <div class="goods-item" v-for="(goods,goodsIndex) in data.TeacherList" :key="goodsIndex">
                  <el-tooltip effect="dark" content="移除老师" placement="top">
                    <el-button @click="deleteGoods(goodsIndex,null)" circle class="goods-delete" type="danger"
                      icon="el-icon-close"></el-button>
                  </el-tooltip>
                  <div class="goods-pic" :style="'background-image:url('+getIconLink(goods.picUrl)+')'"></div>
                </div>
              </draggable>
              <div class="goods-add">
                <el-button size="small" @click="showGoodsDialog(null)" icon="el-icon-plus"></el-button>
              </div>
            </el-form-item>
          </template>
          <hr>
          <el-form-item label="列表样式">
            <el-radio v-model="data.listStyle" :label="0">左右滑动</el-radio>
          </el-form-item>
          <el-form-item label="背景颜色">
            <div class="input-color" flex="dir:left cross:center">
              <el-color-picker size="small" v-model="data.backgroundColor"></el-color-picker>
              <el-input size="small" style="width: 80px;margin-right: 25px;" v-model="data.backgroundColor"></el-input>
            </div>
          </el-form-item>
          <el-form-item label="上边距">
            <el-input type="number" size="small" :min="0" v-model="data.PaddingTop">
              <template slot="append">px</template>
            </el-input>
          </el-form-item>
          <el-form-item label="下边距">
            <el-input type="number" size="small" :min="0" v-model="data.PaddingBottom">
              <template slot="append">px</template>
            </el-input>
          </el-form-item>
          <el-form-item label="左边距">
            <el-input type="number" size="small" :min="0" v-model="data.PaddingLeft">
              <template slot="append">px</template>
            </el-input>
          </el-form-item>
          <el-form-item label="右边距">
            <el-input type="number" size="small" :min="0" v-model="data.PaddingRight">
              <template slot="append">px</template>
            </el-input>
          </el-form-item>
          <el-form-item label="圆角">
            <el-input type="number" size="small" :min="0" v-model="data.SearchFilletPX">
              <template slot="append">px</template>
            </el-input>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <!-- 选择图片文件 -->
    <el-dialog title="选择文件" :visible.sync="choicImg" width="1240px">
      <ChooseImg @SelectId="SelectId"></ChooseImg>
    </el-dialog>
    <!-- 选择商品 -->
    <el-dialog title="选择老师" :visible.sync="isShowGoods" width="800px">
      <choicels ref="choiceGood"></choicels>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="isShowGoods=false">取 消</el-button>
        <el-button size="small" type="danger" @click="getGoodsChoice()">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>
<script>
  import draggable from "vuedraggable"
  import ChooseImg from "@/components/global/ChooseImg.vue";
  import choicels from "../../common/choicels.vue";

  export default {
    props: ["lsData", "index", "dataLeng"],
    components: {
      draggable,
      ChooseImg,
      choicels
    },
    data() {
      return {
        //可以选择的商品数
        ckGoods: 0,
        data: this.lsData.data,
        position: 'center center',
        repeat: 'no-repeat',
        choicImg: false,
        isShowGoods: false,
        //添加的下标
        comIndex: -1,
      };
    },
    created() {

    },
    methods: {
      //向父组件传值 并调用排序
      resetSord(IsUp) {
        this.$emit('getSord', this.index, IsUp);
      },
      //点击触发父组件删除
      delPlugin() {
        this.$emit('comDelPlugin', this.index);
      },
      cCatGoodsList(cat, catIndex) {
        const goodsList = cat.goodsList;
        let newList = [];
        if (catIndex > 0) {
          newList = [];
        } else {
          if (goodsList && goodsList.length) {
            newList = goodsList;
          } else {
            const defaultGoodsItem = {
              id: 0,
              name: '老师名称',
              picUrl: '',
              price: '100.00',
            };
            newList = new Array(cat.goodsNum).fill(defaultGoodsItem);
          }
        }
        return newList;
      },
      goodsTagChange(e) {
        this.data.goodsTagPicUrl = e;
        this.data.customizeGoodsTag = false;
      },
      customizeGoodsTagChange(e) {
        this.data.goodsTagPicUrl = '';
        this.data.customizeGoodsTag = true;
      },
      deleteCat(index) {
        this.data.catList.splice(index, 1);
      },
      showGoodsDialog(catIndex) {
        if (catIndex !== null) {
          this.comIndex = catIndex
        } else {
          this.comIndex = null;
        }
        this.isShowGoods = true;
      },
      deleteGoods(goodsIndex, catIndex) {
        if (catIndex !== null) {} else {
          this.data.TeacherList.splice(goodsIndex, 1);
        }
      },
      listStyleChange(listStyle) {
        if (listStyle === -1 && this.data.textStyle === 2) {
          this.data.textStyle = 1;
        }
        if (this.data.textStyle === 2 && this.data.buyBtn === 'text') {
          this.data.buyBtn = 'cart';
        }
      },
      //选择图片
      SelectId(msg) {
        this.choicImg = false;
      },
      //选择商品
      getGoodsChoice() {
        var ckedArr = this.$refs.choiceGood.getChoicedGoods();
        var myArray = [];
        if (ckedArr && ckedArr.length > 0) {
          ckedArr.forEach(element => {
            myArray.push({
              id: element.ID,
              name: element.Name,
              nationality: element.Nationality,
              foreignersUrl: element.IsForeigners,
              lableName: "",
              lableNameList: element.LableNameList,
              picUrl: element.TeacherLogo,
            });
          });
        }
        if (this.comIndex !== null) {} else {
          this.data.TeacherList = this.data.TeacherList.concat(myArray);
        }
        this.isShowGoods = false;
        this.$refs.choiceGood.toggleSelection();
      },
      //取消选择商品
      quxiaoGoods() {
        this.isShowGoods = false;
        this.$refs.choiceGood.toggleSelection();
      }
    },
    computed: {
      cMainFlex() {
        if (this.data.catPosition === 'left') {
          return 'dir:left box:first';
        }
        if (this.data.catPosition === 'top') {
          return 'dir:top';
        }
      },
      cCatFlex() {
        if (this.data.catPosition === 'left') {
          return 'dir:top';
        }
        if (this.data.catPosition === 'top') {
          return 'dir:left';
        }
      },
      cCatList() {
        if (this.data.showCat) {
          if (this.data.catList && this.data.catList.length) {
            return this.data.catList;
          } else {
            const defaultCatItem = {
              id: 0,
              name: '分类名称',
              menuName: '分类名称',
              goodsList: [],
              goodsNum: 3,
            };
            return [defaultCatItem, defaultCatItem];
          }
        } else {
          return [{
            id: null,
            name: null,
            menuName: null,
            goodsList: this.data.TeacherList,
          }];
        }
      },
      cCatStyle() {},
      cGoodsListStyle() {
        if (this.data.listStyle === 0) {
          return 'flex-wrap: nowrap;overflow-x:auto;';
        } else {
          return 'flex-wrap: wrap;';
        }
      },
      cGoodsItemFlex() {
        if (this.data.listStyle === -1) {
          return 'dir:left box:first cross:center';
        }
        return 'dir:top';
      },
      cGoodsItemStyle() {
        let style;
        if (this.data.goodsStyle != 3) {
          if (this.data.listStyle === -1) {
            style = 'border: 1px solid #e2e2e2;border-radius:10px;background:#fff;'
          } else {
            style = 'border: 1px solid #e2e2e2;border-radius:10px;background:#fff;'
          }
        } else {
          return style
        }
        if (this.data.goodsStyle === 2) {
          return style
        } else if (this.data.goodsStyle === 1) {
          if (this.data.listStyle === -1) {
            return 'border-radius:10px;background:#fff;'
          } else {
            return 'border-radius:10px;background:#fff;'
          }
        } else {
          return 'background:#fff;';
        }
      },
      cShowBuyBtn() {
        if (!this.data.showBuyBtn) {
          return false;
        }
        if (this.data.textStyle === 2 || this.data.listStyle === 0) {
          return false;
        }
        return true;
      },
      cGoodsItemInfoStyle() {
        let style = '';
        if (this.data.textStyle === 2) {
          style += `text-align: center;`;
        }
        if (this.data.listStyle === -1) {
          style += `height: 200px;padding: 20px 24px 20px 32px;`;
        } else {
          style += `padding:24px 24px;`;
        }
        return style;
      },
      cGoodsItemWidth() {
        if (this.data.listStyle === 0) {
          return 'width: 240px;';
        }
        return 'width: 100%;';
      },
      cButtonStyle() {
        let style =
          `background: ${this.data.buttonColor};border-color: ${this.data.buttonColor};height:48px;line-height:46px;padding:0 20px;`;
        if (this.data.buyBtnStyle === 3 || this.data.buyBtnStyle === 4) {
          style += `border-radius:999px;`;
        }
        if (this.data.buyBtnStyle === 2 || this.data.buyBtnStyle === 4) {
          style += `background:#fff;color:${this.data.buttonColor}`;
        }
        return style;
      },
    },
    mounted() {

    }
  };

</script>
