<template>
  <div class="app-container">
    <div v-if="!isAdmin">
      <el-tabs v-model="step">
        <el-tab-pane label="首页模版配置" name="first"></el-tab-pane>
        <el-tab-pane label="商城logo配置" name="second"></el-tab-pane>
      </el-tabs>

      <div v-show="step == 'first'">
        <el-form label-width="120px" label-position="right">
          <el-form-item label="选择模版：" style="margin-bottom: 40px">
            <el-radio-group v-model="currentTemplate" @change="changeTemplate()">
              <div class="item">
                <el-radio :label="1">模版一</el-radio>
                <el-image :src="require('@/assets/template/tmp_1_thumb.png')"
                  :preview-src-list="[require('@/assets/template/tmp_1.png')]" fit="cover"></el-image>
              </div>
              <div class="item">
                <el-radio :label="2">模版二</el-radio>
                <el-image :src="require('@/assets/template/tmp_2_thumb.png')"
                  :preview-src-list="[require('@/assets/template/tmp_2.png')]" fit="cover"></el-image>
              </div>
              <div class="item">
                <el-radio :label="3">模版三</el-radio>
                <el-image :src="require('@/assets/template/tmp_3_thumb.png')"
                  :preview-src-list="[require('@/assets/template/tmp_3.png')]" fit="cover"></el-image>
              </div>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="首页模块设置：" v-show="currentTemplate === 1">
            <div class="diy-table">
              <div class="item head">
                <div class="label">模块名称</div>
                <div class="value">状态</div>
              </div>
              <div class="item">
                <div class="label">分类</div>
                <div class="value">
                  <el-switch v-model="switch_classify" disabled></el-switch>
                  <span :class="switch_classify ? 'on' : ''">{{ switch_classify ? '开启' : '隐藏' }}</span>
                </div>
              </div>
              <div class="item">
                <div class="label">轮播图</div>
                <div class="value">
                  <el-switch v-model="switch_banner"></el-switch>
                  <span :class="switch_banner ? 'on' : ''">{{ switch_banner ? '开启' : '隐藏' }}</span>
                </div>
              </div>
              <div class="item">
                <div class="label">公告</div>
                <div class="value">
                  <el-switch v-model="switch_notice"></el-switch>
                  <span :class="switch_notice ? 'on' : ''">{{ switch_notice ? '开启' : '隐藏' }}</span>
                </div>
              </div>
              <div class="item">
                <div class="label">专属优惠券</div>
                <div class="value">
                  <el-switch v-model="switch_coupon"></el-switch>
                  <span :class="switch_coupon ? 'on' : ''">{{ switch_coupon ? '开启' : '隐藏' }}</span>
                </div>
              </div>
              <div class="item">
                <div class="label">客户经理</div>
                <div class="value">
                  <el-switch v-model="switch_manager"></el-switch>
                  <span :class="switch_manager ? 'on' : ''">{{ switch_manager ? '开启' : '隐藏' }}</span>
                </div>
              </div>
              <div class="item">
                <div class="label">秒杀专区</div>
                <div class="value">
                  <el-switch v-model="switch_seckill"></el-switch>
                  <span :class="switch_seckill ? 'on' : ''">{{ switch_seckill ? '开启' : '隐藏' }}</span>
                </div>
              </div>
              <div class="item">
                <div class="label">好物推荐</div>
                <div class="value">
                  <el-switch v-model="switch_recom" disabled></el-switch>
                  <span :class="switch_recom ? 'on' : ''">{{ switch_recom ? '开启' : '隐藏' }}</span>
                </div>
              </div>
            </div>
          </el-form-item>
          <el-form-item label="首页模块设置：" v-show="currentTemplate === 2">
            <div class="diy-table">
              <div class="item head">
                <div class="label">模块名称</div>
                <div class="value">状态</div>
              </div>
              <div class="item">
                <div class="label">首页弹窗</div>
                <div class="value">
                  <el-switch v-model="switch_dialog"></el-switch>
                  <span :class="switch_dialog ? 'on' : ''">{{ switch_dialog ? '开启' : '隐藏' }}</span>
                </div>
              </div>
              <div class="item">
                <div class="label">轮播图</div>
                <div class="value">
                  <el-switch v-model="switch_banner"></el-switch>
                  <span :class="switch_banner ? 'on' : ''">{{ switch_banner ? '开启' : '隐藏' }}</span>
                </div>
              </div>
              <div class="item">
                <div class="label">胶囊图</div>
                <div class="value">
                  <el-switch v-model="switch_activity1"></el-switch>
                  <span :class="switch_activity1 ? 'on' : ''">{{ switch_activity1 ? '开启' : '隐藏' }}</span>
                </div>
              </div>
              <div class="item">
                <div class="label">分类</div>
                <div class="value">
                  <el-switch v-model="switch_classify" disabled></el-switch>
                  <span :class="switch_classify ? 'on' : ''">{{ switch_classify ? '开启' : '隐藏' }}</span>
                </div>
              </div>
              <div class="item">
                <div class="label">活动专区2</div>
                <div class="value">
                  <el-switch v-model="switch_activity2"></el-switch>
                  <span :class="switch_activity2 ? 'on' : ''">{{ switch_activity2 ? '开启' : '隐藏' }}</span>
                </div>
              </div>
              <div class="item">
                <div class="label">专场专区</div>
                <div class="value">
                  <el-switch v-model="switch_special"></el-switch>
                  <span :class="switch_special ? 'on' : ''">{{ switch_special ? '开启' : '隐藏' }}</span>
                </div>
              </div>
              <div class="item">
                <div class="label">秒杀专区</div>
                <div class="value">
                  <el-switch v-model="switch_seckill"></el-switch>
                  <span :class="switch_seckill ? 'on' : ''">{{ switch_seckill ? '开启' : '隐藏' }}</span>
                </div>
              </div>
              <div class="item">
                <div class="label">专属优惠券</div>
                <div class="value">
                  <el-switch v-model="switch_coupon"></el-switch>
                  <span :class="switch_coupon ? 'on' : ''">{{ switch_coupon ? '开启' : '隐藏' }}</span>
                </div>
              </div>
              <div class="item">
                <div class="label">客户经理</div>
                <div class="value">
                  <el-switch v-model="switch_manager"></el-switch>
                  <span :class="switch_manager ? 'on' : ''">{{ switch_manager ? '开启' : '隐藏' }}</span>
                </div>
              </div>
              <div class="item">
                <div class="label">公告</div>
                <div class="value">
                  <el-switch v-model="switch_notice"></el-switch>
                  <span :class="switch_notice ? 'on' : ''">{{ switch_notice ? '开启' : '隐藏' }}</span>
                </div>
              </div>
              <div class="item">
                <div class="label">新维度分类专区</div>
                <div class="value">
                  <el-switch v-model="switch_newclassify" disabled></el-switch>
                  <span :class="switch_newclassify ? 'on' : ''">{{ switch_newclassify ? '开启' : '隐藏' }}</span>
                </div>
              </div>
            </div>
          </el-form-item>
          <el-form-item label="首页模块设置：" v-show="currentTemplate === 3">
            <div class="diy-table">
              <div class="item head">
                <div class="label">模块名称</div>
                <div class="value">状态</div>
              </div>
              <div class="item">
                <div class="label">首页弹窗</div>
                <div class="value">
                  <el-switch v-model="switch_dialog"></el-switch>
                  <span :class="switch_dialog ? 'on' : ''">{{ switch_dialog ? '开启' : '隐藏' }}</span>
                </div>
              </div>
              <div class="item">
                <div class="label">轮播图</div>
                <div class="value">
                  <el-switch v-model="switch_banner"></el-switch>
                  <span :class="switch_banner ? 'on' : ''">{{ switch_banner ? '开启' : '隐藏' }}</span>
                </div>
              </div>
              <div class="item">
                <div class="label">胶囊图</div>
                <div class="value">
                  <el-switch v-model="switch_activity1"></el-switch>
                  <span :class="switch_activity1 ? 'on' : ''">{{ switch_activity1 ? '开启' : '隐藏' }}</span>
                </div>
              </div>
              <div class="item">
                <div class="label">分类</div>
                <div class="value">
                  <el-switch v-model="switch_classify" disabled></el-switch>
                  <span :class="switch_classify ? 'on' : ''">{{ switch_classify ? '开启' : '隐藏' }}</span>
                </div>
              </div>
              <div class="item">
                <div class="label">活动专区2</div>
                <div class="value">
                  <el-switch v-model="switch_activity2"></el-switch>
                  <span :class="switch_activity2 ? 'on' : ''">{{ switch_activity2 ? '开启' : '隐藏' }}</span>
                </div>
              </div>
              <div class="item">
                <div class="label">专场专区</div>
                <div class="value">
                  <el-switch v-model="switch_special"></el-switch>
                  <span :class="switch_special ? 'on' : ''">{{ switch_special ? '开启' : '隐藏' }}</span>
                </div>
              </div>
              <div class="item">
                <div class="label">秒杀专区</div>
                <div class="value">
                  <el-switch v-model="switch_seckill"></el-switch>
                  <span :class="switch_seckill ? 'on' : ''">{{ switch_seckill ? '开启' : '隐藏' }}</span>
                </div>
              </div>
              <div class="item">
                <div class="label">专题精选</div>
                <div class="value">
                  <el-switch v-model="switch_selected"></el-switch>
                  <span :class="switch_selected ? 'on' : ''">{{ switch_selected ? '开启' : '隐藏' }}</span>
                </div>
              </div>
              <div class="item">
                <div class="label">专属优惠券</div>
                <div class="value">
                  <el-switch v-model="switch_coupon"></el-switch>
                  <span :class="switch_coupon ? 'on' : ''">{{ switch_coupon ? '开启' : '隐藏' }}</span>
                </div>
              </div>
              <div class="item">
                <div class="label">客户经理</div>
                <div class="value">
                  <el-switch v-model="switch_manager"></el-switch>
                  <span :class="switch_manager ? 'on' : ''">{{ switch_manager ? '开启' : '隐藏' }}</span>
                </div>
              </div>
              <div class="item">
                <div class="label">公告</div>
                <div class="value">
                  <el-switch v-model="switch_notice"></el-switch>
                  <span :class="switch_notice ? 'on' : ''">{{ switch_notice ? '开启' : '隐藏' }}</span>
                </div>
              </div>
              <div class="item">
                <div class="label">新维度分类专区</div>
                <div class="value">
                  <el-switch v-model="switch_newclassify" disabled></el-switch>
                  <span :class="switch_newclassify ? 'on' : ''">{{ switch_newclassify ? '开启' : '隐藏' }}</span>
                </div>
              </div>
              <div class="item">
                <div class="label">底部广告图</div>
                <div class="value">
                  <el-switch v-model="switch_bottom"></el-switch>
                  <span :class="switch_bottom ? 'on' : ''">{{ switch_bottom ? '开启' : '隐藏' }}</span>
                </div>
              </div>
            </div>
          </el-form-item>
        </el-form>
      </div>

      <div v-show="step == 'second'">
        <el-form label-width="120px" label-position="right">
          <el-upload class="avatar-uploader" :action="baseURL + 'common/upload'" :headers="myHeaders"
            :show-file-list="false" :on-success="uploadSuccess" :before-upload="beforeUpload">
          </el-upload>
          <el-form-item label="主logo：" prop="imgUrl">
            <div class="images">
              <div class="main-img">
                <div class="img" v-if="img1_url" @mouseover="img1_hover = true;" @mouseout="img1_hover = false;">
                  <el-image ref="img1" :src="img1_url" :preview-src-list="[img1_url]" style="width: 120px; height: 120px"
                    fit="contain"></el-image>
                  <div class="mask" v-show="img1_hover">
                    <i class="el-icon-zoom-in" @click="previewImage('img1')"></i>
                    <i class="el-icon-upload2" @click="uploadImage('img1')"></i>
                  </div>
                </div>
                <div class="add" v-else @click="uploadImage('img1')">
                  <i class="el-icon-plus avatar-uploader-icon"></i>
                </div>
              </div>
              <div class="tmp-img">
                <el-image :src="require('@/assets/template/template_1.png')"
                  :preview-src-list="[require('@/assets/template/template_1.png')]"
                  style="width: 120px; height: 120px; display: block" fit="contain"></el-image>
                <div class="tmp">示例图</div>
              </div>
            </div>
            <div class="tips">
              <span>建议尺寸：400*400</span>
            </div>
          </el-form-item>
          <el-form-item label="通用logo：" prop="imgUrl">
            <div class="images">
              <div class="main-img">
                <div class="img" v-if="img2_url" @mouseover="img2_hover = true;" @mouseout="img2_hover = false;">
                  <el-image ref="img2" :src="img2_url" :preview-src-list="[img2_url]" style="width: 120px; height: 120px"
                    fit="contain"></el-image>
                  <div class="mask" v-show="img2_hover">
                    <i class="el-icon-zoom-in" @click="previewImage('img2')"></i>
                    <i class="el-icon-upload2" @click="uploadImage('img2')"></i>
                  </div>
                </div>
                <div class="add" v-else @click="uploadImage('img2')">
                  <i class="el-icon-plus avatar-uploader-icon"></i>
                </div>
              </div>
              <div class="tmp-img">
                <el-image :src="require('@/assets/template/template_2.png')"
                  :preview-src-list="[require('@/assets/template/template_2.png')]"
                  style="width: 120px; height: 120px; display: block" fit="contain"></el-image>
                <div class="tmp">示例图</div>
              </div>
            </div>
            <div class="tips">
              <span>建议尺寸：280*60</span>
            </div>
          </el-form-item>
          <el-form-item label="头像logo：" prop="imgUrl">
            <div class="images">
              <div class="main-img">
                <div class="img" v-if="img3_url" @mouseover="img3_hover = true;" @mouseout="img3_hover = false;">
                  <el-image ref="img3" :src="img3_url" :preview-src-list="[img3_url]" style="width: 120px; height: 120px"
                    fit="contain"></el-image>
                  <div class="mask" v-show="img3_hover">
                    <i class="el-icon-zoom-in" @click="previewImage('img3')"></i>
                    <i class="el-icon-upload2" @click="uploadImage('img3')"></i>
                  </div>
                </div>
                <div class="add" v-else @click="uploadImage('img3')">
                  <i class="el-icon-plus avatar-uploader-icon"></i>
                </div>
              </div>
              <div class="tmp-img">
                <el-image :src="require('@/assets/template/template_3.png')"
                  :preview-src-list="[require('@/assets/template/template_3.png')]"
                  style="width: 120px; height: 120px; display: block" fit="contain"></el-image>
                <div class="tmp">示例图</div>
              </div>
            </div>
            <div class="tips">
              <span>建议尺寸：400*400</span>
            </div>
          </el-form-item>
        </el-form>
      </div>

      <div class="page-footer">
        <div class="footer" :class="classObj">
          <el-button type="primary" @click="submitStep1Form" :loading="formLoading" v-if="step == 'first'">{{ formLoading
            ? '保存中 ...' : '保 存' }}</el-button>
          <el-button type="primary" @click="submitStep2Form" :loading="formLoading" v-if="step == 'second'">{{ formLoading
            ? '保存中 ...' : '保 存' }}</el-button>
        </div>
      </div>
    </div>

    <div v-else>
      <div class="nodata">超级管理员无法进行商城配置</div>
    </div>

  </div>
</template>

<script>
import { getToken, getUserid } from '@/utils/auth';
import { getTemplate, editAccount, editTemplate, getDetail } from "@/api/merchant";
import { getInfo } from '@/api/user'
export default {
  data() {
    return {
      isAdmin: JSON.parse(localStorage.getItem("greemall_user")).type === 2,
      baseURL: process.env.VUE_APP_BASE_API,
      myHeaders: { 'x-token': getToken() },
      step: 'first',
      companyId: JSON.parse(localStorage.getItem("greemall_user")).companyWechatId,
      currentTemplate: JSON.parse(localStorage.getItem("greemall_user")).useTemplate,

      switch_dialog: true,
      switch_banner: true,
      switch_activity1: true,
      switch_classify: true,
      switch_activity2: true,
      switch_special: true,
      switch_seckill: true,
      switch_coupon: true,
      switch_manager: true,
      switch_notice: true,
      switch_newclassify: true,
      switch_recom: true,
      switch_selected: true,
      switch_bottom: true,

      templateList: [],

      uploadImageType: null,
      img1_url: '',
      img1_hover: false,
      img2_url: '',
      img2_hover: false,
      img3_url: '',
      img3_hover: false,

      formLoading: false
    }
  },
  computed: {
    sidebar() {
      return this.$store.state.app.sidebar
    },
    classObj() {
      return {
        hideSidebar: !this.sidebar.opened,
        openSidebar: this.sidebar.opened
      }
    },
  },

  created() {
    if (this.isAdmin) return false;
    this.getTemplate();
  },

  watch: {
    step() {
      if (this.step === 'first') {
        this.getTemplate();
      } else {
        this.getLogoDetail();
      }
    }
  },

  methods: {
    // 获取模版列表
    getTemplate() {
      getTemplate({
        companyId: this.companyId
      }).then(res => {
        if(res.data){
          this.templateList = res.data;
        }
        this.initHome();
      })
    },

    // 切换模版
    changeTemplate() {
      this.initHome();
    },

    // 初始化配置
    initHome() {
      let index = this.$findElem(this.templateList, 'templateType', this.currentTemplate);
      this.switch_dialog = this.templateList[index].popupStatus;
      this.switch_banner = this.templateList[index].carouseStatus;
      this.switch_activity1 = this.templateList[index].active1Status;
      this.switch_classify = this.templateList[index].categoryStatus;
      this.switch_activity2 = this.templateList[index].active2Status;
      this.switch_special = this.templateList[index].onlyStatus;
      this.switch_seckill = this.templateList[index].killStatus;
      this.switch_coupon = this.templateList[index].couponStatus;
      this.switch_manager = this.templateList[index].customerStatus;
      this.switch_notice = this.templateList[index].notiveStatus;
      this.switch_newclassify = this.templateList[index].newsStatus;
      this.switch_recom = this.templateList[index].okStatus;
      this.switch_selected = this.templateList[index].topicsStatus;
      this.switch_bottom = this.templateList[index].bottomBannerStatus;
    },

    // 保存首页配置
    submitStep1Form() {
      let index = this.$findElem(this.templateList, 'templateType', this.currentTemplate);
      let params = {
        companyWechatId: this.companyId,
        useTemplate: this.currentTemplate,
        companyWechatTemplatesList: [{
          companyWechatTemplateId: this.templateList[index].companyWechatTemplateId,
          templateType: this.templateList[index].templateType,
          popupStatus: this.switch_dialog,
          carouseStatus: this.switch_banner,
          active1Status: this.switch_activity1,
          categoryStatus: this.switch_classify,
          active2Status: this.switch_activity2,
          onlyStatus: this.switch_special,
          killStatus: this.switch_seckill,
          couponStatus: this.switch_coupon,
          customerStatus: this.switch_manager,
          notiveStatus: this.switch_notice,
          newsStatus: this.switch_newclassify,
          okStatus: this.switch_recom,
          topicsStatus: this.switch_selected,
          bottomBannerStatus: this.switch_bottom,
        }],
      }
      this.formLoading = true;
      editTemplate(params).then(res => {
        this.getTemplate();
        this.$successMsg('保存成功');
        getInfo(getUserid()).then(response => {
          localStorage.setItem("greemall_user", JSON.stringify(response.data));
        })
      }).finally(res => {
        this.formLoading = false;
      })
    },

    // 获取logo信息
    getLogoDetail() {
      getDetail({ adminUserId: getUserid() }).then(res => {
        this.img1_url = res.data.minLogo1;
        this.img2_url = res.data.minLogo2;
        this.img3_url = res.data.minLogo3;
      })
    },

    uploadImage(type) {
      this.uploadImageType = type;
      document.querySelector('.avatar-uploader input').click();
    },

    // 上传图片
    uploadSuccess(res, file) {
      this[this.uploadImageType + '_url'] = res.data.url;
    },

    beforeUpload(file) {
      const fileSuffix = file.name.substring(file.name.lastIndexOf(".") + 1);
      const whiteList = ['jpg', 'jpeg', 'png'];
      if (whiteList.indexOf(fileSuffix) === -1) {
        this.$errorMsg('只支持上传jpg/jpeg/png文件!');
        return false;
      }
    },

    // 预览图片
    previewImage(type) {
      this.$refs[type].showViewer = true;
    },

    submitStep2Form() {
      if (!this.img1_url) {
        return this.$errorMsg('请上传主logo');
      }
      if (!this.img2_url) {
        return this.$errorMsg('请上传通用logo');
      }
      if (!this.img3_url) {
        return this.$errorMsg('请上传头像logo');
      }
      this.formLoading = true;
      let params = {
        companyWechatId: this.companyId,
        mainLog: this.img1_url,
        commonLog: this.img2_url,
        minLogo: this.img3_url,
      }
      editTemplate(params).then(res => {
        this.getLogoDetail();
        this.$successMsg('保存成功');
      }).finally(res => {
        this.formLoading = false;
      })
    },
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-radio-group {
  display: flex;

  .item {
    margin-right: 20px;

    .el-radio {
      height: 40px;
      line-height: 40px;
    }

    .el-image {
      width: 120px;
      height: 200px;
      display: block;
    }
  }
}

.images {
  display: flex;
  flex-wrap: wrap;

  .main-img {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    width: 120px;
    margin-right: 20px;

    .img {
      border: 1px dashed #eaeaea;
      border-radius: 5px;
      overflow: hidden;
      position: relative;

      .el-image {
        display: block;
      }

      .mask {
        position: absolute;
        left: 0;
        top: 0;
        width: 120px;
        height: 120px;
        background: rgba($color: #000000, $alpha: 0.3);
        display: flex;
        align-items: center;
        justify-content: center;

        i {
          font-size: 20px;
          color: #ffffff;
          cursor: pointer;
          margin: 0 8px;
        }
      }
    }

    .text {
      font-size: 14px;
      color: #666666;
    }
  }

  .add {
    width: 120px;
    height: 120px;
    border: 1px dashed #eaeaea;
    border-radius: 5px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;

    i {
      font-size: 30px;
      color: #999;
    }
  }

  .tmp-img {
    position: relative;

    .tmp {
      position: absolute;
      left: 0;
      top: 0;
      line-height: 20px;
      padding: 0 8px;
      background: #f66460;
      border-radius: 0 0 10px 0;
      font-size: 12px;
      color: #ffffff;
    }
  }
}

.tips {
  margin-top: 10px;

  span {
    display: inline-block;
    width: 300px;
    text-align: center;
    line-height: 32px;
    background: #ffefef;
    font-size: 14px;
    color: #f66460;
  }
}

.diy-table {
  width: 400px;

  .item {
    display: flex;
    border: 1px solid #eaeaea;
    border-bottom: none;

    &.head {
      font-weight: bold;
    }

    &:last-child {
      border-bottom: 1px solid #eaeaea;
    }

    >div {
      flex: 1;
      display: flex;
      align-items: center;
      justify-content: center;
    }

    .label {
      border-right: 1px solid #eaeaea;
    }

    .value {
      span {
        margin-left: 10px;
        color: #666666;

        &.on {
          color: #409EFF;
        }
      }
    }
  }
}

.nodata {
  font-size: 18px;
  color: #666666;
}</style>