<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">{{objOption.Id==0?'新增':'修改'}}{{Type==1?'留学':'就业'}}</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">{{Type==1?'留学':'就业'}}信息</div>
        <div class="row wrap">
          <q-input filled stack-label maxlength="30" :dense="false" v-model="objOption.Name" ref="Name"
            class="col-6 q-pr-lg q-pb-lg" label="项目名称" :rules="[val => !!val || '项目名称']" />
          <q-select filled stack-label option-value="Id" option-label="Name" v-model="objOption.SupplierId" ref="SupplierId"
            :options="supplierList" label="供应商" :dense="false" :rules="[val => !!val || '请选择供应商']" class="col-6 q-pb-lg" emit-value map-options />
        </div>
        <div class="row wrap">
          <div class="col-6">
            <q-select filled stack-label option-value="SId" option-label="SName" v-model="objOption.School_Id"
              ref="School_Id" :options="SchoolList" label="所属校区" :dense="false" class="col-6 q-pr-lg q-pb-lg" emit-value
              map-options />
            <div class="col-6">
              <a v-if="objOption.SupplierContract&&isHaveViewContractAction"
                style="margin-left:10px;color:#2961FE;text-decoration:none;" :href="objOption.SupplierContract">查看合同</a>
            </div>
          </div>
        </div>
        <div class="row wrap">
          <div class="col-6 q-pb-lg q-pr-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>
        <div class="row wrap">
          <q-input filled stack-label :dense="false" v-model="objOption.Remark" style="margin-top: 20px" type="textarea"
            class="col-12" label="介绍" maxlength="300" />
        </div>
        <div class="row wrap" style="margin-top:5px;color:red;font-weight:bold;">
          必须上传供应商合同才能上架.
        </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="saveStudyInfo" />
      </q-card-actions>
    </q-card>
  </q-dialog>
</template>
<script>
  import {
    saveStudyAbroad,
    queryStudyAbroadInfo,
    querySupplierList
  } from '../../api/studyabroad/index'
  import {
    UploadSelfFile
  } from '../../api/common/common'
  import {
    getSchoolDropdown,
  } from '../../api/school/index'
  import {
    mapState
  } from "vuex";
  export default {
    props: {
      saveObj: {
        type: Object,
        default: null
      },
      Type: {
        type: Number,
        default: null
      }
    },
    data() {
      return {
        persistent: true,
        objOption: {
          Id: 0,
          Type: 0, //1留学 2就业
          Name: '', //项目名称
          SupplierId: 0, //供应商编号
          SupplierContract: '', //供应商合同
          Remark: '', //备注
          School_Id: -1, //所属校区
          ImgCover:'', //封面图
        },
        optionTitle: "",
        saveLoading: false,
        supplierList: [], //供应商下拉数据
        uploadMultple: true,
        fileObj: {
          FileName: '',
          FileUrl: ''
        },
        SchoolList: [], //校区列表
      }
    },
    computed: mapState({
      //是否有查看合同权限
      isHaveViewContractAction(state) {
        if (state.user.userInfo && state.user.userInfo.ActionMenuList && state.user.userInfo.ActionMenuList.length >
          0) {
          let action = state.user.userInfo.ActionMenuList.find(x => {
            if (x.FunctionCode == "View_Supplier_Contract") {
              return x;
            }
          });
          return action && action.FunctionCode;
        }
        return false;
      }
    }),
    created() {
      this.getSchool()
    },
    mounted() {
      this.getStudyAbroadList();
      this.initObj()
    },
    methods: {
      uploadFile(files) {
        UploadSelfFile("course", files[0], res => {
          if (res.Code == 1) {
            this.objOption.ImgCover = res.FileUrl;
          }
        });
      },
      //获取校区下拉
      getSchool() {
        getSchoolDropdown({}).then(res => {
          if (res.Code == 1) {
            this.SchoolList = res.Data;
            var obj = {
              SName: '请选择',
              SId: -1
            }
            this.SchoolList.unshift(obj);
          }
        })
      },
      //初始化表单
      initObj() {
        if (this.saveObj && this.saveObj.Id > 0) {
          queryStudyAbroadInfo({
            Id: this.saveObj.Id
          }).then(res => {
            if (res.Code == 1) {
              var tempObj = res.Data;
              if (tempObj) {
                this.objOption.Id = tempObj.Id;
                this.objOption.Type = tempObj.Type;
                this.objOption.Name = tempObj.Name;
                this.objOption.SupplierId = tempObj.SupplierId;
                this.objOption.SupplierContract = tempObj.SupplierContract;
                this.objOption.Remark = tempObj.Remark;
                this.objOption.School_Id = tempObj.School_Id;
                this.objOption.ImgCover = tempObj.ImgCover;
              }
            }
          })
        } else {
          this.objOption.Id = 0;
          this.objOption.Type = this.Type;
          this.objOption.Name = '';
          this.objOption.SupplierId = 0;
          this.objOption.SupplierContract = '';
          this.objOption.Remark = '';
          this.objOption.School_Id = -1;
        }
      },
      //关闭弹窗
      closeSaveForm() {
        this.$emit('close')
        this.persistent = false
      },
      //保存留学就业信息
      saveStudyInfo() {
        // if (this.objOption.SupplierContract == '') {
        //   this.$q.notify({
        //     type: 'negative',
        //     position: "top",
        //     message: `请上传供应商合同!`
        //   })
        //   return;
        // }
        this.$refs.Name.validate();
        this.$refs.SupplierId.validate();
        if (!this.$refs.Name.hasError&&!this.$refs.SupplierId.hasError) {
          this.saveLoading = true;
          saveStudyAbroad(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();
          })
        }
      },
      //获取下拉框数据
      getStudyAbroadList() {
        querySupplierList({}).then(res => {
          if (res.Code == 1) {
            this.supplierList = res.Data;
            let obj = {
              Id: 0,
              Name: '请选择'
            }
            this.supplierList.unshift(obj);
          }
        })
      },
      //上传附件
      UploadAttachment(files) {
        UploadSelfFile('Attachment', files.file, res => {
          if (res.Code == 1) {
            this.fileObj.FileName = res.FileName;
            this.fileObj.FileUrl = res.FileUrl;
            this.objOption.SupplierContract = res.FileUrl;
          }
        })
      },
    }
  }

</script>
