<template>
  <div class="carousel-config">
    <div class="config-header">设置</div>
    <div class="confit-item">
      <div class="config-title">
        一行显示
      </div>
      <div class="config-setting">
        <Select
          v-model="currentSub.number"
          placeholder="请选择"
          class="full-input"
        >
          <Option
            v-for="item in numberList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </Select>
      </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="confit-item">
                <div class="config-title">
                  名称
                </div>
                <div class="config-setting">
                  <Input v-model="item.name" class="full-input" />
                </div>
              </div>
              <Jump :item="item" />
            </div>
          </div>
        </div>
      </div>
    </div>
    <a class="add-widget" href="javascript:;" @click="addCarousel">
      <i class="el-icon-plus"></i>
      添加图片
    </a>
  </div>
</template>

<script>
import { mapState, mapMutations } from 'vuex'
import { Select, Option, Input } from 'element-ui'
import ImageUpload from '@/components/ImageUpload'
import Jump from '../components/Jump'
export default {
  components: {
    Select,
    Option,
    ImageUpload,
    Input,
    Jump
  },
  data() {
    return {
      numberList: [
        {
          value: 3,
          label: '三个'
        },
        {
          value: 4,
          label: '四个'
        },
        {
          value: 5,
          label: '五个'
        }
      ]
    }
  },
  computed: {
    ...mapState('creationCamp', [
      'diyComponents',
      'templatePage',
      'currentComponentKeyCode',
      'currentComponentConfig'
    ]),
    currentSub() {
      return this.currentComponentConfig
    }
  },
  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
    },
    success(response, file, fileList) {
      const { url } = response.data
      fileList[0].src = url
    },
    delectItem(list, index) {
      list.splice(index, 1)
    },
    addCarousel() {
      if (this.currentSub.list.length >= 20) {
        return
      }
      this.currentSub.list = this.currentSub.list.map(item => {
        item.collapseState = false
        return item
      })
      this.currentSub.list.push({
        href: '',
        src: '',
        name: '',
        collapseState: true
      })
    }
  }
}
</script>

<style lang="less" scoped>
.carousel-config {
  .collapse-list {
    margin-bottom: 10px;
    .collapse-item {
      margin-top: 10px;
      background-color: #fff;
      border: 1px solid #c5cddb;
      .collapse-item-header {
        display: flex;
        align-items: center;
        height: 36px;
        background-color: #eef0f2;
        .header-icon {
          width: 32px;
          text-align: center;
          cursor: move;
        }
        .header-thumbnail {
          display: flex;
          align-items: center;
          width: 30px;
          height: 25px;
          font-size: 20px;
          img {
            width: 100%;
            vertical-align: middle;
            border-style: none;
          }
        }
        .header-name {
          margin-left: 10px;
          flex: 1;
        }
        .header-arrow {
          width: 32px;
          text-align: center;
          font-size: 16px;
          i {
            transition: all 0.3s;
            &.open {
              transform: rotate(-180deg);
            }
          }
        }
      }
      .collapse-item-content {
        padding: 20px 10px;
        .upload-widget {
          margin-bottom: 10px;
          .upload-uploader {
            width: 100%;
            height: 110px;
            /deep/ .el-upload {
              width: 100%;
            }
          }
          .upload-images {
            width: 100%;
            height: 110px;
          }
          .upload-btn {
            box-sizing: border-box;
            display: flex;
            align-items: center;
            justify-content: center;
            width: 100%;
            height: 110px;
            cursor: pointer;
            border: 2px dashed #2e60f8;
            border-radius: 2px;
            background-color: #f8f9fb;
            text-align: center;
            font-size: 20px;
            color: #2e60f8;
            i {
              color: #2e60f8;
              font-size: 20px;
            }
            .upload-title {
              margin-left: 10px;
              font-size: 14px;
            }
          }
        }
      }
    }
  }
  .add-widget {
    height: 36px;
    line-height: 36px;
    font-size: 14px;
    background-color: #2e60f8;
    color: #fff;
    text-align: center;
    display: block;
    cursor: pointer;
    margin-bottom: 20px;
  }
}
</style>
