<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="[2]">
        <div slot="OI2" 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: {},
      metroName: ''
    }
  },
  watch: {
    isShow: function(val) {
      if (val) {
        if (this.curObj.id) {
          this.$api.other.admmetroinfodetail({
            id: this.curObj.id
          }).then(res => {
            this.cObj = {...res}
            this.metroName = res.metro_name
            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(pObj) {
      let params = { ...this.cObj, ...pObj }
      let nArr = []
      if (params.metro_line) {
        const mtArr = this.$dictData.metro_type || []
        mtArr.forEach(item => {
          if ((item.option1).indexOf(params.metro_line) > -1) {
            nArr.push(item)
          }
        })
      }
      this.formData = [
        { label: '线路', key: 'metro_line', class: 'c-2', type: 'select', options: this.$dictData.metro_line, rules: 1, changeHandle: this.metroLineChange},
        { label: '站名', key: 'metro_type', class: 'c-2', type: 'select', options: nArr, rules: 1, changeHandle: this.metroTypeChange},
        { label: '排序', key: 'sort'},
      ]
      this.setDefaultValue(params)
    },
    metroTypeChange (val, item, op) {
     this.metroName = op.key
    },
    metroLineChange (val) {
      this.getDef({metro_line: val, metro_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
            let apiStr = 'admmetroinfoadd'
            if (newForm.id) {
              apiStr = 'admmetroinfoedit'
            }
            newForm.metro_name = this.metroName
            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.getDef({sort: Number(newForm.sort)+1})
              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>
