<template>
  <div>
    <el-dialog
      v-loading="loading"
      :show-close="false"
      :close-on-click-modal="false"
      :visible.sync="isShow"
      :title="curObj.estate_id ? '编辑摇号信息' : '新增摇号信息'"
      :fullscreen="false"
      width="700px"
      custom-class="xl-dialog"
      center
    >
      <base-form ref="ruleForm" class="lib-edit" :data="formData" :is-inline="false" label-width="110px" :insertSlotArr="[0, 1]">
        <div slot="OI0" class="scoped-other-form">
          <div class="scoped-img-area">
            <div class="sia-title">项目图</div>
            <div class="sia-op" v-for="(imgsrc,index) in infoImg" :key="index">
              <img class="img" :src="imgsrc" alt="img">
              <span class="close" @click="infoImgDel(index)"></span>
            </div>
            <el-upload
              class="sia-img"
              :action="`${domainUrl}/adm/upload/cloudpub`"
              :data="{logic_type: 'estate', token}"
              name="upload"
              :show-file-list="false"
              :on-success="infoImgSuccess"
              :before-upload="imgUploadBefore"
              >
              <i class="el-icon-plus icon" />
            </el-upload>
          </div>
        </div>
        <div slot="OI1" class="scoped-other-form">
          <div class="scoped-img-area">
            <div class="sia-title">一房一价图</div>
            <div class="sia-op" v-for="(imgsrc,index) in houseImg" :key="index">
              <img class="img" :src="imgsrc" alt="img">
              <span class="close" @click="houseImgDel(index)"></span>
            </div>
            <el-upload
              class="sia-img"
              :action="`${domainUrl}/adm/upload/cloudpub`"
              :data="{logic_type: 'estate', token}"
              name="upload"
              :show-file-list="false"
              :on-success="houseImgSuccess"
              :before-upload="imgUploadBefore"
              >
              <i class="el-icon-plus icon" />
            </el-upload>
          </div>
        </div>
        <div slot="otherItem" class="scoped-other-form">
          <div class="scoped-img-area">
            <div class="sia-title">摇号结果图</div>
            <div class="sia-op" v-for="(imgsrc,index) in lotteryImg" :key="index">
              <img class="img" :src="imgsrc" alt="img">
              <span class="close" @click="lotteryImgDel(index)"></span>
            </div>
            <el-upload
              class="sia-img"
              :action="`${domainUrl}/adm/upload/cloudpub`"
              :data="{logic_type: 'estate', token}"
              name="upload"
              :show-file-list="false"
              :on-success="lotteryImgSuccess"
              :before-upload="imgUploadBefore"
              >
              <i class="el-icon-plus icon" />
            </el-upload>
          </div>
        </div>
        <div slot="footer" style="padding-top: 20px;">
          <el-button class="xl-form-btn t2" @click="close">关 闭</el-button>
          <el-button class="xl-form-btn t1" @click="close('confirm')">确定</el-button>
        </div>
      </base-form>
    </el-dialog>
  </div>
</template>
<script>
export default {
  components: { },
  mixins,
  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,
      formData: [],
      loading: true,
      cObj: {},
      infoImg: [],
      houseImg: [],
      lotteryImg: [],
    }
  },
  watch: {
    isShow: function(val) {
      if (val) {
        this.infoImg = []
        this.houseImg = []
        this.lotteryImg = []
        this.$api.house.admestatelotterydetail({estate_id: this.curObj.estate_id}).then(res => {
          this.cObj = res || {}
          if (res.info_img) this.infoImg = res.info_img.split(',')
          if (res.house_img) this.houseImg = res.house_img.split(',')
          if (res.lottery_img) this.lotteryImg = res.lottery_img.split(',')
          this.getDef()
        })
      }
    },
  },
  methods: {
    comImgDel (str, index) {
      let cImg = this[str]
      cImg.splice(index, 1)
      this[str] = [...cImg]
    },
    comImgSuccess(str, res) {
      const data = res.data || {}
      let cImg = [...this[str]]
      cImg.push(`${data.url}`)
      this[str] = [...cImg]
    },
    lotteryImgDel (index) {
      this.comImgDel('lotteryImg', index)
    },
    lotteryImgSuccess(res, file) {
      this.comImgSuccess('lotteryImg', res)
    },
    houseImgDel (index) {
      this.houseImg.splice(index, 1)
      this.houseImg = [...this.houseImg]
    },
    houseImgSuccess(res, file) {
      this.comImgSuccess('houseImg', res)
    },
    infoImgDel (index) {
      this.infoImg.splice(index, 1)
      this.infoImg = [...this.infoImg]
    },
    infoImgSuccess(res, file) {
      this.comImgSuccess('infoImg', res)
    },
    imgUploadBefore(file) {
      const isJPGPNG = file.type === 'image/jpeg' || file.type === 'image/png'
      const isLt2M = file.size / 1024 / 1024 < 5
      if (!isJPGPNG) {
        this.$message.error('上传图片只能是 JPG PNG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 5M!')
      }
      return isJPGPNG && isLt2M
    },
    getDef() {
      const params = { ...this.cObj }
      this.formData = [
        { label: '摇号中标题', key: 'under_way' },
        { label: '摇号结果标题', key: 'result' },
      ]
      this.setDefaultValue(params)
    },
    close(str) {
      if (str === 'confirm') {
        this.$refs['ruleForm'].$refs['baseForm'].validate((valid) => {
          if (valid) {
            const oldform = this.$refs.ruleForm.baseForm
            const newForm = { ...oldform }
            if (this.curObj.estate_id) {
              newForm.estate_id = this.curObj.estate_id
            }
            if (this.infoImg && this.infoImg.length > 0) {
              newForm.info_img = this.infoImg.join(',')
            }
            if (this.houseImg && this.houseImg.length > 0) {
              newForm.house_img = this.houseImg.join(',')
            }
            if (this.lotteryImg && this.lotteryImg.length > 0) {
              newForm.lottery_img = this.lotteryImg.join(',')
            }
            let apiStr = 'admestatelotterychange'
            this.$api.house[apiStr](newForm).then(data => {
              this.$msgs(newForm.id ? '编辑成功' : '新增成功')
              this.$emit('close', newForm)
            })
          }
        })
      } else {
        this.$emit('close')
        this.setDefaultValue()
      }
    }
  }
}
</script>
<style lang="scss" scoped>
@import '../../../../styles/libEdit.scss';
.lib-edit {
  padding-top: 0;
  ::v-deep .el-form-item {
    margin-bottom: 10px;
  }
  ::v-deep .el-date-editor.el-input {
    width: 100%;
  }
}

.scoped-img-area {
  padding-left: 20px;
  margin-bottom: 20px;
  .sia-title {
    font-size: 12px;
    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>