
<style scoped>
.user-panel {
  margin: 10px auto;
}
</style>
<template>
  <el-dialog
    :visible.sync="showDialog"
    :title="title"
    :modal-append-to-body="false"
    style="text-align: left"
    @close="closeDialog"
    :close-on-click-modal="false"
  >
    <div class="user-panel" v-loading="loading">
      <el-form
        ref="form"
        :model="formModel"
        :rules="ruleValidate"
        :label-width="'100px'"
      >
        <el-form-item label="地区名称" prop="name">
          <el-input
            v-model="formModel.name"
            placeholder="请输入地区名称"
            style="width: 300px"
          ></el-input>
        </el-form-item>
        <el-form-item label="全称" prop="fullName">
          <el-input
            v-model="formModel.fullName"
            placeholder="请输入全称"
            style="width: 300px"
          ></el-input>
        </el-form-item>
        <el-form-item label="上级" prop="parentId">
          <el-select-tree
            :props="props"
            :options="regionData"
            v-model="formModel.parentId"
            size="mediumn"
            width="300px"
          ></el-select-tree>
        </el-form-item>
        <el-form-item label="经纬度" prop="location">
          <el-input
            v-model="formModel.location"
            placeholder="请输入经纬度"
            style="width: 300px"
          ></el-input>
          <i class="el-icon-map-location my-font" @click="handleLocation()"></i>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="formModel.remark"
            placeholder="请输入备注"
            style="width: 300px"
          ></el-input>
        </el-form-item>
        <el-form-item label="排序" prop="sortNo">
          <el-input-number
            v-model="formModel.sortNo"
            placeholder="请输入排序"
            style="width: 200px"
            :min="0"
          ></el-input-number>
        </el-form-item>
      </el-form>
      <el-dialog
        :visible.sync="showMapDialog"
        title="地图选点"
        :modal-append-to-body="true"
        :append-to-body="true"
        style="text-align: left"
        :close-on-click-modal="false"
      >
        <el-amap
          ref="stationMap"
          vid="stationMap"
          :amap-manager="amapManager"
          :center="pointPosition"
          :zoom="zoom"
          :events="events"
          class="amap-demo"
        >
          <el-amap-marker
            vid="amapDemo"
            :position="pointPosition"
          ></el-amap-marker>
        </el-amap>
        <span slot="footer" class="dialog-footer">
          <div style="position: absolute; left: 20px; bottom: 20px">
            <el-input
              placeholder="当前经纬度"
              v-model="formModel.location"
              style="width: 400px"
            >
              <template slot="prepend">经纬度</template>
              <el-button
                slot="append"
                icon="el-icon-search"
                @click="handleRegeo"
                >定位</el-button
              >
            </el-input>
          </div>
          <el-button @click="showMapDialog = false">取 消</el-button>
          <el-button type="primary" @click="handleSelectPoint()"
            >确 定</el-button
          >
        </span>
      </el-dialog>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" @click="handleSubmit" :loading="submitting"
        >确 定</el-button
      >
    </span>
  </el-dialog>
</template>
<script>
import Constant from "@/constant";
import regionInfoApi from "@/api/base/regionInfo";
import SelectTree from "@/components/SelectTree";

import AMap from "vue-amap";
let amapManager = new AMap.AMapManager();

export default {
  props: ["businessKey", "title"],
  data() {
    return {
      formModel: {},
      ruleValidate: {
        name: [
          { required: true, message: "地区名称不能为空", trigger: "blur" },
        ],
        fullName: [
          { required: true, message: "全称不能为空", trigger: "blur" },
        ],
      },
      showDialog: true,
      loading: false,
      submitting: false,
      regionData: [],
      props: {
        // 配置项（必选）
        value: "id",
        label: "name",
        children: "children",
      },
      pointPosition: [112.240222, 30.337053],
      showMapDialog: false,
      amapManager,
      zoom: 12,
      events: {
        click: (e) => {
          var pt = e.lnglat; //点击选择新地址为中心点
          console.log(pt);
          this.pointPosition = [pt.lng, pt.lat];
          this.formModel.location = pt.lng + "," + pt.lat;
          this.formModel.longitude = pt.lng;
          this.formModel.latitude = pt.lat;
        },
      },
    };
  },
  created() {
    regionInfoApi.list().then((response) => {
      var jsonData = response.data;
      this.regionData = jsonData.data;
    });
  },
  methods: {
    closeDialog() {
      this.$emit("close", false);
    },
    handleSubmit() {
      var self = this;

      this.$refs["form"].validate((valid) => {
        if (valid) {
          (function () {
            var id = self.formModel.id;

            if (id == null || id.length == 0) {
              return regionInfoApi.add(self.formModel);
            } else {
              return regionInfoApi.update(self.formModel);
            }
          })().then(function (response) {
            var jsonData = response.data;

            if (jsonData.result) {
              self.$message({
                message: "保存成功!",
                type: "success",
              });

              self.$emit("close", true);
            } else {
              self.$message({
                message: jsonData.message + "",
                type: "warning",
              });

              self.$emit("close", false);
            }
          });
        }
      });
    },
    handleLocation() {
      this.showMapDialog = true;
      if (this.formModel.longitude != null && this.formModel.latitude != null) {
        var arr = [this.formModel.longitude, this.formModel.latitude];
        this.pointPosition = arr;
      }
    },

    handleRegeo() {
      if (this.formModel.longitude != null && this.formModel.latitude != null) {
        var arr = [this.formModel.longitude, this.formModel.latitude];
        this.pointPosition = arr;
      }
    },
    handleSelectPoint() {
      this.formModel.location =
        this.pointPosition[0] + "," + this.pointPosition[1];
      this.formModel.longitude = this.pointPosition[0];
      this.formModel.latitude = this.pointPosition[1];
      this.showMapDialog = false;
    },
  },
  mounted: function () {
    var self = this;

    (function () {
      if (self.businessKey.length == 0) {
        return regionInfoApi.create();
      } else {
        return regionInfoApi.edit(self.businessKey);
      }
    })()
      .then((response) => {
        var jsonData = response.data;
        self.loading = false;

        if (jsonData.result) {
          self.formModel = jsonData.data;
          if (self.formModel.sortNo == null) {
            self.formModel.sortNo = 0;
          }
          if (
            self.formModel.longitude != null &&
            self.formModel.latitude != null
          ) {
            self.formModel.location =
              self.formModel.longitude + "," + self.formModel.latitude;
          }
        } else {
          self.$message.error(jsonData.message + "");
        }
      })
      .catch((error) => {
        self.$message.error(error + "");
      });
  },
  components: {
    "el-select-tree": SelectTree,
  },
};
</script>
<style scoped>
.user-panel {
  margin: 10px auto;
}

.amap-demo {
  width: 100%;
  height: 400px;
}

.my-font {
  font-size: 20px;
  cursor: pointer;
  margin-right: 10px;
}
</style>