<template>
  <el-dialog
    v-if="visible"
    :title="form.id?'编辑民宿':'新建民宿'"
    :visible.sync="visible"
    width="1200px"
  >
    <el-form v-loading="isLoading" :model="form" label-width="100px" label-position="top">
      <el-row :gutter="24">
        <el-col :span="12">
          <el-row :gutter="24">
            <el-col :span="12">
              <el-form-item label="名称">
                <el-input v-model="form.baseDataInfo.name" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="客户数量">
                <el-input-number v-model="form.totalRoomCount" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="图片">
            <el-upload
              :file-list="fileList"
              action="#"
              list-type="picture-card"
              name="fileData"
              :http-request="uploaderHandle"
              :on-success="uploadSuccess"
              :on-remove="handleRemove"
              :auto-upload="true"
            >
              <i slot="default" class="el-icon-plus" />
            </el-upload>
          </el-form-item>
          <el-form-item label="地址">
            <el-input v-model="form.baseDataInfo.address" />
          </el-form-item>
          <el-row :gutter="24">
            <el-col :span="12">
              <el-form-item label="联系人">
                <el-input v-model="form.baseDataInfo.contacts" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="联系电话">
                <el-input v-model="form.baseDataInfo.contactNumber" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-col>
        <el-col :span="12">
          <el-form-item label="标注位置">
            <tencent-map id="amapcontainer" :position="[lat,lng]" @click="mapClick" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="简介">
        <el-input v-model="form.baseDataInfo.brief" type="textarea" rows="10" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" @click="handleSubmit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>

import { uploaderHandle } from '@/api/upload'
import { save, updateById } from '@/api/bz/base/homeStay'
import TencentMap from '@/components/TencentMap'

export default {
  components: { TencentMap },
  data() {
    return {
      visible: false,
      isLoading: false,
      listAllAuthorities: [],
      form: {
        baseDataInfo: {}
      },
      fileList: [],
      map: null,
      marker: null,
      lng: null,
      lat: null
    }
  },
  mounted() {
  },
  methods: {
    uploaderHandle,
    handleRemove(file, fileList) {
      this.fileList = fileList
    },
    uploadSuccess(res, file, fileList) {
      file.url = res.url
      this.fileList = fileList
    },
    open(data) {
      this.visible = true
      this.isLoading = false
      this.fileList = []
      this.form = {
        baseDataInfo: {}
      }
      if (data && data.baseDataInfo) {
        const baseDataInfo = data.baseDataInfo
        if (baseDataInfo.litpics) {
          baseDataInfo.litpics.forEach(o => {
            this.fileList.push({ url: o })
          })
        }

        this.lng = baseDataInfo?.lng
        this.lat = baseDataInfo?.lat

        this.form = Object.assign({}, data)
      }
    },
    mapClick(e) {
      this.lng = e.latLng.lng
      this.lat = e.latLng.lat
    },
    handleSubmit() {
      const litpics = []
      this.fileList.forEach(o => {
        litpics.push(o.url)
      })
      this.isLoading = true
      const param = { ...this.form, baseDataInfo: { ...this.form.baseDataInfo, lng: this.lng, lat: this.lat, litpics }}
      console.log(param)
      if (this.form.id) {
        updateById(this.form.id, param).then(() => {
          this.visible = false
          this.$emit('ok')
        }).finally(() => {
          this.isLoading = false
        })
      } else {
        save(param).then(res => {
          this.visible = false
          this.$emit('ok')
        }).finally(() => {
          this.isLoading = false
        })
      }
    }
  }
}
</script>
