<template>
  <div class="addMenu app-container">
    <el-form
      :model="setQuery"
      ref="setQuery"
      :rules="rules"
      label-width="130px"
    >
      <el-form-item label="菜单名称：" prop="menuName">
        <el-input
          v-model="setQuery.menuName"
          class="item-width-300"
        ></el-input>
      </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="菜单上方文字：">
        <el-input
          v-model="setQuery.labelAbove"
          class="item-width-300"
        ></el-input>
      </el-form-item>
      <el-form-item label="菜单下方文字：">
        <el-input
          v-model="setQuery.labelBelow"
          class="item-width-300"
        ></el-input>
      </el-form-item>

       <el-form-item label="域名地址：" prop="domainUrl">
        <el-input
          v-model="setQuery.domainUrl"
          class="item-width-300"
        ></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="status">
        
        <el-switch
        v-model="setQuery.status"
        active-text="显示"
        inactive-text="隐藏"
        :active-value="2"
        :inactive-value="1"
        active-color="#13ce66"
        inactive-color="#ff4949">
        </el-switch>
      </el-form-item>

      <el-form-item label="图标：" prop="imgUrl">
        <Upload
          type="imgUrl"
          id="imgUrl"
          width="150px"
          height="150px"
          uploadType="SHOP_INFO"
          :imgUrl="setQuery.imgUrl"
          @uploadEnd="uploadEnd"
        ></Upload>
      </el-form-item>
    

    </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 {addOrUpdate} from '@/api/appConfig'

export default {
    components: {
        Upload,
    },
  data() {
    return {
        loading:false,
      setQuery: {
        "menuLevel":1,
        "parentId":'',
        "domainUrl": "",
        "id": 0,
        "imgUrl": "",
        "labelAbove": "",
        "labelBelow": "",
        "linkUrl": "",
        "menuCode": "",
        "menuName": "",
        "note": "",
        "secondaryImg": "",
        "sort": 0,
        "status": 0,
        "belongType":'2'
      },
      belongTypeOptions:[
        { 
          label: "贵大",
          value: '1',
        },
        {
          label: "慧研学",
          value: '2',
        },
      ],
      rules: {
        menuName: [{ required: true, message: "请输入菜单名称", trigger: "blur" }],
        domainUrl: [{ required: true, message: "请输入链接地址", trigger: "blur" }],
        belongType: [{ required: true, message: "请选择小程序", trigger: "change" }],
        imgUrl: [{ required: true, message: "请输入图标", trigger: "blur" }],
        sort: [{ required: true, message: "请输入权重", trigger: "blur" }],
        secondaryImg: [{ required: true, message: "请上传分类顶部图", trigger: "change" }],
      },
    };
  },
  methods: {
    uploadEnd(val){
      this.setQuery[val.type] = val.url
    },
    cancel(){
      this.$router.push('/operationManage/appMenuManage/index')
    },
    save() {
      this.$refs.setQuery.validate((v) => {
        if (v) {
          this.loading = true
          addOrUpdate(this.setQuery).then(res=>{
            this.loading = false

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

              this.$notify({
                  title: '成功',
                  message: '操作成功',
                  type: 'success'
                });
               
                  this.$router.push('/operationManage/appMenuManage/index')
            }
          })
              
        }
      });
    },
  },
  created () {
    if(this.$route.query.title == '编辑菜单'){
      this.setQuery = JSON.parse(sessionStorage.getItem('menuItem')) 
    }else{
      this.setQuery.parentId = this.$route.query.parentId
      this.setQuery.menuLevel = this.$route.query.menuLevel || 1
    }
  },
};
</script>

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