<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="[9]">
        <div slot="OI9" 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" />
  </div>
</template>
<script>
import handleMap from '@/components/Common/Map'
export default {
  components: { handleMap },
  mixins,
  props: {
    isShow: Boolean,
    curObj: Object
  },
  inject: ['parentData'],
  data() {
    return {
      formData: [],
      loading: true,
      cObj: {},
      isShowMap: false
    }
  },
  watch: {
    isShow: function(val) {
      if (val) {
        if (val) {
          if (this.curObj.id) {
            this.$api.school.admschooldetail({id: this.curObj.id}).then(res => {
              let curData = res || {}
              this.cObj = curData || {}
              this.getDef()
            })
          } else {
            this.cObj = this.curObj
            this.getDef()
          }
        }
      }
    },
  },
  methods: {
    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.length > 0) {
        params.estate_id_list = params.estate_list.map(item => {
          remoteOptionsHouse.push({ keyRO: item.estate_name, valRO: item.id })
          return item.id
        })
      } else {
        params.estate_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: 'estate_id_list', multiple: true, type: 'selectRemote',
            remoteParams: { skey: 'estate_name', api: `house.admestatelist`, opKey: 'estate_name', opVal: 'id' },
            remoteOptions: remoteOptionsHouse
          },
          { label: '学校地址', key: 'address' },
          { 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: 'upload', class: 'c-2' },
          { label: '学区范围图', key: 'district_img', type: 'upload', class: 'c-2' },
          { label: '学区范围', key: 'district',type: 'textarea' },
          { label: '所属楼盘', key: 'estate_id_list', multiple: true, type: 'selectRemote',
            remoteParams: { skey: 'estate_name', api: `house.admestatelist`, opKey: 'estate_name', opVal: 'id' },
            remoteOptions: remoteOptionsHouse
          },
          { label: '学校地址', key: 'address' },
        ]
      }
      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_id_list && newForm.estate_id_list.length > 0) {
              newForm.estate_id_list = newForm.estate_id_list.join(',')
            } else {
              newForm.estate_id_list = ''
            }
            newForm.longitude = this.cObj.longitude
            newForm.latitude = this.cObj.latitude
            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;
}
</style>
