<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="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>

    </el-form>

    <div class="btn-group">
       <el-button>取 消</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
        },
      options:[
        {
          label:'首页Banner',
          value:1
        },
        {
          label:'首页广告位',
          value:2
        }
      ]
    };
  },
  computed: {
    rules() {
        return {
        advertsImg: [{ required: true, message: "请上传广告图", trigger: "change" }],
        advertsType: [{ 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
    },
   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>