<template>
  <div class="addBanner app-container">
    <el-form
      :model="setQuery"
      ref="setQuery"
      :rules="rules"
      label-width="130px"
    >
      <el-form-item label="名称：" prop="advertsName">
        <el-input
          v-model="setQuery.advertsName"
          class="item-width-300"
        ></el-input>
      </el-form-item>

     <el-form-item label="广告图：" prop="advertsImg">
        <Upload
          type="advertsImg"
          id="advertsImg"
          width="300px"
          height="200px"
          uploadType="SHOP_INFO"
          :imgUrl="setQuery.advertsImg"
          @uploadEnd="uploadEnd"
        ></Upload>
      </el-form-item>
       <el-form-item label="广告类型：" prop="advertsType">
        <el-select
          v-model="setQuery.advertsType"
          clearable
          class="item-width-200"
          placeholder="请选择广告类型"
        >
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="小程序：" prop="belongType">
        <el-select
          v-model="setQuery.belongType"
          clearable
          class="item-width-200"
          placeholder="请选择小程序"
        >
          <el-option
            v-for="item in belongTypeOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
        <el-form-item label="跳转链接：" prop="jumpUrl">
        <el-input
          v-model="setQuery.jumpUrl"
          class="item-width-300"
          placeholder="请输入跳转链接"
        ></el-input>
      </el-form-item>
       <el-form-item label="权重排序：" prop="sort">
        <el-input
          v-model.number="setQuery.sort"
          class="item-width-300"
        ></el-input>
      </el-form-item>
      <el-form-item label="展示位置顺序：" prop="showSort">
        <el-input
          v-model.number="setQuery.showSort"
          class="item-width-300"
        ></el-input>
      </el-form-item>
       <el-form-item label="是否显示：">
          <el-radio-group v-model="setQuery.status">
          <el-radio :label="1">显示</el-radio>
          <el-radio :label="2">隐藏</el-radio>
        </el-radio-group>
      </el-form-item>
      <template v-if="setQuery.advertsType == 6">
        <el-form-item label="弹框显示限制：">
            <el-radio-group v-model="setQuery.loginShow">
            <el-radio label="1">未登录</el-radio>
            <el-radio label="2">已登录</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="登录限制：">
          <el-checkbox :true-label="1" :false-label="2" v-model="setQuery.loginLimit">登录后才允许查看</el-checkbox>
        </el-form-item>
      </template>

    </el-form>

    <div class="btn-group">
       <el-button @click="cancel">取 消</el-button>
     <el-button type="primary" :loading="loading"  @click="save">确 定</el-button>
    </div>
  </div>
</template>

<script>
import Upload from '@/components/Upload'
import {addOrUpdateAdv} from '@/api/appConfig'
export default {
    components: {
        Upload,
    },
  data() {
    return {
        loading:false,
        setQuery: {
          "advertsImg": "",
          "advertsName": "",
          "advertsType": 1,
          "jumpUrl": "",
          "menuId": '',
          "sort": 0,
          "showSort":0,
          "status":1,
          "belongType":'2',
          "loginShow":'1',
          "loginLimit":1,
        },
      options:[
        {
          label:'首页Banner',
          value:1
        },
        {
          label:'首页广告位',
          value:2
        },
        {
          label:'贵大奶茶Banner',
          value:3
        },
        {
          label:'贵大奶茶广告位',
          value:4
        },
        {
          label:'首页弹窗',
          value:6
        }
      ],
      belongTypeOptions:[
        {
          label: "贵大",
          value: '1',
        },
        {
          label: "慧研学",
          value: '2',
        },
      ],
    };
  },
  computed: {
    rules() {
        return {
        advertsImg: [{ required: true, message: "请上传广告图", trigger: "change" }],
        advertsType: [{ required: true, message: "请选择广告类型", trigger: "change" }],
        belongType: [{ required: true, message: "请选择小程序", trigger: "change" }],
        advertsName: [{ required: true, message: "请输入广告名称", trigger: "blur" }],
        jumpUrl: [{ required: true, message: "请输入调整链接", trigger: "blur" }],
        sort: [{ required: true, message: "请输入排序权重", trigger: "blur" }],
        showSort: [{ required: true, message: "请输入广告顺序", trigger: "blur" }],
      }
    }
  },
  methods: {
    uploadEnd(val){
      this.setQuery.advertsImg = val.url
    },
    cancel(){
      this.$router.push('/operationManage/advertisingManage/index')
    },
   save() {
      this.$refs.setQuery.validate((v) => {
        if (v) {
          if(this.loading) return
          this.loading = true
          addOrUpdateAdv(this.setQuery).then(res=>{
            this.loading = false

            if(res.state == 'Success'){

              this.$notify({
                  title: '成功',
                  message: '操作成功',
                  type: 'success'
                });

                  this.$router.push('/operationManage/advertisingManage/index')
            }
          })

        }
      });
    },
  },
  created () {
    if(this.$route.query.mode == 'edit'){
        this.setQuery = JSON.parse(sessionStorage.getItem('advItem'))
    }
  },
};
</script>

<style lang="scss" scoped>
.addBanner {
  padding-bottom: 40px;
  .btn-group{
    display: flex;
    align-items: center;
    justify-content: center;
  }
}
</style>
