<template>
  <el-dialog v-if="isShow"
             :model-value="isShow"
             title="编辑数据"
             :width="700"
             :close-on-click-modal="false"
             :before-close="handleClose"
  >
    <div id="addData">
      <el-form ref="form" :inline="true" :model="formData" :rules="formDataRules" style="margin: 0 15px"
               :label-width="110">
        <el-form-item label="ID：" v-show="false">
          <el-input v-model="formData.id"/>
        </el-form-item>
        <el-form-item label="标题：" prop="title" style="width: 100%">
          <el-input v-model="formData.title" placeholder="请输入标题" :disabled="formData.isDataView"/>
        </el-form-item>
        <el-form-item label="描述：" prop="content" style="width: 100%">
          <el-input v-model="formData.content" placeholder="请输入描述" :disabled="formData.isDataView"/>
        </el-form-item>
        <el-form-item label="类型：" prop="modelType" style="width: 100%">
          <el-input v-model="formData.modelType" placeholder="请输入类型" :disabled="formData.isDataView"/>
        </el-form-item>
        <el-form-item label="类别：" prop="menuId">
          <el-select v-model="formData.menuId" placeholder="请选择类别" @change="handleMenuIdSelect" :disabled="formData.isDataView">
            <el-option
                v-for="item in category"
                :key="item.id"
                :label="item.title"
                :value="item.id"
            />
          </el-select>
        </el-form-item>
        <br/>
        <el-form-item label="经度：" style="width: 40%" prop="lon">
          <el-input :model-value="formData.lon" placeholder="经度" :disabled="formData.isDataView">
          </el-input>
        </el-form-item>
        <el-form-item label="纬度：" style="width: 35%" :label-width="70" prop="lat">
          <el-input :model-value="formData.lat" placeholder="纬度" :disabled="formData.isDataView">
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button style="font-size: 18px" @click="handleLonlatSelect" >
            <el-icon><ElIconLocation/></el-icon>
          </el-button>
        </el-form-item>
        <br/>
        <el-form-item label="模型数据：" prop="url" style="width: 100%">
          <el-input v-model="formData.url" placeholder="请设置模型地址" :disabled="formData.isDataView">
            <template #append>
              <el-button @click="handleMapShow(true)">
                查看模型
              </el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="地名地址库：" prop="address" style="width: 100%">
          <el-input v-model="formData.address" placeholder="请输入地名地址库名称" :disabled="formData.isDataView"/>
        </el-form-item>
      </el-form>
      <HighOptionData :init-data="[]" :call-back="setProperties"></HighOptionData>
    </div>
    <template #footer>
      <el-button type="primary" @click="submit" v-show="!formData.isDataView">保存</el-button>
    </template>
  </el-dialog>
  <CesiumMap v-if="isMapShow"
             :is-show="isMapShow"
             :item="formData"
             :close="handleMapShow">
  </CesiumMap>

  <OlMap v-if="isLonLatShow"
         :is-show="isLonLatShow"
         :is-view="formData.isDataView"
         :geometry-str="formData.geometryStr"
         :menuId="1"
         :callback="setLonLatData"
         :close="handleLonlatSelect">
  </OlMap>

  <JsonDataView v-if="isJsonDataView"
                :is-show="isJsonDataView"
                :title="'地理数据预览'"
                :data="formData.geometryStr"
                :close="handleJsonDataView"
  >
  </JsonDataView>
</template>

<script>
import CesiumMap from "@/components/map/CesiumMap";
import OlMap from "@/components/map/OlMap";
import JsonDataView from "@/components/json/JsonDataView";
import api from "@/api/data/ModelData";
import menuApi from "@/api/data/MenuData";
import HighOptionData from "@/components/dataManage/dataDetail/HighOptionData"

export default {
  data() {
    return {
      formData: {},
      oriFormData: {},
      formDataRules: {
        title: [
          {required: true, message: '请输入标题', trigger: 'blur'},
          {required: true, message: '请输入标题', trigger: 'change'},
        ],
        content: [
          {required: true, message: '请输入描述', trigger: 'blur'},
          {required: true, message: '请输入描述', trigger: 'change'},
        ],
        modelType: [
          {required: true, message: '请输入类型', trigger: 'blur'},
          {required: true, message: '请输入类型', trigger: 'change'},
        ],
        menuId: [
          {required: true, message: '请选择类别', trigger: 'blur'},
          {required: true, message: '请选择类别', trigger: 'change'},
        ],
        geometryStr: [
          {required: true, message: '请输入Geojson数据', trigger: 'blur'},
        ],
        url: [
          {required: true, message: '该字段不能为空', trigger: 'blur'},
          {required: true, message: '该字段不能为空', trigger: 'change'},
        ],
        lon: [
          {required: true, message: '该字段不能为空', trigger: 'blur'},
          {required: true, message: '该字段不能为空', trigger: 'change'},
        ],
        lat: [
          {required: true, message: '该字段不能为空', trigger: 'blur'},
          {required: true, message: '该字段不能为空', trigger: 'change'},
        ]
        // address: [
        //   { required: true, message: '请输入地名地址库名称', trigger: 'change' },
        // ]
      },
      category: [],
      isMapShow: false,
      isLonLatShow: false,

      isJsonDataView: false,

    }
  },
  props: {
    isShow: Boolean,
    item: Object,
    close: Function
  },
  watch: {

  },
  mounted() {
    this.getMenuData();
    this.formData = this.item;
    this.oriFormData = JSON.parse(JSON.stringify(this.formData))
  },
  components: {
    CesiumMap,
    OlMap,
    JsonDataView,
    HighOptionData,
  },
  methods: {
    setProperties(item) {
      this.formData['properties'] = item;
    },
    handleMapShow(flag) {
      if (flag) {
        if (this.formData.url && this.formData.url !== '') {
          this.isMapShow = true
        } else {
          this.$message({message: '请输入模型地址', type: 'warning'})
        }
      } else {
        this.isMapShow = false
      }
    },
    handleLonlatSelect(flag) {
      if (flag) {
        this.formData.geometryStr = JSON.stringify(this.covertToGeometry())
        this.isLonLatShow = true
      } else {
        this.isLonLatShow = false
      }
    },
    covertToGeometry() {
      let geometry = {
        type: 'Point',
        coordinates: []
      }
      if (this.formData.lon && this.formData.lon!=='' && this.formData.lat && this.formData.lat !=='') {
        geometry.coordinates.push(this.formData.lon)
        geometry.coordinates.push(this.formData.lat)
        return geometry;
      } else {
        return '';
      }
    },
    setLonLatData(obj) {
      if (obj.coordinates && obj.coordinates.length>1) {
        this.formData.lon = obj.coordinates[0];
        this.formData.lat = obj.coordinates[1];
      }
    },
    getMenuData() {
      let app = this;
      let params = {
        type: "1",
        parentId: this.$constant.menuIds['model'],
      }
      menuApi.getMenuData(params).then(res => {
        if (res.code === 200) {
          app.category = res.content
        }
      })
    },
    setMapData(geometry) {
      this.formData.geometryStr = JSON.stringify(geometry)
    },
    handleClose() {
      this.close();
    },
    submit() {
      let app = this;
      app.$refs.form.validate(valid => {
        if (valid) {
          if (app.formData.isEdit) {
            app.updateData();
          } else {
            app.addData();
          }
        } else {
          app.$message({message: '请完善数据', type: 'warning'})
        }
      })
    },
    // 数据录入
    addData() {
      let app = this;
      let params = JSON.parse(JSON.stringify(app.formData));
      params['menuNameOne'] = '模型数据'
      params['userName'] = app.$store.state.userInfo.username;
      api.addData(params).then(res => {
        if (res.code === 200) {
          app.$message({message: '录入成功', type: 'success'})
          app.close(true);
        }
      })
    },
    // 编辑数据
    updateData() {
      let app = this;
      let params = JSON.parse(JSON.stringify(app.formData));
      delete params.createDate;
      delete params.updateDate;
      params['userName'] = app.$store.state.userInfo.username;
      api.updateData(params).then(res => {
        if (res.code === 200) {
          app.$message({message: '修改成功', type: 'success'})
          app.close(true);
        }
      })
    },
    handleJsonDataView(flag) {
      this.isJsonDataView = flag;
    },
    handleMenuIdSelect(val) {
      for (let i = 0; i < this.category.length; i++) {
        let item = this.category[i];
        if (item.id == val) {
          this.formData['menuNameTwo'] = item.title
          break;
        }
      }
    },
  }
}
</script>

<style>
#addData {
  width: 100%;
  height: 100%;
}
</style>