<template>
  <div>
    <el-dialog
      v-loading="loading"
      :show-close="false"
      :close-on-click-modal="false"
      :visible.sync="isShow"
      :title="curObj.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">
        <div slot="otherItem">
          <div class="scoped-img-area">
            <div class="sia-op" v-for="(imgsrc,index) in imagesArr" :key="index">
              <img class="img" :src="imgsrc + '_adm0'" alt="img">
              <span class="close" @click="imgDel(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"/>
            </el-upload>
          </div>
        </div>
      </base-form>
      <div class="xl-form">
        <div class="xl-form-footer">
          <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-dialog>
  </div>
</template>
<script>
import { arrToObj } from '@/utils'
export default {
  components: { },
  mixins,
  props: {
    isShow: Boolean,
    curObj: Object
  },
  inject: ['parentData'],
  data() {
    const token = window.sessionStorage.getItem('fp_token')
    let domainUrl = process.env.VUE_APP_BASE_API
    return {
      domainUrl,
      token,
      loading: false,
      formData: [],
      cObj: {},
      imagesArr: [],
    }
  },
  watch: {
    isShow: function(val) {
      if (val) {
        this.getDef()
      }
    },
  },
  methods: {
    imgDel (index) {
      this.imagesArr.splice(index, 1)
    },
    roomAreaUploadSuccess(res, file) {
      const data = res.data || {}
      this.imagesArr.push(`${data.url}`)
    },
    roomAreaUploadBefore(file) {
      const isJPGPNG = file.type === 'image/jpeg' || file.type === 'image/png'
      const isLt2M = file.size / 1024 / 1024 < 2
      if (!isJPGPNG) {
        this.$message.error('上传图片只能是 JPG PNG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 2M!')
      }
      return isJPGPNG && isLt2M
    },
    getDef (str) {
      let params = {}
      params = { ...this.curObj }
      let imagesArr = []
      if (params.images) imagesArr = params.images.split(',')
      this.imagesArr = [...imagesArr]
      this.formData = [
        { label: '标题', key: 'title'},
        { label: '时间', key: 'news_at', type: 'datePicker', type2: 'date'},
        { label: '动态内容', key: 'trends_cont', type: 'textarea'},
      ]
      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.id) newForm.estate_news_id = this.curObj.id
            newForm.estate_id = this.parentData.searchForm.estate_id
            if (this.imagesArr && this.imagesArr.length > 0) newForm.images = this.imagesArr.join(',')
            let apiStr = 'admestatenewsadd'
            if (this.curObj.id) apiStr = 'admestatenewsedit'
            this.$api.house[apiStr](newForm).then(data => {
              this.$msgs(newForm.estate_id ? '编辑成功' : '新增成功')
              this.productData = []
              this.$emit('close', newForm)
            })
          }
        })
      } else {
        this.$emit('close')
        this.productData = []
        this.setDefaultValue()
      }
    },
  }
}
</script>
<style lang="scss" scoped>
@import '../../../../styles/libEdit.scss';
.lib-edit {
  padding-top: 0;
  ::v-deep .el-date-editor.el-input {
    width: 100%;
  }
  ::v-deep .el-textarea__inner {
    height: 300px;
  }
}

.scoped-img-area {
  padding-left: 40px;
  .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>
