<script>
import Vue from 'vue'
import { saveExpo, getMyExpoInfo } from '@/api/expo'
import { getFormList } from '@/api/form'
import { upload } from '@/api/system'
export default Vue.extend({
  name: 'ExhibitorSetting',
  data() {
    return {
      exhibitorSetting: {
        id: '',
        expo_name: '',
        start_date: '',
        end_date: '',
        location: '',
        organizer: '',
        contact_phone: '',
        contact_email: '',
        content: '',
        logo: '/common/2025/0915/68c7b25cc9e26.webp',
        images: ['/common/2025/0915/68c7b263bc4b1.webp'],
        social_links: {
          facebook: '',
          twitter: '',
          linkedin: ''
        },
        form_template_id: '',
        urla: '',
        seo_title: '',
        seo_description: '',
        seo_keywords: '',
        show_official_footer: '1'
      },
      formList: [],
      loading: false,
      ossUrl: process.env.VUE_APP_OSS_DOMAIN
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    init() {
      if (this.$route.params.id) {
        this.loading = true
        this.exhibitorSetting.id = this.$route.params.id
        getMyExpoInfo(this.exhibitorSetting.id).then(res => {
          this.exhibitorSetting = res.data
          this.exhibitorSetting.images = JSON.parse(this.exhibitorSetting.images)
          this.exhibitorSetting.social_links = JSON.parse(this.exhibitorSetting.social_links)
          this.loading = false
          console.log(res)
        }).catch(err => {
          this.loading = false
          console.log(err)
        })
      }
      getFormList(1, 1000).then(res => {
        this.formList = res.data.data
      }).catch(err => {
        console.log(err)
      })
    },
    uploadImage(event, type) {
      this.loading = true
      console.log(event.target.files[0])
      upload(event.target.files[0]).then(res => {
        console.log(res)
        if (type === 'logo') {
          this.exhibitorSetting.logo = res.data.file
        }
        if (type === 'cover') {
          this.exhibitorSetting.images[0] = res.data.file
          this.exhibitorSetting.images.push()
        }
        this.loading = false
      }).catch(err => {
        this.loading = false
        console.log(err)
        this.$message.error('上传图片时遇到问题')
      })
    },
    save() {
      if (
        this.exhibitorSetting.expo_name === '' ||
        this.exhibitorSetting.start_date === '' ||
        this.exhibitorSetting.end_date === '' ||
        this.exhibitorSetting.location === '' ||
        this.exhibitorSetting.organizer === '' ||
        this.exhibitorSetting.contact_phone === '' ||
        this.exhibitorSetting.contact_email === '' ||
        this.exhibitorSetting.urla === ''
      ) {
        this.$message.error('请将信息填写完整')
        return
      }
      if (this.loading) {
        return
      }
      this.loading = true
      saveExpo(
        this.exhibitorSetting.id,
        this.exhibitorSetting.expo_name,
        this.exhibitorSetting.start_date,
        this.exhibitorSetting.end_date,
        this.exhibitorSetting.location,
        this.exhibitorSetting.organizer,
        this.exhibitorSetting.contact_phone,
        this.exhibitorSetting.contact_email,
        this.exhibitorSetting.content,
        this.exhibitorSetting.logo,
        this.exhibitorSetting.images,
        this.exhibitorSetting.social_links,
        this.exhibitorSetting.form_template_id,
        this.exhibitorSetting.page_template_id,
        this.exhibitorSetting.urla,
        this.exhibitorSetting.seo_title,
        this.exhibitorSetting.seo_description,
        this.exhibitorSetting.seo_keywords
      ).then(res => {
        console.log(res)
        this.$message.success('保存成功')
        this.loading = false
        this.$router.push('/exhibitor/list')
      }).catch(err => {
        console.log(err)
        this.loading = false
        this.$message.error('保存失败')
      })
    }
  }
})
</script>

<template>
  <div v-loading="loading" class="main-box">
    <div class="save">
      <el-button v-permission="'exhibitor.save'" type="primary" @click="save">{{ exhibitorSetting.id?'保存修改':'新建展商' }}</el-button>
    </div>
    <div class="info">
      <div class="scroll">
        <div class="form-item required">
          <div class="label">展会名称 </div>
          <el-input v-model="exhibitorSetting.expo_name" class="input" placeholder="请输入展会名称" />
        </div>
        <div class="form-item required">
          <div class="label">展会时间</div>
          <div class="time-cont">
            <el-date-picker
              v-model="exhibitorSetting.start_date"
              type="date"
              placeholder="开始时间"
              value-format="yyyy-MM-dd"
              style="width: 100%;"
            />
            <el-date-picker
              v-model="exhibitorSetting.end_date"
              type="date"
              placeholder="结束时间"
              value-format="yyyy-MM-dd"
              style="width: 100%;"
            />
          </div>
        </div>
        <div class="form-item required">
          <div class="label">展会地点</div>
          <el-input v-model="exhibitorSetting.location" class="input" placeholder="请输入展会地点" />
        </div>
        <div class="form-item required">
          <div class="label">主办单位</div>
          <el-input v-model="exhibitorSetting.organizer" class="input" placeholder="请输入主办单位" />
        </div>
        <div class="form-item required">
          <div class="label">联系电话</div>
          <el-input v-model="exhibitorSetting.contact_phone" class="input" placeholder="请输入联系电话" />
        </div>
        <div class="form-item required">
          <div class="label">联系邮箱</div>
          <el-input v-model="exhibitorSetting.contact_email" class="input" placeholder="请输入联系邮箱" />
        </div>
        <div class="form-item">
          <div class="label">社交账号</div>
          <div class="social-list">
            <div class="social-item">
              <el-input v-model="exhibitorSetting.social_links.facebook" placeholder="请输入facebook主页地址">
                <template slot="append">facebook</template>
              </el-input>
            </div>
            <div class="social-item">
              <el-input v-model="exhibitorSetting.social_links.twitter" placeholder="请输入twitter主页地址">
                <template slot="append">twitter</template>
              </el-input>
            </div>
            <div class="social-item">
              <el-input v-model="exhibitorSetting.social_links.linkedin" placeholder="请输入linkedin主页地址">
                <template slot="append">linkedin</template>
              </el-input>
            </div>
          </div>
        </div>
        <div class="form-item required">
          <div class="label">表单模板</div>
          <el-select v-model="exhibitorSetting.form_template_id">
            <el-option v-for="item in formList" :key="item.id" :value="item.id" :label="item.template_name"></el-option>
          </el-select>
        </div>
        <div v-permission="'exhibitor.copyright'" class="form-item required">
          <div class="label">表单底部显示系统信息</div>
          <el-switch active-value="1" inactive-value="0" v-model="exhibitorSetting.show_official_footer" class="input" />
        </div>
        <div class="form-item required">
          <div class="label">url短名称</div>
          <el-input v-model="exhibitorSetting.urla" class="input" placeholder="请输入url" />
        </div>
        <div class="form-item">
          <div class="label">SEO标题</div>
          <el-input v-model="exhibitorSetting.seo_title" class="input" placeholder="请输入SEO标题" />
        </div>
        <div class="form-item">
          <div class="label">SEO关键字</div>
          <el-input v-model="exhibitorSetting.seo_keywords" class="input" placeholder="请输入SEO关键字" />
        </div>
        <div class="form-item">
          <div class="label">SEO描述</div>
          <el-input v-model="exhibitorSetting.seo_description" class="input" placeholder="请输入SEO描述" />
        </div>
      </div>
    </div>
    <div class="desc">
      <div class="scroll">
        <div class="vision-cont">
          <img :src="ossUrl + exhibitorSetting.images[0]" alt="" class="image loading">
          <div class="avatar-name">
            <img :src="ossUrl + exhibitorSetting.logo" alt="" class="avatar loading">
            <div>
              <div class="name">
                {{ exhibitorSetting.expo_name||'示例展会名称' }}
              </div>
              <div class="exhibitor">
                {{ exhibitorSetting.organizer||'示例主办方名称' }}
              </div>
            </div>
            <div class="avatar-upload">
              <div class="upload-icon">
                <span class="el-icon-upload icon" />
                <span class="text">上传logo</span>
              </div>
              <input type="file" class="uploader" @change="uploadImage($event,'logo')">
            </div>
          </div>
          <div class="cover-upload">
            <div class="upload-icon">
              <span class="el-icon-upload icon" />
              <span class="text">上传主视觉图</span>
            </div>
            <input type="file" class="uploader" @change="uploadImage($event,'cover')">
          </div>
        </div>
        <el-input v-model="exhibitorSetting.content" rows="12" type="textarea" class="text-area" placeholder="请输入展会介绍" />
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
  .main-box{
    height: 100%;
    width: 100%;
    display: grid;
    grid-gap: 16px;
    grid-template-columns: 1fr 1fr;
    grid-template-rows: auto 1fr;
    .save{
      display: flex;
      justify-content: flex-end;
      grid-column: span 2;
    }
    .info{
      position: relative;
      height: 100%;
      width: 100%;
      .scroll{
        position: absolute;
        top: 0;
        left: 0;
        height: 100%;
        width: 100%;
        overflow: hidden;
        overflow-y: auto;
      }
      .form-item{
        margin: 16px 0;
        .el-select{
          width: 100%;
        }
        .label{
          margin-bottom: 8px;
        }
        .social-list{
          margin-bottom: 12px;
          display: flex;
          flex-direction: column;
          grid-gap: 8px;
          .drag-cont{
            display:block;
            min-height: 60px;
            .social-item{
              position: relative;
              margin-bottom: 6px;
              .handel{
                color: gray;
                height: 100%;
                width: 20px;
                position: absolute;
                top: 0;
                left: 0;
                display: flex;
                justify-content: center;
                align-items: center;
                cursor: move;
              }
              .el-select{
                width: 160px;
              }
            }
          }
        }
        .time-cont{
          display: grid;
          grid-template-columns: 1fr 1fr;
          grid-gap: 16px;
        }
        &.required{
          .label::after{
            content: '*';
            color: red;
            margin-left: 4px;
          }
        }
      }
    }
    .desc{
      height: 100%;
      width: 100%;
      position: relative;
      .scroll{
        padding: 8px;
        position: absolute;
        top: 0;
        left: 0;
        height: 100%;
        width: 100%;
        overflow: hidden;
        overflow-y: auto;
      }
      .text-area{
        margin-top: 24px;
      }
      .vision-cont{
        position: relative;
        box-shadow: 0 0 8px 0 #00000022;
        overflow: hidden;
        width: 100%;
        border-radius: 16px;
        transition-duration: 300ms;
        &:hover{
          transform: translateY(-2px);
          box-shadow: 0 2px 12px 0 #00000022;
        }
        .cover-upload{
          position: absolute;
          top: 0;
          left: 0;
          width: 100%;
          aspect-ratio: 2.4;
          .upload-icon{
            overflow: hidden;
            transition-duration: 300ms;
            opacity: 0;
            color: gray;
            width: 100%;
            height: 100%;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            backdrop-filter: blur(0px);
            .icon{
              font-size: 48px;
            }
          }
          &:hover{
            .upload-icon{
              backdrop-filter: blur(8px);
              opacity: 1;
            }
          }
          .uploader{
            opacity: 0;
            width: 100%;
            height: 100%;
            position: absolute;
            left: 0;
            top: 0;
          }
        }
        .avatar-name{
          position: relative;
          padding: 24px;
          display: grid;
          grid-template-columns: auto 1fr;
          align-items: center;
          grid-gap: 24px;
          .avatar-upload{
            position: absolute;
            top: 24px;
            left: 24px;
            width: 80px;
            height: 80px;
            .upload-icon{
              border-radius: 50%;
              transition-duration: 300ms;
              opacity: 0;
              color: gray;
              width: 100%;
              height: 100%;
              display: flex;
              flex-direction: column;
              align-items: center;
              justify-content: center;
              .icon{
                margin-top: -8px;
                font-size: 32px;
              }
              .text{
                line-height: 1;
                font-size: 12px;
              }
            }
            &:hover{
              .upload-icon{
                backdrop-filter: blur(8px);
                opacity: 1;
              }
            }
            .uploader{
              opacity: 0;
              width: 100%;
              height: 100%;
              position: absolute;
              left: 0;
              top: 0;
            }
          }
          .avatar{
            width: 80px;
            height: 80px;
            border-radius: 50%;
          }
          .name{
            font-size: 24px;
            font-weight: bold;
          }
          .exhibitor{
            color: gray;
          }
        }
        .image{
          object-fit: cover;
          width: 100%;
          aspect-ratio: 2.4;
        }
      }
    }
  }
</style>
