<template>
  <div>
    <el-drawer
      v-loading="loading"
      :show-close="false"
      :title="'编辑相册'"
      :wrapper-closable="false"
      :close-on-press-escape="false"
      :visible.sync="isShow"
      size="960px"
      custom-class="xl-drawer"
      direction="rtl"
    >
      <div class="scoped-img-area" v-for="(item, iIndex) in imagesArr" :key="iIndex">
        <div class="sia-title">{{item.key}}</div>
        <div class="sia-op" v-for="(imgsrc,index) in item.urls" :key="index">
          <img class="img" :src="imgsrc + '_adm0'" alt="img" @click="openbigImg(imgsrc + '_adm0')">
          <span class="close" @click="imgDel(iIndex, index)"></span>
        </div>
        <el-upload
          class="sia-img"
          :action="`${domainUrl}/adm/upload/cloud`"
          :data="{logic_type: 'estate', token}"
          name="upload"
          :show-file-list="false"
          :on-success="roomAreaUploadSuccess"
          :before-upload="roomAreaUploadBefore"
          >
          <i class="el-icon-plus icon" @click="addImgHandle(iIndex)" />
        </el-upload>
      </div>
      <div class="xl-form">
        <div class="xl-form-footer fixed" style="width:960px;padding-top: 20px;border-top: 1px solid #dcdcdc;right:0;">
          <el-button class="xl-form-btn t2" @click="close">关 闭</el-button>
          <el-button class="xl-form-btn t1" @click="close('confirm')">确定</el-button>
        </div>
      </div>
    </el-drawer>
    <popup-big-img :is-show="bigImgShow" :src="`${bigImgSrc}`" @close="closebigImg" />
  </div>
</template>
<script>
import bigImgPopup from '_m/bigImgPopup.js'
export default {
  components: { },
  mixins: [...mixins, bigImgPopup],
  props: {
    isShow: Boolean,
    curObj: Object
  },
  inject: ['parentData'],
  data() {
    let domainUrl = process.env.VUE_APP_BASE_API
    const token = window.sessionStorage.getItem('fp_token')
    return {
      domainUrl,
      token,
      loading: false,
      formData: [],
      cObj: {},
      isShowMap: false,
      imagesArr: [],
      curIndex: 0,
    }
  },
  watch: {
    isShow: function(val) {
      if (val) {
        let imagesArr = JSON.parse(JSON.stringify(this.$dictData.estate_photo))
        if (this.curObj.id) {
          this.loading = true
          this.$api.house.admestatephotolist({estate_id: this.curObj.id}).then(res => {
            let curArr = res.data ? JSON.parse(res.data) : []
            curArr.forEach(item => {
              imagesArr.map(one => {
                if (item.val === one.val) {
                  one.urls = item.urls
                }
              })
            })
            this.imagesArr = [...imagesArr]
            this.loading = false
          }).catch(err => {
            this.loading = false
          })
        }
      }
    },
  },
  methods: {
    addImgHandle (index) {
      this.curIndex = index
    },
    imgDel (iIndex, index) {
      this.imagesArr[iIndex].urls.splice(index, 1)
      this.imagesArr = [...this.imagesArr]
    },
    roomAreaUploadSuccess(res, file) {
      const data = res.data || {}
      let urls = this.imagesArr[this.curIndex].urls || []
      urls.push(`${data.url}`)
      this.imagesArr[this.curIndex].urls = urls
      this.imagesArr = [...this.imagesArr]
    },
    roomAreaUploadBefore(file) {
      const isJPGPNG = file.type === 'image/jpeg' || file.type === 'image/png'
      const isLtM = file.size / 1024 / 1024 < 10
      if (!isJPGPNG) {
        this.$message.error('上传图片只能是 JPG PNG 格式!')
      }
      if (!isLtM) {
        this.$message.error('上传图片大小不能超过 10M!')
      }
      return isJPGPNG && isLtM
    },
    close (str) {
      if (str === 'confirm') {
        const newForm = { }
        if (this.curObj.id) newForm.estate_id = this.curObj.id
        let cArr = []
        this.imagesArr.forEach(obj => {
          const imgArr = obj.urls || []
          if (imgArr.length > 0) {
            cArr.push({
              key: obj.key,
              val: obj.val,
              urls: imgArr
            })
          } 
        })
        newForm.images = JSON.stringify(cArr)
        let apiStr = 'admestatephotoedit'
        if (this.curObj.id) apiStr = 'admestatephotoedit'
        this.$api.house[apiStr](newForm).then(data => {
          this.$msgs(newForm.id ? '编辑成功' : '新增成功')
          this.$emit('close', newForm)
          this.curIndex = 0
        })
      } else {
        this.$emit('close')
        this.curIndex = 0
      }
    },
  }
}
</script>
<style lang="scss" scoped>
.scoped-img-area {
  padding-left: 20px;
  margin-bottom: 20px;
  .sia-title {
    font-size: 16px;
    padding-bottom: 10px;
    color: #666;
    font-weight: bold;
  }
  .sia-op {
    display: inline-block;
    vertical-align: middle;
    margin-right: 10px;
    margin-bottom: 10px;
    border: 1px solid #f2f2f2;
    width: 80px;
    height: 80px;
    position: relative;
    &:hover {
      .img-big {
        display: block;
      }
    }
    .img {
      width: 80px;
      height: 80px;
    }
    .close {
      position: absolute;
      width: 20px;
      height: 20px;
      top: -10px;
      right: -10px;
      background: url(../../../../assets/icon_g_close.png) no-repeat;
      background-size: 20px;
      cursor: pointer;
    }
    .img-big {
      position: absolute;
      bottom: 0;
      left: 0;
      width: 400px;
      height: auto;
      display: none;
      box-shadow: 10px 10px 10px #ccc;
      z-index: 99;
    }
  }
  .sia-img {
    display: inline-block;
    vertical-align: middle;
    width: 80px;
    height: 80px;
    overflow: hidden;
    border: 1px dashed #999;
    margin-bottom: 10px;
    .el-icon-plus {
      color: #999;
      padding: 30px;
    }
  }
}
</style>
