<template>
  <div>
    <el-dialog  :title="form.id?'编辑证据文献':'添加证据文献'" :visible.sync="showDialog" width="600px" :close-on-click-modal="false"  :before-close="handleClose" append-to-body>
        <el-form :model="form" :rules="rules" ref="form" label-width="80px" label-position="left">
            <template v-if="form.type == 1">
                <div>
                    <el-form-item  label="文献名称" prop="name">
                        <el-input  v-model="form.name" autocomplete="off" placeholder="请输入文献名称"></el-input>
                    </el-form-item>
                    <el-form-item  label="公开日" prop="publicDate">
                        <el-date-picker v-model="form.publicDate" value-format="yyyy-MM-dd" type="date"  placeholder="请选择时间" style="width: 100%;"></el-date-picker>
                    </el-form-item>
                    <el-form-item  label="作者" prop="authorName">
                        <el-input  v-model="form.authorName" autocomplete="off" placeholder="请输入作者名称"></el-input>
                    </el-form-item>
                    <el-form-item  label="附件">
                        <template>
                            <div> 
                                <myUpload :file-list="form.systemFile?[form.systemFile]:[]" :limit="1" :autoUpload="true" @on-change="onChange" @on-remove="onRemove"></myUpload>
                            </div>
                        </template>
                    </el-form-item>
                </div>
            </template>
            <template v-else>
                <div>
                    <el-form-item  label="专利号" prop="name">
                        <el-input  v-model="form.name" autocomplete="off" placeholder="请输入专利号"></el-input>
                    </el-form-item>
                </div>
            </template>
            <el-form-item  label="备注" prop="description">
              <el-input type="textarea" v-model="form.description" autocomplete="off" placeholder="请输入备注"></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="handleClose">取 消</el-button>
          <el-button type="primary" :loading="btnLoading" @click="submit" >确 定</el-button>
        </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  components: {},
  props: {
    projectId:{
        default:0
    }
  },
  data() {
    return {
        form:{},
        showDialog:false,
        btnLoading:false,
        rules: {
            name:[{required : false , message:'请输入文献名称',trigger:'blur'}]
        }
    };
  },
  watch: {},
  computed: {},
  created() {},
  mounted() {},
  methods: {
    //打开弹窗
    open(form){
        this.form = JSON.parse(JSON.stringify(form))
        this.showDialog = true
    },
    //关闭弹窗
    handleClose(){
        this.showDialog = false
    },
    //切换文件
    onChange(file, fileList){
        this.form.systemFile=file.raw
    },
    //移除文件
    onRemove(file, fileList){
        this.form.systemFile=null
    },
    //提交
    submit(){
        this.$refs.form.validate((valid) => {
          if (valid) {
            var guids = this.$commonJS.checkUploadFile([this.form.systemFile])
            if(!guids){
                return false
            }
            this.form.fileGuid = guids.length>0?guids[0]:''
            this.form.projectId = this.projectId
            this.btnLoading = true
            if(this.form.id){
                var a = {
                    updateBatchLiteratureDTOs:[this.form],
                    projectId:this.projectId
                }
                this.$api.updateCompareLiteratureBatch(a).then(response=>{
                    if(response.code == 200){
                        this.$emit('save',true)
                        this.$message.success('编辑成功')
                        this.btnLoading = false
                        this.handleClose()
                    }
                }).catch(error=>{
                    this.$message.error('修改失败')
                    this.btnLoading = false
                })
            }else{
                this.$api.addNotPatent(this.form).then(response=>{
                    if(response.code == 200){
                        this.$emit('save',true)
                        this.$message.success('添加成功')
                        this.btnLoading = false
                        this.handleClose()
                    }
                }).catch(error=>{
                    this.$message.error('添加失败')
                    this.btnLoading = false
                })
            }
          } else {
            return false;
          }
        });
    },
  },
};
</script>
<style lang="scss" scoped>
</style>