<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="100px" :insertSlotArr="[4]">
        <div slot="OI4" class="scoped-other-form">
          <el-form-item label="点位坐标" class="scoped-item-two">
            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="openMap">点击从地图获取</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" />
  </div>
</template>
<script>
import handleMap from '@/components/Common/Map'
export default {
  components: {
    handleMap
  },
  mixins,
  props: {
    isShow: Boolean,
    curObj: Object
  },
  inject: ['parentData'],
  data() {
    return {
      isShowMap: false,
      formData: [],
      loading: true,
      cObj: {},
    }
  },
  watch: {
    isShow: function(val) {
      if (val) {
        if (this.curObj.id) {
          this.$api.other.admareainfodetail({
            id: this.curObj.id
          }).then(res => {
            this.cObj = {...res}
            this.getDef()
          })
        } else {
          this.getDef()
        }
      }
    },
  },
  methods: {
    openMap() { // 定位
      this.isShowMap = true
      const pointObj = {
        latitude: this.cObj.latitude || '',
        longitude: this.cObj.longitude || '',
      }
      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
    },
    getDef() {
      let params = { ...this.cObj }
      let disabled = false
      if (this.cObj.id) disabled = true
      let roVals = []
      let newsIds = []
      if (params.information_list && params.information_list.length > 0) {
        params.information_list.forEach(item => {
          roVals.push({ keyRO: item.title, valRO: item.id })
          newsIds.push(item.id)
        })
        params.newsIds = [...newsIds]
      }
      this.formData = [
        { label: '区域名', key: 'area_name'},
        { label: '区域图', key: 'pri_image', type: 'uploads', class: 'c-2' },
        { label: '背景图', key: 'bg_image', type: 'uploads', class: 'c-2' },
        { label: '上级区域', key: 'parent_id', type: 'selectRemote', class: 'c-2', disabled,
          remoteParams: { skey: 'area_name', api: `other.admareainfolist`, opKey: 'area_name', opVal: 'id' },
          remoteOptions: [{ keyRO: params.parent_name, valRO: params.parent_id }]
        },
        { label: '关联文章', key: 'newsIds', type: 'selectRemote', multiple: true,
          remoteParams: { skey: 'title', api: `house.adminformationlist`, opKey: 'title', opVal: 'id' },
          remoteOptions: roVals
        },
        { label: '描述', key: 'remark', type: 'textarea'},
        { label: '预设1', key: 'option1', class: 'c-2'},
        { label: '预设2', key: 'option2', class: 'c-2'},
      ]
      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.id = this.curObj.id
            let apiStr = 'admareainfoadd'
            if (newForm.id) {
              apiStr = 'admareainfoedit'
              delete newForm.parent_id
            }
            if (newForm.newsIds && newForm.newsIds.length > 0) {
              newForm.information_id_list = newForm.newsIds.join(',')
            }
            delete newForm.newsIds
            newForm.longitude = this.cObj.longitude
            newForm.latitude = this.cObj.latitude
            if (!newForm.longitude) return this.$msgw('请选择经度！')
            else if (!newForm.latitude) return this.$msgw('请选择纬度！')
            this.$api.other[apiStr](newForm).then(data => {
              this.$msgs(newForm.id ? '编辑成功' : '新增成功')
              this.$emit('close', newForm)
            })
          }
        })
      } else {
        this.$emit('close')
        this.cObj = {}
        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%;
  }
}
::v-deep .img-upload {
  width: 182px;
  height: 110px;
  overflow: hidden;
  .icon {
    width: 180px;
    height: 100px;
    line-height: 100px;
  }
  .img {
    width: 180px;
    height: 110px;
  }
}


.scoped-other-form {
  .map-btn{
    display: inline-block;
    vertical-align: bottom;
    height: 36px;
  }
  .scoped-item-two {
    .el-input {
      display: inline-block;
      vertical-align: bottom;
      width: 140px;
      margin: 0 10px;
    }
  }
}
</style>
