<template>
  <div class="addBanner app-container">
    <el-form
      :model="setQuery"
      ref="setQuery"
      :rules="rules"
      label-width="130px"
    >
      <el-form-item label="名称：" prop="bannerName">
        <el-input
          v-model="setQuery.bannerName"
          class="item-width-300"
        ></el-input>
      </el-form-item>
      <el-form-item label="所属平台" prop="platformType">
         <el-select
          v-model="setQuery.platformType"
          size="small"
          clearable
          class="item-width-300"
          placeholder="请选择主商户"
        >
          <el-option
            v-for="item in masterShopOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
     <el-form-item label="banner图：" prop="bannerImg">
        <Upload
          type="bannerImg"
          id="bannerImg"
          width="300px"
          height="200px"
          uploadType="SHOP_INFO"
          :imgUrl="setQuery.bannerImg"
          @uploadEnd="uploadEnd"
        ></Upload>
      </el-form-item>
       <el-form-item label="栏目：" prop="menuId">
        <el-select
          v-model="setQuery.menuId"
          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-radio-group v-model="setQuery.jumpSet">
          <el-radio :label="1">不跳转</el-radio>
          <el-radio :label="2">跳转</el-radio>
        </el-radio-group>
        <div v-if="setQuery.jumpSet == 2">
        <el-input
          v-model="setQuery.jumpUrl"
          class="item-width-300"
          placeholder="请输入跳转链接"
        ></el-input>
        </div>
      </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>

    <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 {addOrUpdateBanner,getMenuName} from '@/api/appConfig'
export default {
    components: {
        Upload,
    },
  data() {
    return {
        loading:false,
        setQuery: {
          platformType:'',
          "bannerImg": "",
          "bannerName": "",
          "id": 0,
          "jumpSet": 1,
          "jumpUrl": "",
          "menuId": '',
          "colour": '',
          "type": '1',
          "sort": 0
        },
      options:[]
    };
  },
  computed: {
    rules() {
      if(this.setQuery.jumpSet == 1){
        return {
        platformType: [{ required: true, message: "请选择所属平台", trigger: "change" }],
        bannerImg: [{ required: true, message: "请上传banner图", trigger: "change" }],
        bannerName: [{ required: true, message: "请输入banner名称", trigger: "blur" }],
        colour: [{ required: true, message: "请输入banner主题色", trigger: "blur" }],
        menuId: [{ required: true, message: "请选择栏目", trigger: "change" }],
        sort: [{ required: true, message: "请输入排序", trigger: "blur" }],
      }
      }else{
         return {
        platformType: [{ required: true, message: "请选择所属平台", trigger: "change" }],
        bannerImg: [{ required: true, message: "请上传banner图", trigger: "change" }],
        bannerName: [{ required: true, message: "请输入banner名称", trigger: "blur" }],
        colour: [{ required: true, message: "请输入banner主题色", trigger: "blur" }],
        jumpUrl: [{ required: true, message: "请输入跳转链接", trigger: "blur" }],
        menuId: [{ required: true, message: "请选择栏目", trigger: "change" }],
        sort: [{ required: true, message: "请输入排序", trigger: "blur" }],
      }
      }
    }
  },
  methods: {
    uploadEnd(val){
      this.setQuery.bannerImg = val.url
    },
    getMenuName(){
      getMenuName({}).then(res=>{
        if(res.state == 'Success'){
          for (const key in res.content) {
            if (res.content.hasOwnProperty.call(res.content, key)) {
                this.options.push({
                  label:res.content[key],
                  value:Number(key) 
                })              
            }
          }
          
        }
      })
    },
   save() {
      this.$refs.setQuery.validate((v) => {
        if (v) {
          this.loading = true
          addOrUpdateBanner(this.setQuery).then(res=>{
            this.loading = false

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

              this.$notify({
                  title: '成功',
                  message: '操作成功',
                  type: 'success'
                });
               
                  this.$router.push('/operationManage/bannerManage/index')
            }
          })
              
        }
      });
    },
  },
  created () {
    this.getMenuName()
    if(this.$route.query.title == '编辑banner'){
     this.setQuery = JSON.parse(sessionStorage.getItem('bannerItem')) 
    }
  },
};
</script>

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