<template>
  <div>
    <el-form :model="formInline" :rules="leixing?rules:rulesSt" ref="formInline" label-width="100px"
      class="demo-ruleForm">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="广告位名称" prop="advertisingName">
            <el-input v-model="formInline.advertisingName" placeholder="请输入" style="margin-top:5px;"
              maxlength="50"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="广告位编号" prop="advertisingSpaceNumber">
            <el-input v-model="formInline.advertisingSpaceNumber" disabled placeholder="将根据类型自动为资源编号"
              style="margin-top:5px;"></el-input>
          </el-form-item>
        </el-col>

      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="广告位类型" prop="advertisingType">
            <el-select @change="changeadvertisingType" v-model="formInline.advertisingType" placeholder="请选择" style="margin-top:5px;width: 100%;">
              <el-option v-for="(item, index) in typelist" :key="index" :label="item.name" :value="item.vaule"></el-option>
              <!-- <el-option label="实体广告位" value="实体广告位" v-else></el-option> -->
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="所属端" prop="affiliation" v-if="ontype =='线上广告位'">
            <el-select v-model="formInline.affiliation" placeholder="请选择" style="margin-top:5px;width: 100%;">
              <el-option label="游客小程序" value="游客小程序"></el-option>
              <el-option label="商家客户端" value="商家客户端"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="详细位置" prop="detailedLocation" v-if="ontype =='实体广告位'">
            <el-input v-model="formInline.detailedLocation" placeholder="请输入" style="margin-top:5px;"
              maxlength="200"></el-input>
            <!-- </el-select> -->
          </el-form-item>
        </el-col>

      </el-row>
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="地图标点" prop="mapPunctuation" v-if="ontype =='实体广告位'">
            <div style="width: 100%;height: 300px;">
              <map-mark :sendChildDate="getChildDate"></map-mark>
            </div>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="轮播顺序" prop="rotationOrder" v-if="ontype =='线上广告位'">
            <el-select v-model="formInline.rotationOrder" placeholder="请选择" style="margin-top:5px;width: 100%;">
              <el-option label="1" value="1"></el-option>
              <el-option label="2" value="2"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="广告尺寸" prop="dimensions" v-if="ontype =='线上广告位'">
            <el-input v-model="formInline.dimensions" disabled placeholder="宽:300px 长:400px"
              style="margin-top:5px;"></el-input>

          </el-form-item>
        </el-col>

      </el-row>



      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="可上传大小" prop="size" v-if="ontype =='线上广告位'">
            <el-input v-model="formInline.size" disabled placeholder="100kb" style="margin-top:5px;"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="上传格式" prop="uploadFormat" v-if="ontype =='线上广告位'">
            <el-input v-model="formInline.uploadFormat" disabled placeholder="JPG、PNG、MP4"
              style="margin-top:5px;"></el-input>
          </el-form-item>
        </el-col>

      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="可上传大小" prop="size" v-if="ontype =='实体广告位'">
            <el-input v-model="formInline.size" disabled placeholder="100kb" style="margin-top:5px;"></el-input>
          </el-form-item>
          <el-form-item label="设备类型" prop="deviceType" v-if="ontype =='实体广告位'">
            <div style="display: flex;justify-content: space-between;">
              <el-select v-model="formInline.deviceType" placeholder="请选择" style="margin-top:5px;width: 92%;">
                <el-option label="灯箱广告牌" value="灯箱广告牌"></el-option>

              </el-select>
            </div>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="设备大小" prop="equipmentSize" v-if="ontype =='实体广告位'">
            <div style="display: flex;">
              <div>
                <span style="font-size: 14px;">长：</span>
                <el-select v-model="formInline.equipmentSizeH" placeholder="请选择"
                  style="margin-top:5px;width: 90px;margin: 0 5px 0 0;">
                  <el-option label="10" value="10"></el-option>
                  <el-option label="20" value="20"></el-option>
                </el-select>
                <span style="font-size: 14px;margin-right:5px;">mm</span>
              </div>
              <div>
                <span style="font-size: 14px;">宽：</span>
                <el-select v-model="formInline.equipmentSizeK" placeholder="请选择"
                  style="margin-top:5px;width: 90px;margin: 0 5px 0 0;">
                  <el-option label="10" value="10"></el-option>
                  <el-option label="20" value="20"></el-option>
                </el-select>
                <span style="font-size: 14px;margin-right:5px;">mm</span>
              </div>
              <div>
                <span style="font-size: 14px;">高：</span>
                <el-select v-model="formInline.equipmentSizeG" placeholder="请选择"
                  style="margin-top:5px;width: 90px;margin: 0 5px 0 0;">
                  <el-option label="10" value="10"></el-option>
                  <el-option label="20" value="20"></el-option>
                </el-select>
                <span style="font-size: 14px;margin-right:5px;">mm</span>
              </div>
            </div>
          </el-form-item>
        </el-col>

      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="广告尺寸" v-if="ontype =='实体广告位'">
            <div style="display: flex;">
              <div>
                <span style="font-size: 14px;">长：</span>
                <el-select v-model="formInline.dimensionsH" placeholder="请选择"
                  style="margin-top:5px;width: 90px;margin: 0 5px 0 0;">
                  <el-option label="10" value="10"></el-option>
                  <el-option label="20" value="20"></el-option>
                </el-select>
                <span style="font-size: 14px;margin-right:5px;">mm</span>
              </div>
              <div>
                <span style="font-size: 14px;">宽：</span>
                <el-select v-model="formInline.dimensionsK" placeholder="请选择"
                  style="margin-top:5px;width: 90px;margin: 0 5px 0 0;">
                  <el-option label="10" value="10"></el-option>
                  <el-option label="20" value="20"></el-option>
                </el-select>
                <span style="font-size: 14px;margin-right:5px;">mm</span>
              </div>
            </div>
          </el-form-item>

        </el-col>
        <el-col :span="12">
          <el-form-item label="广告材质" prop="advertisingMaterial" v-if="ontype =='实体广告位'">
            <div style="display: flex;justify-content: space-between;">
              <el-select v-model="formInline.advertisingMaterial" placeholder="请选择" style="margin-top:5px;width: 92%">
                <el-option label="亚克力" value="亚克力"></el-option>
                <el-option label="玻璃" value="玻璃"></el-option>
              </el-select>
            </div>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="位置示意图" prop="locationDiagram">
            <upimg v-model="formInline.locationDiagram" :limit="3"></upimg>
          </el-form-item>
        </el-col>
      </el-row>
      <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 {
    uploadUrl
  } from '@/utils/request'
  import {
    miniioupload
  } from '../../api/commodityLease.js'
  import {
    gongyuan,
    lvdaoduan,
    addList,
    editList
  } from '../../api/map1.js';

  export default {
    components: {
      upimg,
      MapMark
    },
    data() {
      return {
        ontype:'线上广告位',
        typelist:[
          {
            name:'线上广告位',
            vaule:'线上广告位'
          },
          {
            name:'实体广告位',
            vaule:'实体广告位'
          },
        ],
        //地图标点的经纬度
        mapData: {},
        uploadFileUrl: uploadUrl, // 请求地址
        formInline: {
          advertisingName: '', //广告位名称
          advertisingType: '', //广告位类型
          rotationOrder: '', // 轮播顺序
          createUser: '', // 创建人
          affiliation: '', //所属端
          detailedLocation: '', //详细位置
          dimensions: '', //广告尺寸
          size: '', //广告大小
          uploadFormat: '', //上传类型
          advertisingSpaceStrategy: '', //策略
          advertisingSpaceNumber: '', //广告位编号
          locationDiagram: '', //位置示意图
          mapPunctuation: '', //地图标点
          deviceType: '', //设备类型
          equipmentSize: '', //设备尺寸
          advertisingMaterial: '', //广告材质
          createDate: '', //创建时间
          equipmentSizeH: "",
          equipmentSizeK: '',
          equipmentSizeG: '',
          dimensionsH: '',
          dimensionsK: '',
          rentalStatus: '0',
          publishStatus: '0'
        },
        rules: {
          advertisingName: [{
            required: true,
            message: '请输入名称',
            trigger: ['blur', 'change']
          }],
          advertisingType: [{
            required: true,
            message: '请选择类型',
            trigger: ['blur', 'change']
          }],
          affiliation: [{
            required: true,
            message: '请选择所属端',
            trigger: ['blur', 'change']
          }],
          rotationOrder: [{
            required: true,
            message: '请选择轮播顺序',
            trigger: ['blur', 'change']
          }],

        },
        rulesSt: {
          advertisingName: [{
            required: true,
            message: '请输入名称',
            trigger: ['blur', 'change']
          }],
          advertisingType: [{
            required: true,
            message: '请选择类型',
            trigger: ['blur', 'change']
          }],
          position: [{
            required: true,
            message: '请输入详细位置',
            trigger: ['blur', 'change']
          }],
          dimensionsH: [{
            required: true,
            message: '请选择广告尺寸',
            trigger: ['blur', 'change']
          }],
          dimensionsK: [{
            required: true,
            message: '请选择广告尺寸',
            trigger: ['blur', 'change']
          }],
          advertisingMaterial: [{
            required: true,
            message: '请选择材质',
            trigger: ['blur', 'change']
          }],
        },
        uploadFiles: [],
        filesLength: 1,
        gongyuanList: [],
        fileData: [],
        parentMessage: {},
        edit: true
      };
    },
    props: {
      info: {
        type: Object,
        default: {}
      },
      leixing: {
        type: Boolean,
        default: true
      }
    },
    async created() {
      if (this.info && this.info.id) {
        this.formInline = this.info
        if (this.leixing == false) {
          this.parentMessage = JSON.parse(this.info.mapPunctuation)
        }
        this.ontype = this.formInline.advertisingType
        if(this.ontype == '线上广告位') {
          this.typelist = [
          {
            name:'线上广告位',
            vaule:'线上广告位'
          }]
        } else {
          this.typelist = [
          {
            name:'实体广告位',
            vaule:'实体广告位'
          }]
        }
      } else {
        if (this.leixing ) {
         this.ontype = '线上广告位'
         this.formInline.advertisingType = '线上广告位'
        } else {
          this.ontype = '实体广告位'
          this.formInline.advertisingType = '实体广告位'
        }
      }
    },
    methods: {
      changeadvertisingType(e){
        console.error(e)
        this.ontype = e
      },
      //获取当前时间
      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.formInline.belongingParkTrail = ''
        this.gongyuan(vaule)
      },
      async gongyuan(value) {
        const gongyaun = await gongyuan()
        this.gongyuanList = gongyaun.data.filter(item => item.superior == value);
      },
      add() {

        if (this.formInline.id) {
          this.$refs.formInline.validate((valid) => {
            console.log(valid)
            if (valid) {
              this.formInline.updateDate = this.updateCurrentTime()

              if (this.leixing == false) {
                if (Object.keys(this.mapData).length != 0) {
                  this.formInline.mapPunctuation = JSON.stringify(this.mapData)
                }

                this.formInline.equipmentSize =
                  `长：${this.formInline.equipmentSizeH} 宽：${this.formInline.equipmentSizeK} 高：${this.formInline.equipmentSizeG}`
                this.formInline.dimensions = `长：${this.formInline.dimensionsH} 宽：${this.formInline.dimensionsK}`
              }

              editList(this.formInline).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.formInline.validate((valid) => {
            console.log(valid)
            if (valid) {
              this.formInline.createDate = this.updateCurrentTime()
              if (this.leixing) {
                this.formInline.dimensions = '宽:300px 长:400px'
                this.formInline.size = '100kb'
                this.formInline.uploadFormat = 'JPG、PNG、MP4'
              } else {
                this.formInline.mapPunctuation = JSON.stringify(this.mapData)
                this.formInline.equipmentSize =
                  `长：${this.formInline.equipmentSizeH} 宽：${this.formInline.equipmentSizeK} 高：${this.formInline.equipmentSizeG}`
                this.formInline.dimensions = `长：${this.formInline.dimensionsH} 宽：${this.formInline.dimensionsK}`
              }
              addList(this.formInline).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() {
        this.hefuData = []
      },
      hefuSuccess(response, file, fileList) {
        this.fileData = []
        console.error(response)
        let fd = new FormData()
        fd.append('file', response.raw)
        fd.append('filePath', 'hetong')
        miniioupload(fd).then(res => {
          console.error(res)
          const fileMsg = {
            name: response.name,
            url: res.data,
          }
          this.fileData.push(fileMsg)
        })
      },


    },
  };
</script>
<style lang="scss">
  .el-upload {
    display: block;
    /* text-align: left; */
  }

  .video-uploader {
    .el-upload {
      text-align: left;
    }
  }

  .el-dialog__header {
    background-color: #F2F3F5;
    text-align: left;
  }

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