<template>
  <div>
    <q-dialog v-model="persistent" persistent content-class="bg-grey-1" transition-show="scale" transition-hide="scale">
        <q-card style="width: 450px; max-width: 450px">
        <q-card-section>
            <div class="text-h6">
            {{ objOption.Id == 0 ? "新增小程序信息" : "修改小程序信息" }}
            </div>
        </q-card-section>
        <q-card-section class="q-pt-none scroll" style="max-height: 70vh">
            <div class="row wrap">
            <q-input filled stack-label :dense="false" v-model="objOption.Name" ref="Name"
                class="col-12 q-pb-lg" label="小程序名称" :rules="[(val) => !!val || '请填写小程序名称']" />
            <q-input filled stack-label :dense="false" v-model="objOption.AppID" ref="AppID"
                class="col-12 q-pb-lg" label="AppID" :rules="[(val) => !!val || '请填写小程序AppID']" />
            </div>
            <div style="width:100px;height:100px;border:1px solid #d1d1d1;text-align:center;line-height:100px;cursor:pointer;" @click="getImgChoice=true">
                <i class="el-icon-plus avatar-uploader-icon" style="font-size: 30px;"
                v-if="objOption.Image==''"></i>
                <q-img v-else :src="objOption.Image" style="width: 100%;height: 100%;">
                </q-img>
            </div>
            <q-input filled stack-label :dense="false" v-model="objOption.Page" ref="Page"
                class="col-12 q-pt-lg" label="路径"/>
        </q-card-section>
        <q-separator />
        <q-card-actions align="right" class="bg-white">
            <q-btn label="取消" flat color="grey-10" style="font-weight: 400 !important" @click="closeCourseForm" />
            <q-btn label="立即提交" color="accent q-px-md" style="font-weight: 400 !important" :loading="saveCourseLoading"
            @click="saveCourse" />
        </q-card-actions>
        </q-card>
    </q-dialog>
        <!-- 选择文件 -->
    <q-dialog title="选择文件" v-model="getImgChoice" width="1200px">
      <ChooseImg @SelectId="SelectId" :type="0" @closeDia="getImgChoice=false"></ChooseImg>
    </q-dialog>
  </div>
</template>

<script>
  import {
    SetContributeApplet
  } from "../../api/system/wechat";
  import ChooseImg from '../../pages/teacher/components/ChooseImg'
  export default {
    components: {
      ChooseImg
    },
    props: {
      saveObj: {
        type: Object,
        default: null,
      },
    },
    data() {
      return {
        persistent: true,
        saveCourseLoading:false,
        getImgChoice:false,
        objOption: {
          Id: 0,
          Name: "", //小程序名称
          AppID: "", //	appid
          Image: "", //图标
          Page: '' //路径
        }
      };
    },
    created() {
    },
    mounted() {
        if(this.saveObj){
            this.objOption.Id = this.saveObj.Id;
            this.objOption.Name = this.saveObj.Name;
            this.objOption.AppID = this.saveObj.AppID;
            this.objOption.Image = this.saveObj.Image;
            this.objOption.Page = this.saveObj.Page;
        }
    },
    methods: {
      SelectId(obj) {
        this.objOption.Image = obj[0].ImageUrl;
        this.getImgChoice = false;
      },
      //关闭弹窗
      closeCourseForm() {
        this.$emit("close");
        this.persistent = false;
      },
      //保存菜单
      saveCourse() {
        this.$refs.Name.validate();
        this.$refs.AppID.validate();
        if (
          !this.$refs.Name.hasError &&
          !this.$refs.AppID.hasError
        ) {
          this.saveCourseLoading = true;
          SetContributeApplet(this.objOption)
            .then((res) => {
              this.saveCourseLoading = false;
              this.$q.notify({
                icon: "iconfont icon-chenggong",
                color: "accent",
                timeout: 2000,
                message: "数据保存成功！",
                position: "top",
              });
              this.$emit("success");
              this.$emit("close");
            })
            .catch(() => {
              this.saveCourseLoading = false;
            });
        }
      }
    },
  };

</script>
