<template>
  <div>
    <el-form :model="ruleForm" :rules="rulesHetong" ref="heForm" label-width="130px" class="demo-ruleForm">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="商铺名称" prop="shopName">
            <el-input v-model="ruleForm.shopName" placeholder="请输入" maxlength="20"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="商铺类型" prop="shopType">
            <el-select v-model="ruleForm.shopType" placeholder="请选择" style="width: 100%;">
              <el-option label="移动铺位" value="1"></el-option>
              <el-option label="固定铺位" value="2"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="测绘面积" prop="surveyingArea">
            <el-input v-model.number="ruleForm.surveyingArea" placeholder="请输入" maxlength="10" @input="validateNumber('surveyingArea', $event)">
              <template slot="append">M<sup>2</sup></template>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="产权面积" prop="propertyArea">
            <el-input v-model.number="ruleForm.propertyArea" placeholder="请输入" maxlength="10" @input="validateNumber('propertyArea', $event)">
              <template slot="append">M<sup>2</sup></template>
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="实际使用面积" prop="actualUsableArea">
            <el-input v-model.number="ruleForm.actualUsableArea" placeholder="请输入" maxlength="10" @input="validateNumber('actualUsableArea', $event)">
              <template slot="append">M<sup>2</sup></template>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="所属区域" prop="belongingRegion">
            <el-select v-model="ruleForm.belongingRegion" placeholder="请选择" style="width: 100%;">
              <el-option v-for="(item,index) in regionOptions"
              :key="index" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="所属绿道段" prop="belongingGreenwaySection">
            <el-select v-model="ruleForm.belongingGreenwaySection" placeholder="请选择"
              style="margin-top:5px;width: 100%;" @change="lvdaoSel">
              <el-option :label="item.name" :value="item.code" v-for="(item,index) in lvdaoList"
                :key="index"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
		
        <el-col :span="12">
          <el-form-item label="门牌号" prop="houseNumber">
            <el-input v-model="ruleForm.houseNumber" placeholder="请输入" 
              maxlength="20"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="归属部门" prop="belongingDepartment">
            <el-input v-model="ruleForm.belongingDepartment" placeholder="请输入" maxlength="20">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="负责人" prop="head">
            <el-input v-model="ruleForm.head" placeholder="请输入" maxlength="10">
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="联系人" prop="contacts">
            <el-input v-model="ruleForm.contacts" placeholder="请输入" maxlength="50">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="联系方式" prop="telephone">
            <el-input v-model="ruleForm.telephone" placeholder="请输入" maxlength="20">
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="详细位置" prop="detailedLocation">
        <el-input :disabled="true" v-model="ruleForm.detailedLocation" placeholder="请地图标点选择位置" maxlength="100">
        </el-input>
      </el-form-item>
      <el-form-item label="地图标点" prop="mapPunctuation">
        <div style="width: 100%;height: 300px;">
          <newmap :isonloed="info.id?true:false" :lat="lat" :lng="lng" @updatecenter="updatecenter"></newmap>
        </div>
      </el-form-item>
      <el-form-item label="商铺描述" prop="shopDescription">
        <el-input maxlength="200" show-word-limit rows="4" v-model="ruleForm.shopDescription" placeholder="请输入"
          type="textarea" />
      </el-form-item>
      <el-form-item  label="建筑图纸" prop="architecturalDrawings">
        <upimg :cmpOption="{disabled:false,isSetCover:false}" filePath="sp" inputtype="architecturalDrawings" :value="ruleForm.architecturalDrawings"
          @changimg="e=>changimg(e,'architecturalDrawings')"></upimg>
      </el-form-item>
      <el-form-item label="宣传图" prop="displayMainImage">
        <upimg filePath="sp" inputtype="displayMainImage" :value="ruleForm.displayMainImage"
          @changimg="e=>changimg(e,'displayMainImage')" @changfmindex="changfmindex"></upimg>
      </el-form-item>
      <el-form-item label="其他视频" prop="otherImageVideos">
        <upfile filePath="sp" inputtype="otherImageVideos" :value="ruleForm.otherImageVideos" @changimg="e=>changimg(e,'otherImageVideos')"></upfile>
      </el-form-item>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item>
            <div style="margin-top: 20px">
              <el-button @click="add()" style="background-color: #3F9B6A;color: #fff;">确定</el-button>
              <el-button @click="close()" class="buttonHover"
                style="color: #606266;border: 1px solid #DBDBDB;background-color: #fff;">取消</el-button>
            </div>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
  import MapMark from "@/components/MapContainer/MapMark";
  import upimg from "@/components/ImageUpload/index"
  import upfile from "@/components/fujianUpload/fujianList"
  import {
    uploadUrl
  } from '@/utils/request'
  import {
    miniioupload
  } from '../../api/commodityLease.js'
  import {
    gongyuan,
    lvdaoduan,
    addList,
    editList
  } from '../../api/information.js';
  import newmap from "@/components/newmap/index";
  export default {
    components: {
      upfile,
      upimg,
      MapMark,
      newmap
    },
    computed: {
      regionOptions() {
        return this.$store.state.app.regionOptions;
      }
    },
    data() {
      return {
        lat: 30.67,
        lng: 104.06,
        uploadFileUrl: uploadUrl, // 请求地址
        ruleForm: {
          shopName: '', // 商铺名称
          venueNumber: '', // 场地编号
          houseNumber: '', // 门牌号
          architecturalForm: '', // 建筑形式
          actualUsableArea: '', // 实际使用面积
          belongingRegion: '', // 所属区域
          belongingDepartment: '', // 归属部门
          head: '', // 负责人
          detailedLocation: '', // 详细位置
          mapPunctuation: '', // 地图标点
          architecturalDrawings: '', // 建筑图纸
          shopDescription: '', // 商铺描述
          displayMainImage: '', // 展示主图
          otherImageVideos: '', // 其他图片视频
          rentalStatus: '', // 租赁状态
          publishStatus: '', // 发布状态
          leaseExpirationDate: '', // 租赁到期时间
          createDate: '', // 创建时间
          createUser: '', // 创建人
          // updateDate: '', // 修改时间
          // updateUser: '', // 修改人
          businessNature: '', // 经营性质
          telephone: '', // 联系电话
          identityCard: '', // 身份证号码
          belongingGreenwaySection: '', // 所属绿道段
          belongingParkTrail: '', // 所属公园/步道
          floorSpace: '', // 建筑面积
          contractNumber: '', // 合同编号
          shopType: '1', // 商铺类型
          surveyingArea: '', // 测绘面积
          propertyArea: '', // 产权面积
          contacts: '', // 联系人
          planningDrawings: '' // 规划图纸
        },
        rulesHetong: {
          shopName: [{
            required: true,
            message: '请输入商铺名称',
            trigger: 'blur'
          }, ],
          shopType: [{
            required: true,
            message: '请选择商铺类型',
            trigger: 'blur'
          }, ],
          belongingGreenwaySection: [{
            required: true,
            message: '请填写所属绿道段',
            trigger: 'blur'
          }, ],
          belongingRegion: [{
            required: true,
            message: '请选择所属区域',
            trigger: 'change'
          }],
          detailedLocation: [{
            required: true,
            message: '请地图标点选择位置',
            trigger: 'blur'
          }, ],
          mapPunctuation: [{
            required: true,
            message: '请选择地图标点',
            trigger: 'change'
          }],
          displayMainImage: [{
            required: true,
            message: '请上传宣传图',
            trigger: 'change'
          }]
        },
        uploadFiles: [],
        filesLength: 1,
        lvdaoList: [],
        gongyuanList: [],
        parentMessage: {},
        edit: true
      };
    },
    props: {
      info: {
        type: Object,
        default: {}
      }
    },
    async created() {
      console.error(this.info)
      if (this.info.id) {
        this.ruleForm = this.info
        if (this.ruleForm.mapPunctuation) {
          this.lat = Number(this.ruleForm.mapPunctuation.split(',')[0])
          this.lng = Number(this.ruleForm.mapPunctuation.split(',')[1])
        }
      }
      const lvdao = await lvdaoduan()
      this.lvdaoList = lvdao.data
    },
    methods: {
      validateNumber(field, value) {
      // 使用正则表达式确保输入的是数字，并且可以包含最多两位小数
        const regex = /^\d+(\.\d{1,2})?$/;
        if (!regex.test(value)) {
          this.ruleForm[field] = value.replace(/[^\d.]/g, '').replace(/(\.\d{2}).*$/, '$1');
        }
      },
      updatecenter(e) {
        console.error('-----------', e)
        this.ruleForm.detailedLocation = e.address
        this.lat = e.lat
        this.lng = e.lng
      },
      changfmindex(e) {
        console.error(e)
      },
      changimg(e, type) {
        this.ruleForm[type] = e
        console.error(this.ruleForm[type])
      },
      //获取当前时间
      updateCurrentTime() {
        const now = new Date();
        const year = now.getFullYear();
        const month = (now.getMonth() + 1).toString().padStart(2, '0');
        const day = now.getDate().toString().padStart(2, '0');
        const hours = now.getHours().toString().padStart(2, '0');
        const minutes = now.getMinutes().toString().padStart(2, '0');
        const seconds = now.getSeconds().toString().padStart(2, '0');

        return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
      },
      //获取地图点击的经纬度
      getChildDate(mapData) {
        this.mapData = mapData

      },
      lvdaoSel(vaule) {
        this.ruleForm.belongingParkTrail = ''
        this.gongyuan(vaule)
      },
      async gongyuan(value) {
        const gongyaun = await gongyuan()
        this.gongyuanList = gongyaun.data.filter(item => item.superior == value);
      },
      add() {
        console.log({
          ...this.ruleForm
        })
        if(this.lat) {
          this.ruleForm.mapPunctuation = this.lat + ',' + this.lng
        }
        
        // return
        if (this.ruleForm.id) {
          this.$refs.heForm.validate((valid) => {
            console.log(valid)
            if (valid) {
              this.ruleForm.updateDate = this.updateCurrentTime()
              this.ruleForm.updateUser = localStorage.getItem('roleName')
              editList(this.ruleForm).then(res => {
                if (res.code == 200) {
                  this.$message({
                    message: '修改成功',
                    type: 'success'
                  })
                  this.$emit('removeonaction', '1')
                } else {
                  this.$message({
                    message: res.msg,
                    type: 'error'
                  })
                }
              })
            } else {
              this.$message({
                message: '请填写完整信息',
                type: 'error'
              })
              return false;
            }


          })
        } else {
          this.$refs.heForm.validate((valid) => {
            console.log(valid)
            if (valid) {
              this.ruleForm.createDate = this.updateCurrentTime()
              this.ruleForm.rentalStatus = 0
              this.ruleForm.publishStatus = 0
              this.ruleForm.createUser = localStorage.getItem('roleName')
              addList(this.ruleForm).then(res => {
                if (res.code == 200) {
                  this.$message({
                    message: '添加成功',
                    type: 'success'
                  })
                  this.$emit('removeonaction', '1')
                } else {
                  this.$message({
                    message: res.msg,
                    type: 'error'
                  })
                }
              })
            } else {
              this.$message({
                message: '请填写完整信息',
                type: 'error'
              })
              return false;
            }
          })

        }
      },
      close() {
        this.$emit('removeonaction', '1')
      },
      hefuRemove(e) {
        // this.ruleForm[e] = ''
      },
      hefuSuccess(response, e) {
        console.error(response)
        console.error(e)
        this.ruleForm[e] = ''
        let fd = new FormData()
        fd.append('file', response.raw)
        fd.append('filePath', 'sp')
        miniioupload(fd).then(res => {
          console.error(res)
          this.ruleForm[e] = res.data
        })
      }


    },
  };
</script>
<style lang="scss">
  .el-dialog__header {
    background-color: #F2F3F5;
    text-align: left;
  }

  .el-dialog__title {
    line-height: 30px;
    font-size: 15px;
    color: #303133;
  }

  .avatar-uploader .el-upload {
    border: 1px dashed #3F9B6A;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #3F9B6A;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
