)<template>
  <div class="carousel-config">
    <div class="config-header">设置</div>
    <widgetFormitem>
      分组名称是否展示
      <el-switch slot="right" v-model="currentSub.show" />
    </widgetFormitem>
    <div class="confit-item">
      <div class="config-title">
        分组名称
      </div>
      <div class="config-setting">
        <el-input
          v-model="currentSub.groupName"
          placeholder="分组名称"
          class="full-input"
        />
      </div>
    </div>
    <div class="confit-item">
      <div class="config-title">
        排列方式
      </div>
      <div class="config-setting">
        <el-select
          v-model="currentSub.arrangement"
          placeholder="请选择"
          class="full-input"
        >
          <el-option
            v-for="link in arrangementList"
            :key="link.value"
            :label="link.label"
            :value="link.value"
          />
        </el-select>
      </div>
    </div>
    <div class="confit-item">
      <div class="config-title">
        <div class="left">展示数量</div>
        <span>{{ currentSub.quantity }}</span>
      </div>
      <div class="config-setting">
        <el-slider v-model="currentSub.quantity" :max="20" input-size="mini" />
      </div>
    </div>
    <div class="config-header">内容</div>
    <div class="config-item">
      <div class="config-setting">
        <div class="collapse-list">
          <div
            v-for="(item, index) in currentSub.list"
            :key="index"
            class="collapse-item"
          >
            <div class="collapse-item-header" @click="collapseToggle(item)">
              <i class="header-icon el-icon-rank"></i>
              <div class="header-thumbnail">
                <img v-if="item.src" :src="item.src" />
                <div v-else class="el-icon-picture-outline"></div>
              </div>
              <div class="header-name">图片</div>
              <div class="header-arrow">
                <i
                  class="el-icon-delete"
                  @click="delectItem(currentSub.list, index)"
                ></i>
              </div>
              <div class="header-arrow">
                <i
                  class="el-icon-arrow-down"
                  :class="item.collapseState ? 'open' : ''"
                ></i>
              </div>
            </div>
            <div v-if="item.collapseState" class="collapse-item-content">
              <div class="config-title">选择图片</div>
              <ImageUpload
                :key="index + '-' + 1"
                :item="[item]"
                :src="item.src"
                @success="success"
              />
              <div class="collapse-item-header">
                <div class="title">商品来源</div>
              </div>
              <div class="collapse-item-content">
                <div
                  v-for="(category, index1) in item.categoryList"
                  :key="index1"
                  class="class-item"
                >
                  <div class="left">
                    类别
                  </div>
                  <div class="content text-overflow">
                    {{ category.categoryName }}
                  </div>
                  <div class="right">
                    <i
                      class="el-icon-delete"
                      @click="delectItem(item.categoryList, index1)"
                    ></i>
                  </div>
                </div>
                <a
                  v-if="!item.categoryList.length"
                  class="add-shop"
                  href="javascript:;"
                  @click="addGoodsClass(index)"
                >
                  <i class="el-icon-plus"></i>
                  添加类别
                </a>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <a class="add-widget" href="javascript:;" @click="addGategory">
      <i class="el-icon-plus"></i>
      添加类别
    </a>
    <goods-class
      ref="edit"
      :dialog-visible="dialog1.isVisible"
      :type="dialog1.type"
      :exclude="exclude1"
      @close="close1"
      @success="success1"
    />
  </div>
</template>

<script>
import { mapState, mapMutations } from 'vuex'
import widgetFormitem from '../components/widgetFormitem'
import GoodsClass from '@/components/applet/GoodsClass'
import ImageUpload from '@/components/ImageUpload'
export default {
  components: {
    widgetFormitem,
    GoodsClass,
    ImageUpload
  },
  data() {
    return {
      arrangementList: [
        {
          value: 1,
          label: '网格'
        },
        {
          value: 2,
          label: '列表'
        }
      ],
      carouselList: [],
      value: '',
      Slidervalue1: 100,
      dialog1: {
        type: 'add',
        isVisible: false
      },
      addClassIndex: null
    }
  },
  computed: {
    ...mapState('creationCamp', [
      'currentComponentKeyCode',
      'currentComponentConfig'
    ]),
    currentSub() {
      return this.currentComponentConfig
    },
    exclude1() {
      const exclude = []
      this.currentSub.list.map(i => {
        i.categoryList.map(category => {
          exclude.push(category.id)
        })
      })
      return exclude
    }
  },
  watch: {
    currentSub: {
      handler: function handler() {
        this.setComponentConfig({
          keyCode: this.currentComponentKeyCode,
          content: this.currentSub
        })
      },
      deep: true
    }
  },
  methods: {
    ...mapMutations('creationCamp', ['setComponentConfig']),
    collapseToggle(item) {
      item.collapseState = !item.collapseState
    },
    addGategory() {
      this.currentSub.list.map(item => {
        item.collapseState = false
      })
      this.currentSub.list.push({
        src: '',
        categoryList: [],
        collapseState: true
      })
    },
    addGoodsClass(index) {
      this.addClassIndex = index
      this.dialog1.isVisible = true
    },
    success(response, file, fileList) {
      const { url } = response.data
      fileList[0].src = url
    },
    success1(obj) {
      this.currentSub.list[this.addClassIndex].categoryList.push(obj)
    },
    close1() {
      this.dialog1.isVisible = false
    },
    delectItem(list, index) {
      list.splice(index, 1)
    }
  }
}
</script>

<style lang="less" scoped>
.collapse-item-header {
  display: flex;
  align-items: center;
  padding: 0 12px;
  .title {
    flex: 1;
  }
}
.add-shop {
  height: 30px;
  line-height: 30px;
  font-size: 14px;
  color: #409eff;
  border: 1px dashed #ccc;
  text-align: center;
  display: block;
  cursor: pointer;
  margin-bottom: 20px;
}
.full-input {
  margin-bottom: 12px;
}
.class-item {
  display: flex;
  align-items: center;
  height: 36px;
  padding: 0 12px;
  margin-bottom: 10px;
  border: 1px solid #eee;
  font-size: 12px;
  .left {
    color: #409eff;
  }
  .content {
    flex: 1;
    margin: 0 8px;
  }
}
</style>
