<template>
  <el-dialog v-if="isShow"
             :model-value="isShow"
             :title="dialogTitle"
             :width="700"
             :close-on-click-modal="false"
             :before-close="handleClose"
  >
    <div id="addData">
      <el-form ref="form" :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" >
          <el-input v-model="formData.title" placeholder="请输入标题" :disabled="formData.isDataView" />
        </el-form-item>
        <el-form-item label="描述：" prop="content">
          <el-input v-model="formData.content" placeholder="请输入描述" :disabled="formData.isDataView" />
        </el-form-item>
        <el-form-item label="类别：" prop="menuId">
          <el-select v-model="formData.menuId" placeholder="请选择类别" @change="handleMenuIdSelect" :disabled="true" >
            <el-option
                v-for="item in category"
                :key="item.id"
                :label="item.title"
                :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="地理信息：" prop="geometryStr" >
          <el-input v-model="formData.geometryStr" placeholder="请设置地理信息" :disabled="true" >
            <template #prepend>
              <el-button @click="handleMapShow(true)" type="primary" class="inputButtonStyle">
                <span v-if="formData.isDataView">查看</span>
                <span v-else>设置</span>
                地理信息
              </el-button>
            </template>
            <template #append>
              <el-button @click="handleJsonDataView(true)" class="inputButtonStyle">
                查看GeoJson
              </el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="地名地址库：" prop="address" >
          <el-input v-model="formData.address" placeholder="请输入地名地址库名称" :disabled="formData.isDataView" />
        </el-form-item>
        <HighOptionData :init-data="formData.properties" :call-back="setProperties"></HighOptionData>
      </el-form>
    </div>
    <template #footer>
      <el-button type="primary" @click="submit" v-show="!formData.isDataView">保存</el-button>
    </template>
  </el-dialog>
    <OlMap v-if="isMapShow"
           :is-show="isMapShow"
           :is-view="formData.isDataView"
           :type="currCategory.geometryType"
           :geometryStr="formData.geometryStr"
           :callback="setMapData"
           :close="handleMapShow">
    </OlMap>

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

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

export default {
  data() {
    return {
      formData: {},
      oriFormData: {},
      formDataRules: {
        title: [
          { required: true, message: '请输入标题', trigger: 'change' },
        ],
        content: [
          { required: true, message: '请输入描述', trigger: 'change' },
        ],
        menuId: [
          { required: true, message: '请选择类别', trigger: 'change' },
        ],
        geometryStr: [
          { required: true, message: '请输入Geojson数据', trigger: 'blur' },
        ],
        // address: [
        //   { required: true, message: '请输入地名地址库名称', trigger: 'change' },
        // ]
      },
      category: [],
      isMapShow: false,

      isJsonDataView: false,

    }
  },
  props: {
    dialogTitle: String,
    isShow: Boolean,
    item: Object,
    currCategory: Object,
    close: Function
  },
  watch: {
  },
  mounted() {
    this.getMenuData()
    this.formData = this.item;
    if (!this.formData.menuId || this.formData.menuId == '') {
      this.formData.menuId = this.currCategory.id
    }
    this.formData.menuId = this.currCategory.id
    this.oriFormData = JSON.parse(JSON.stringify(this.formData))
  },
  components: {
    OlMap,
    JsonDataView,
    HighOptionData,
  },
  methods: {
    setProperties(item) {
      this.formData['properties'] = item;
    },
    handleMapShow(flag) {
      if (flag) {
        if (this.formData.menuId) {
          this.isMapShow = true
        } else {
          this.$message({message: '请先选择类别', type: 'warning'})
        }
      } else {
        this.isMapShow = false
      }
    },
    getMenuData() {
      let app = this;
      let params = {
        type: "1",
        parentId: this.$constant.menuIds['twoD'],
      }
      menuApi.getMenuData(params).then(res=>{
        if (res.code===200) {
          app.category = res.content
          if (!app.formData.menuId || app.formData.menuId == '') {
            app.formData.menuId = app.currCategory.id
            app.handleMenuIdSelect(app.formData.menuId)
          }
        }
      })
    },
    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));
      params['userName'] = app.$store.state.userInfo.username;
      delete params.createDate;
      delete params.updateDate;
      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>