<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="300" :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 />
            <a v-if="objOption.SupplierContract&&isHaveViewContractAction"
              style="margin-left:10px;color:#2961FE;text-decoration:none;" :href="objOption.SupplierContract">查看合同</a>
          </div>
          <div class="col-6">
            <q-select filled stack-label option-value="Id" option-label="Name" v-model="objOption.ProductType"
              ref="ProductType" :options="productList" label="产品类型" :dense="false" class="col-6 q-pb-lg" emit-value
              map-options />
          </div>
        </div>
         <div class="row wrap">
          <div class="col-6">
            <q-select filled stack-label option-value="ID" option-label="Name" v-model="objOption.StudyCountryId"
              ref="StudyCountryId" :options="CountryList" label="留学国家" :dense="false" class="col-6 q-pr-lg q-pb-lg" emit-value
              map-options />
            
          </div>
          <div class="col-6">
        
          </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="text-caption q-my-md q-px-xs text-grey-6">特色</div>
        <div class="row wrap">
          <ext-editor :defaultMsg="objOption.StudyFeature" classStr="col-12" @getEditValue="getFeatureValue">
          </ext-editor>
        </div>
        <div class="text-caption q-my-md q-px-xs text-grey-6">介绍</div>
        <div class="row wrap">
          <ext-editor :defaultMsg="objOption.Remark" classStr="col-12" @getEditValue="getEditValue"></ext-editor>
        </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,
    queryStudyAbroadProductType
  } from '../../api/studyabroad/index'
  import {
    UploadSelfFile,
    queryCountryList
  } from '../../api/common/common'
  import {
    getSchoolDropdown,
  } from '../../api/school/index'
  import {
    mapState
  } from "vuex";
  import extEditor from "../common/ext-editor";
  export default {
    props: {
      saveObj: {
        type: Object,
        default: null
      },
      Type: {
        type: Number,
        default: null
      }
    },
    components: {
      extEditor,
    },
    data() {
      return {
        persistent: true,
        objOption: {
          Id: 0,
          Type: 0, //1留学 2就业
          Name: '', //项目名称
          SupplierId: 0, //供应商编号
          SupplierContract: '', //供应商合同
          Remark: '', //备注
          School_Id: -1, //所属校区
          ImgCover: '', //封面图
          ProductType: 1, //产品类型
          StudyFeature: "", //课程特色
          StudyCountryId:"",//留学国家
        },
        optionTitle: "",
        saveLoading: false,
        supplierList: [], //供应商下拉数据
        uploadMultple: true,
        productList: [], //产品类型
        fileObj: {
          FileName: '',
          FileUrl: ''
        },
        SchoolList: [], //校区列表
        CountryList:[],//国家列表
      }
    },
    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();
      this.getProductList();
      this.getCountryList()
    },
    mounted() {
      this.getStudyAbroadList();
      this.initObj()
    },
    methods: {
      //获取国家列表
      getCountryList() {
        queryCountryList({}).then(res => {
          if (res.Code == 1) {
            this.CountryList=res.Data;
          }
        });
      },
      //课程特色
      getFeatureValue(obj) {
        this.objOption.StudyFeature = obj;
      },
      //获取编辑器值
      getEditValue(obj) {
        this.objOption.Remark = obj;
      },
      //获取产品类型列表
      getProductList() {
        queryStudyAbroadProductType({}).then(res => {
          if (res.Code == 1) {
            this.productList = res.Data
          }
        })
      },
      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;
                this.objOption.ProductType = tempObj.ProductType;
                this.objOption.StudyFeature = tempObj.StudyFeature;
                this.objOption.StudyCountryId=tempObj.StudyCountryId;
              }
            }
          })
        } 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;
          this.objOption.ProductType = 1;
          this.objOption.StudyFeature = "";
          this.objOption.StudyCountryId="";
        }
      },
      //关闭弹窗
      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>