<template>
  <q-dialog v-model="persistent" content-class="bg-grey-1" persistent transition-show="scale" transition-hide="scale">
    <q-card style="width: 800px;max-width:900px;">
      <q-card-section>
        <div class="text-h6">{{optionTitle}}</div>
      </q-card-section>
      <q-card-section class="q-pt-none scroll" style="max-height: 70vh">
        <div class="text-caption q-mb-lg q-px-md text-grey-6">招聘类型信息</div>
        <div class="row wrap" style="align-items:end;">
          <q-input filled stack-label maxlength="50" :dense="false" v-model="objOption.TypeName"
            class="col-12 q-pb-lg" label="招聘类型" ref="TypeName" :rules="[val => !!val || '请填写招聘类型']" />
          <div class="col-6 q-pb-lg q-pr-lg">
            <q-uploader :style="{ backgroundImage: 'url(' + objOption.CheckImg + ')' }"
              style="width:auto;height:200px;background-repeat:no-repeat;background-size:cover;" flat hide-upload-btn
              max-files="1" label="选中图" accept=".jpg, image/*" :factory="uploadFile2" auto-upload>
            </q-uploader>
          </div>
          <div class="col-6 q-pb-lg">
            <q-uploader :style="{ backgroundImage: 'url(' + objOption.ImgCover + ')' }"
              style="width:auto;height:200px;background-repeat:no-repeat;background-size:cover;" flat hide-upload-btn
              max-files="1" label="未选中" accept=".jpg, image/*" :factory="uploadFile" auto-upload>
            </q-uploader>
          </div>
        </div>
      </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="closeSaveForm" />
        <q-btn label="立即提交" color="accent q-px-md" style="font-weight:400 !important" :loading="saveLoading"
          @click="saveRecruit" />
      </q-card-actions>
    </q-card>
  </q-dialog>
</template>

<script>
  import {
    SetWebRecruitmentType,
    GetWebRecruitmentType
  } from '../../api/system/webkit'
  import {
    UploadSelfFile
  } from "../../api/common/common";
  export default {
    props: {
      saveObj: {
        type: Object,
        default: null
      }
    },
    data() {
      return {
        persistent: true,
        objOption: {
          Id: 0,
          TypeName: '',
          ImgCover: '',
          CheckImg:''
        },
        optionTitle: "新增招聘类型",
        saveLoading: false,
      }
    },
    mounted() {
      this.initObj()
    },
    methods: {
      //初始化表单
      initObj() {
        if (this.saveObj && this.saveObj.Id > 0) {
          GetWebRecruitmentType({
            Id: this.saveObj.Id
          }).then(res => {
            if (res.Code == 1) {
              this.objOption.Id = res.Data.Id;
              this.objOption.TypeName = res.Data.TypeName;
              this.objOption.ImgCover = res.Data.ImgCover;
              this.objOption.CheckImg = res.Data.CheckImg;
              this.optionTitle = '修改招聘信息'
            }
          })
        }
      },
      uploadFile(files) {
        UploadSelfFile("course", files[0], res => {
          if (res.Code == 1) {
            this.objOption.ImgCover = res.FileUrl;
          }
        });
      },
      uploadFile2(files){
        UploadSelfFile("course", files[0], res => {
          if (res.Code == 1) {
            this.objOption.CheckImg = res.FileUrl;
          }
        });
      },
      //关闭弹窗
      closeSaveForm() {
        this.$emit('close')
        this.persistent = false
      },
      //保存菜单
      saveRecruit() {
        this.$refs.TypeName.validate();
        if (!this.$refs.TypeName.hasError) {
          this.saveLoading = true
          SetWebRecruitmentType(this.objOption).then(res => {
            this.saveLoading = false
            this.$q.notify({
              icon: 'iconfont icon-chenggong',
              color: 'accent',
              timeout: 2000,
              message: '数据保存成功！',
              position: 'top'
            })
            this.$emit("success")
            this.closeSaveForm()
          }).catch(() => {
            this.saveLoading = false
          })
        }
      }
    },
  }

</script>
