<template>
  <el-dialog
    title="位置选择"
    :visible.sync="dialogVisible"
    width="800px"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    @close="handleClose"
  >
    <div class="map-container block-amap-box">
      <!-- <div class="searchBox">
        <el-input
          placeholder="请输入位置"
          v-model.lazy="searchAddr"
          style="width: auto"
          @change="searchFun"
          clearable
          class="input-with-select"
        >
          <el-button
            style="color: white"
            class="searchBtn"
            slot="append"
            icon="el-icon-search"
            @click="searchFun"
          >
            搜索
          </el-button>
        </el-input>
      </div> -->
      <el-form
        :inline="true"
        :model="locationForm"
        class="demo-form-inline"
        size="mini"
      >
        <el-form-item label="经度">
          <el-input v-model="locationForm.lng" disabled></el-input>
        </el-form-item>
        <el-form-item label="纬度">
          <el-input v-model="locationForm.lat" disabled></el-input>
        </el-form-item>
        <!-- <el-form-item>
          <el-button type="primary" size="mini" @click="locationSearch"
            >定位</el-button
          >
        </el-form-item> -->
        <el-form-item>
          <el-input
            @change="searchFun"
            clearable
            placeholder="请输入地区检索"
            v-model.lazy="searchAddr"
          >
            <i slot="suffix" class="el-input__icon el-icon-search"></i>
          </el-input>
        </el-form-item>
      </el-form>
      <div style="height: 70vh" id="searchAmapWrapper" tabindex="0"></div>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button
        @click="handleClose"
        :disabled="dialogSubmitLoading"
        :loading="dialogSubmitLoading"
        >取 消</el-button
      >
      <el-button
        :disabled="dialogSubmitLoading"
        type="primary"
        @click="submitForm"
        :loading="dialogSubmitLoading"
        >确定</el-button
      >
    </span>
  </el-dialog>
</template>
<script>
import { assign } from 'lodash-es';

export default {
  props: {
    data: {
      type: Object,
      default() {
        return {};
      }
    },
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      isLoading: false,
      dialogSubmitLoading: false,
      dialogVisible: false,
      searchAddr: '',
      searchOption: {},
      locationForm: {
        lng: '',
        lat: ''
      },
      formatAddress: ''
    };
  },
  watch: {
    visible(val) {
      if (val !== this.dialogVisible) {
        this.dialogVisible = val;

        if (val) {
          this.$nextTick(() => {
            this.initMap();
          });
        } else {
          this.resetForm();
        }
      }
    },
    data: {
      deep: true,
      handler(val) {}
    }
  },
  mounted() {},
  methods: {
    initMap() {
      var map = new AMap.Map('searchAmapWrapper', {
        center: this.center,
        resizeEnable: true,
        zoom: 15,
        lang: 'en'
      });

      AMap.plugin(
        [
          'AMap.ToolBar',
          'AMap.Scale',
          'AMap.MouseTool',
          'AMap.Geocoder',
          'AMap.GeometryUtil'
        ],
        () => {
          map.addControl(new AMap.ToolBar());
          map.addControl(new AMap.Scale());

          this.geocoder = new AMap.Geocoder({
            city: '全国',
            radius: 1000
          });
        }
      );

      this.map = map;

      this.map.on('click', (e) => {
        const { lng, lat } = e.lnglat;
        this.locationForm = {
          lng,
          lat
        };

        if (this.marker) {
          this.map.remove(this.marker);
        }

        this.marker = new AMap.Marker();
        this.marker.setPosition(e.lnglat);

        this.map.add(this.marker);
        this.map.setFitView(this.marker);

        this.formatAddress = '';
      });

      this.map.plugin(['AMap.DistrictSearch'], () => {});
    },
    submitForm() {
      this.dialogSubmitLoading = true;

      return new Promise((resolve) => {
        if (!this.formatAddress) {
          this.geocoder.getAddress(
            this.marker.getPosition(),
            (status, result) => {
              if (status === 'complete' && result.regeocode) {
                const { formattedAddress } = result.regeocode;

                this.formatAddress = formattedAddress;
              } else {
                this.$message.error('根据经纬度查询位置失败');
              }
              this.handleSuccess();
              resolve();
            }
          );
        } else {
          this.dialogSubmitLoading = false;
          resolve();
          this.handleSuccess();
        }
      });
    },
    handleSuccess() {
      this.dialogSubmitLoading = false;
      this.handleClose();
      this.$emit(
        'success',
        assign({ address: this.formatAddress }, this.locationForm)
      );
    },
    onSearchResult({ lng, lat }) {},
    handleClose() {
      this.$emit('update:visible', false);
    },
    handleInit() {},
    searchFun() {
      // console.log(this.marker)
      if (this.marker) {
        this.map.remove(this.marker);
      }
      if (!this.searchAddr) {
        return false;
      }
      this.marker = new AMap.Marker();
      this.geocoder.getLocation(this.searchAddr, (status, result) => {
        if (status === 'complete' && result.geocodes.length) {
          const lnglat = result.geocodes[0].location;

          this.locationForm.lng = lnglat.lng;
          this.locationForm.lat =
            result.geocodes[0] && result.geocodes[0].formattedAddress;
          this.marker.setPosition(lnglat);
          this.map.add(this.marker);
          this.map.setFitView(this.marker);
        } else {
          this.$message.error('根据地址查询位置失败');
        }
      });
    },
    resetForm() {
      this.locationForm = { lng: '', lat: '' };
      this.searchAddr = '';
      this.formatAddress = '';
    }
  }
};
</script>

<style lang="scss" scoped>
.amap-wrap {
  width: 100%;
  height: 100%;
}

.block-amap-box {
  position: relative;

  .searchBox {
    position: absolute;
    left: 30px;
    top: 20px;
    z-index: 9;
    display: flex;

    ::v-deep .el-input__inner {
      border-top-left-radius: 50px;
      border-bottom-left-radius: 50px;
      width: 280px;
    }
    ::v-deep .el-input.is-active .el-input__inner,
    ::v-deep .el-input__inner:focus {
      border-color: #3eb984;
      outline: 0;
    }
    ::v-deep .el-input-group__append {
      border-top-right-radius: 50px;
      border-bottom-right-radius: 50px;
      overflow: hidden;
      border: 1px solid #3eb984;
      background-color: #3eb984;
    }
    .searchBtn {
      background: #3eb984;
      color: #fff;
    }
    .drawBtn {
      background-color: #3eafb9;
      border-radius: 18px;
      color: #fff;
      border-color: #3eafb9;
      margin-left: 20px;
    }
  }

  &__edit-area {
    width: 0;
    height: 100%;
    opacity: 0;
    background-color: rgba(40, 43, 49, 0.9);
    font-size: 12px;
    color: #333;
    position: absolute;
    top: 0;
    right: -250px;
    z-index: 999;
    transition: all 1s;
    box-shadow: -6px 0 20px -5px rgba(0, 0, 0, 0.4);
    overflow: hidden;

    &.active {
      width: 240px;
      opacity: 1;
    }
  }

  &__title {
    width: 100%;
    color: #64c3a4;
    font-size: 16px;
    line-height: 40px;
    text-align: left;
    margin-top: 26px;
    font-weight: 700;
    text-align: center;
  }

  &__colors {
    width: 200px;
    background-color: rgba(245, 245, 245, 0.2);
    border: solid 1px rgba(255, 255, 255, 0.2);
    overflow: hidden;
    margin: 0 auto;
    padding: 10px 6px 10px 12px;
    border-radius: 3px;

    .color {
      width: 20px;
      height: 20px;
      float: left;
      position: relative;
      cursor: pointer;
      border-radius: 3px;
      margin: 0 2px;
      background: 0 0;
      margin: 0 9px 9px 0;

      &.active {
        &::after {
          content: '.';
          font-size: 0;
          display: inline-block;
          width: 26px;
          height: 26px;
          border: 1px solid #999;
          position: absolute;
          top: -4px;
          left: -4px;
        }
      }
    }
  }

  &__actions {
    padding: 20px;

    button {
      display: block;
      width: 100%;
      margin-bottom: 20px;
      margin-left: 0;
    }
  }
}

.map-container {
  width: 100%;

  position: relative;

  .search-box {
    position: absolute;
    top: 10px;
    left: 10px;
  }
}
</style>
