<template>
  <div>
    <el-dialog
      v-loading="loading"
      :show-close="false"
      :close-on-click-modal="false"
      :visible.sync="isShow"
      :title="'编辑学校'"
      :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="[7, 10]">
        <div slot="OI7" class="scoped-img-area">
          <div class="sia-title">更多图片</div>
          <div class="sia-op" v-for="(imgsrc,index) in imagesArr" :key="index">
            <img class="img" :src="imgsrc + '_adm0'" alt="img" @click="openbigImg(imgsrc + '_adm0')">
            <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 slot="OI10" class="scoped-other-form">
          <el-form-item label="点位坐标" class="scoped-item-two item">
            纬度N<el-input v-model="cObj.latitude" disabled />
            经度E<el-input v-model="cObj.longitude" disabled />
            <el-button type="primary" class="map-btn" size="small" @click="handleMap">点击从地图获取</el-button>
          </el-form-item>
        </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>
    <handle-map :is-show="isShowMap" @close="closeMap" />
    <popup-big-img :is-show="bigImgShow" :src="`${bigImgSrc}`" @close="closebigImg" />
  </div>
</template>
<script>
import handleMap from '@/components/Common/Map'
import bigImgPopup from '_m/bigImgPopup.js'
export default {
  components: { handleMap },
  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 {
      imagesArr: [],
      domainUrl,
      token,
      formData: [],
      loading: true,
      cObj: {},
      isShowMap: false
    }
  },
  watch: {
    isShow: function(val) {
      if (val) {
        this.imagesArr = []
        if (this.curObj.id) {
          this.$api.school.admschooldetail({id: this.curObj.id}).then(res => {
            let curData = res || {}
            this.cObj = curData || {}
            if (curData.images && curData.images.length > 0) this.imagesArr = curData.images.split(',')
            this.getDef()
          })
        } else {
          this.cObj = this.curObj
          this.getDef()
        }
      }
    },
  },
  methods: {
    imgDel (index) {
      this.imagesArr.splice(index, 1)
      this.imagesArr = [...this.imagesArr]
    },
    roomAreaUploadSuccess(res, file) {
      const data = res.data || {}
      let urls = this.imagesArr || []
      urls.push(`${data.url}`)
      this.imagesArr = 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 GIF 格式!')
      }
      if (!isLtM) {
        this.$message.error('上传图片大小不能超过 10M!')
      }
      return isJPGPNG && isLt2M
    },
    getDef (str) {
      let params = { ...this.cObj }
      if (str === 'school_type') {
        params = {...this.$refs.ruleForm.baseForm}
      }
      const disabled = false
      if (!params.pri_image) params.pri_image = 'http://img.honglounews.com/20210429034015-4091.png'
      let remoteOptionsHouse = []
      if (params.estate_list && params.estate_list.duikou && params.estate_list.duikou.length > 0) {
        params.estate_dk_list = params.estate_list.duikou.map(item => {
          remoteOptionsHouse.push({ keyRO: item.estate_name, valRO: item.id })
          return item.id
        })
      } else {
        params.estate_dk_list = []
      }
      if (params.estate_list && params.estate_list.guihua && params.estate_list.guihua.length > 0) {
        params.estate_gh_list = params.estate_list.guihua.map(item => {
          remoteOptionsHouse.push({ keyRO: item.estate_name, valRO: item.id })
          return item.id
        })
      } else {
        params.estate_gh_list = []
      }

      if (params.school_type === '2') {
        this.formData = [
          { label: '学校名称', key: 'school_name' },
          { label: '所属区域', key: 'area_type', type: 'select', class: 'c-2', options: this.$dictData.area_type },
          { label: '学校属性', key: 'school_attrib', type: 'select', class: 'c-2', options: this.$dictData.school_attrib },
          { label: '学校类型', key: 'school_type', type: 'select', class: 'c-2', options: this.$dictData.school_type, changeHandle: this.typeChange },
          // { label: '学校分类', key: 'school_cate', type: 'select', class: 'c-2', options: this.$dictData.school_cate },
          { label: '学校预警', key: 'school_warn', type: 'select', class: 'c-2', options: this.$dictData.school_warn },
          { label: '主图', key: 'pri_image', type: 'upload', class: 'c-2' },
          { label: '学区范围图', key: 'district_img', type: 'upload', class: 'c-2' },
          { label: '学区范围', key: 'district',type: 'textarea' },
          { label: '当前航拍key', key: 'vr_key', class: 'c-2'},
          { label: '航拍背景图', key: 'vr_image', class: 'c-2', type: 'cuImg',
            options: {
              w: 375,
              h: 250,
            }
          },
          { label: '对口楼盘', key: 'estate_dk_list', multiple: true, type: 'selectRemote',
            remoteParams: { skey: 'estate_name', api: `house.admestatelist`, opKey: 'estate_name', opVal: 'id' },
            remoteOptions: remoteOptionsHouse
          },
          { label: '规划楼盘', key: 'estate_gh_list', multiple: true, type: 'selectRemote',
            remoteParams: { skey: 'estate_name', api: `house.admestatelist`, opKey: 'estate_name', opVal: 'id' },
            remoteOptions: remoteOptionsHouse
          },
          { label: '学校地址', key: 'address' },
          { label: '历年成绩', key: 'achievement', type: 'textarea' },
          { label: '学校评价', key: 'remarked', type: 'textarea' },
          { label: '学费', key: 'tuition', class: 'c-2', type: 'inputFont', appendFont: '万元/年' },
          { label: '有无住宿', key: 'is_live', type: 'select', class: 'c-2', options: this.$dictData.sys_yesno2 },
        ]
      } else {
        this.formData = [
          { label: '学校名称', key: 'school_name' },
          { label: '所属区域', key: 'area_type', type: 'select', class: 'c-2', options: this.$dictData.area_type },
          { label: '学校属性', key: 'school_attrib', type: 'select', class: 'c-2', options: this.$dictData.school_attrib },
          { label: '学校类型', key: 'school_type', type: 'select', class: 'c-2', options: this.$dictData.school_type, changeHandle: this.typeChange },
          // { label: '学校分类', key: 'school_cate', type: 'select', class: 'c-2', options: this.$dictData.school_cate },
          { label: '学校预警', key: 'school_warn', type: 'select', class: 'c-2', options: this.$dictData.school_warn },
          { label: '主图', key: 'pri_image', type: 'cuImg',
            options: {
              w: 375,
              h: 250,
              SY: 1,
            }, class: 'c-2' },
          { label: '学区范围图', key: 'district_img', type: 'upload', class: 'c-2' },
          { label: '学区范围', key: 'district',type: 'textarea' },
          { label: '当前航拍key', key: 'vr_key', class: 'c-2'},
          { label: '航拍背景图', key: 'vr_image', class: 'c-2', type: 'cuImg',
            options: {
              w: 375,
              h: 250,
            }
          },
          { label: '对口楼盘', key: 'estate_dk_list', multiple: true, type: 'selectRemote',
            remoteParams: { skey: 'estate_name', api: `house.admestatelist`, opKey: 'estate_name', opVal: 'id' },
            remoteOptions: remoteOptionsHouse
          },
          { label: '规划楼盘', key: 'estate_gh_list', multiple: true, type: 'selectRemote',
            remoteParams: { skey: 'estate_name', api: `house.admestatelist`, opKey: 'estate_name', opVal: 'id' },
            remoteOptions: remoteOptionsHouse
          },
          { label: '学校地址', key: 'address'},
          { label: '历年成绩', key: 'achievement', type: 'textarea'},
          { label: '学校评价', key: 'remarked', type: 'textarea'},
        ]
      }
      params.pri_image = this.IMadd(params.pri_image)
      params.district_img = this.IMadd(params.district_img)
      this.setDefaultValue(params)
    },
    typeChange () {
      this.getDef('school_type')
    },
    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.id = this.curObj.id
            if (newForm.estate_dk_list && newForm.estate_dk_list.length > 0) {
              newForm.estate_dk_list = newForm.estate_dk_list.join(',')
            } else {
              newForm.estate_dk_list = ''
            }
            if (newForm.estate_gh_list && newForm.estate_gh_list.length > 0) {
              newForm.estate_gh_list = newForm.estate_gh_list.join(',')
            } else {
              newForm.estate_gh_list = ''
            }
            if (this.imagesArr && this.imagesArr.length > 0) {
              newForm.images = this.imagesArr.join(',')
            }
            newForm.longitude = this.cObj.longitude
            newForm.latitude = this.cObj.latitude
            newForm.pri_image = this.IMdel(newForm.pri_image)
            newForm.district_img = this.IMdel(newForm.district_img)
            if (!newForm.longitude) return this.$msgw('请选择经度！')
            else if (!newForm.latitude) return this.$msgw('请选择纬度！')
            let apiStr = 'admschooladd'
            if (newForm.id) apiStr = 'admschooledit'
            this.$api.school[apiStr](newForm).then(data => {
              this.$msgs(newForm.id ? '编辑成功' : '新增成功')
              this.$emit('close', newForm)
            })
          }
        })
      } else {
        this.$emit('close')
        this.setDefaultValue()
      }
    },
    handleMap() { // 定位
      this.isShowMap = true
      const pointObj = {
        latitude: this.cObj.latitude || '',
        longitude: this.cObj.longitude || '',
        address: this.cObj.address || ''
      }
      this.$root.$emit('handleMap', pointObj)
    },
    closeMap(obj) {
      if (obj) {
        const oldform = this.$refs.ruleForm.baseForm
        const newForm = { ...oldform, ...obj }
        this.cObj = newForm
        this.setDefaultValue(newForm)
      }
      this.isShowMap = false
    }
  }
}
</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-other-form {
  .scoped-item-two {
    .el-input {
      display: inline-block;
      width: 115px;
      margin: 0 10px;
      ::v-deep input {
        text-align: center;
      }
    }
  }
}
.map-btn{
  height: 36px;
}






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