<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="Type"
            :options="supplierList" label="供应商" :dense="false" class="col-6 q-pb-lg q-pr-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="row wrap" style="display:none;">
          <div class="col-12">
            <div style="display:flex;align-items:center;">
              <a :href="fileObj.FileUrl" style="margin-right:10px;">{{fileObj.FileName}}</a>
              <el-upload class="upload-demo" action="" :show-file-list="false" :http-request="UploadAttachment"
                :multiple="uploadMultple">
                <q-btn color="accent" size="sm" icon="add" label="上传供应商合同" />
              </el-upload>
            </div>
          </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 {
    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: '', //备注
        },
        optionTitle: "",
        saveLoading: false,
        supplierList: [], //供应商下拉数据
        uploadMultple: true,
        fileObj: {
          FileName: '',
          FileUrl: ''
        }
      }
    },
    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;
      }
    }),
    mounted() {
      this.getStudyAbroadList();
      this.initObj()
    },
    methods: {
      //初始化表单
      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;
              }
            }
          })
        } else {
          this.objOption.Id = 0;
          this.objOption.Type = this.Type;
          this.objOption.Name = '';
          this.objOption.SupplierId = 0;
          this.objOption.SupplierContract = '';
          this.objOption.Remark = '';
        }
      },
      //关闭弹窗
      closeSaveForm() {
        this.$emit('close')
        this.persistent = false
      },
      //保存留学就业信息
      saveStudyInfo() {
        // if (this.objOption.SupplierContract == '') {
        //   this.$q.notify({
        //     type: 'negative',
        //     position: "top",
        //     message: `请上传供应商合同!`
        //   })
        //   return;
        // }
        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>
