<template>
    <div class="responseDialog">
      <el-dialog  :title="(form.id?'编辑':'添加') +type.title" :visible.sync="showDialog" width="700px" :close-on-click-modal="false"  :before-close="handleClose" append-to-body>
        <el-form  :model="form" :rules="rules" ref="form" label-width="120px" label-position="left" v-loading="loading">
          <el-form-item :label="type.time">
            <el-date-picker v-model="form[type.timeField]" value-format="yyyy-MM-dd" type="date"  placeholder="请选择时间" style="width: 100%;"></el-date-picker>
          </el-form-item>
          <template v-if="type.type==0">
            <el-form-item  label="地点">
              <el-input  v-model="form.position" autocomplete="off" placeholder="请输入地点"></el-input>
            </el-form-item>
            <el-form-item  label="参与人">
              <el-input  v-model="form.participator" autocomplete="off" placeholder="请输入参与人"></el-input>
            </el-form-item>
            <el-form-item  label="口审笔录描述">
              <el-input  v-model="form.recordDescription" autocomplete="off" placeholder="请输入口审笔录描述"></el-input>
            </el-form-item>
          </template>
          <template v-if="type.type ==1">
            <div>
                <el-form-item label="发文日">
                    <el-date-picker v-model="form.invalidFileSendDate" value-format="yyyy-MM-dd" type="date"  placeholder="请选择时间" style="width: 100%;"></el-date-picker>
                </el-form-item>
            </div>
          </template>
          <template v-if="type.type ==3">
            <div>
                <el-form-item label="决定书号码">
                    <el-input v-model="form.fileNo" autocomplete="off" placeholder="请输入决定书号码"></el-input>
                </el-form-item>
                <el-form-item label="法律依据">
                    <el-input v-model="form.legalBase" autocomplete="off" placeholder="请输入法律依据"></el-input>
                </el-form-item>
                <el-form-item label="决定要点">
                    <el-input v-model="form.decisionKey" autocomplete="off" placeholder="请输入决定要点"></el-input>
                </el-form-item>
                <el-form-item label="结论">
                    <el-input v-model="form.conclusion" autocomplete="off" placeholder="请输入结论"></el-input>
                </el-form-item>
            </div>
          </template>
          <el-form-item label="附件：">
            <template>
              <div> 
                <myUpload :file-list="form.systemFileList" :autoUpload="true" @on-change="onChange" @on-remove="onRemove"></myUpload>
              </div>
            </template>
          </el-form-item>
          <el-form-item label="备注内容：">
            <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" @click="finish" >确 定</el-button>
        </span>
      </el-dialog>
    </div>
</template>
  
<script>
  export default {
    props:{
        projectId:{
            default:0
        }
    },
    data() {
      return {
        showDialog: false,
        loading: false,
        form: {},
        rules: {},
        file: [],
        isFinish: false,
        type:{},
        types:{
            0:{
                type:'0',
                title:'口审记录',
                time:'口审时间',
                timeField:'oralTrailTime'
            },
            1:{
                type:'1',
                title:'无效请求书',
                time:'提出无效时间',
                timeField:'invalidRequestDate'
            },
            2:{
                type:'2',
                title:'陈述意见书',
                time:'陈述答复时间',
                timeField:'stateReplyTime'
            },
            3:{
                type:'3',
                title:'无效决定书',
                time:'无效决定时间',
                timeField:'invalidDecisionTime'
            },
            4:{
                type:'4',
                title:'行政诉讼书',
                time:'发起诉讼时间',
                timeField:'proceedingTime'
            },
            5:{
                type:'5',
                title:'行政诉讼判决书',
                time:'法院判决时间',
                timeField:'judgmentTime'
            },
            6:{
                type:'6',
                title:'权要修改记录',
                time:'权要修改时间',
                timeField:'claimHistoryTime'
            },
            7:{
                type:'7',
                title:'补充证据和理由记录',
                time:'补充证据和理由时间',
                timeField:'supplyEvidenceTime'
            },
        },
        rules: {
          fileName:[{required : false , message:'请输入文献名称',trigger:'blur'}]
        }
      }
    },
    watch: {
    },
    mounted() {
  
    },
    methods: {
        open(type,form) {
            this.type = this.types[type]
            this.form = JSON.parse(JSON.stringify(form))
            this.showDialog=true
        },
        // 弹窗确定
        finish() {
            this.$refs.form.validate((valid) => {
                if (valid) {
                    var guids = this.$commonJS.checkUploadFile(this.form.systemFileList)
                    if(!guids){
                      return false
                    }
                    this.form.projectId = this.projectId
                    this.form.fileGuids = guids
                    if(!this.projectId){
                      this.$emit('save',this.form)
                      this.handleClose()
                      return false
                    }
                    switch(this.type.type){
                        case '0':
                            this.addOralTrail()
                            break;
                        case '1':
                            this.addInvalidProcess()
                            break;
                        case '2':
                            this.addStateOpinion()
                            break;
                        case '3':
                            this.addInvalidDecisionFile()
                            break;
                        case '4':
                            this.addAdminProceed()
                            break;
                        case '5':
                            this.addAdminProceedJudgment()
                            break;
                        case '6':
                            this.addPatentClaimHistory()
                            break;
                        case '7':
                            this.addSupplyEvidence()
                            break;
                    }
                } else {
                    return false;
                }
            });
            
        },
        // 上传无效请求书
        addInvalidProcess() {
          var api = ''
          if(this.form.id){
            api = 'updateInvalidRequestFile'
          }else{
            api = 'addInvalidRequestFile'
          }
          this.$api[api](this.form).then(response=>{
            if(response.code == 200){
              this.$emit('save',true)
              this.handleClose()
            }
          }) 
        },
        // 上传口审记录
        addOralTrail() {
          var api = ''
          if(this.form.id){
            api = 'updateOralTrail'
          }else{
            api = 'addOralTrail'
          }
          this.$api[api](this.form).then(response=>{
            if(response.code == 200){
              this.$emit('save',true)
              this.handleClose()
            }
          }) 
        },
        // 上传无效决定书
        addInvalidDecisionFile() {
          var api = ''
          if(this.form.id){
            api = 'updateInvalidDecisionFile'
          }else{
            api = 'addInvalidDecisionFile'
          }
          this.$api[api](this.form).then(response=>{
            if(response.code == 200){
              this.$emit('save',true)
              this.handleClose()
            }
          }) 
        },
        // 上传行政诉讼书
        addAdminProceed() {
          var api = ''
          if(this.form.id){
            api = 'updateAdminProceed'
          }else{
            api = 'addAdminProceed'
          }
          this.$api[api](this.form).then(response=>{
            if(response.code == 200){
              this.$emit('save',true)
              this.handleClose()
            }
          }) 
        },
        // 上传行陈述意见书
        addStateOpinion() {
          var api = ''
          if(this.form.id){
            api = 'updateStateOpinion'
          }else{
            api = 'addStateOpinion'
          }
          this.$api[api](this.form).then(response=>{
            if(response.code == 200){
              this.$emit('save',true)
              this.handleClose()
            }
          }) 
        },
        // 上传行陈述意见书
        addAdminProceedJudgment() {
          var api = ''
          if(this.form.id){
            api = 'updateAdminProceedJudgment'
          }else{
            api = 'addAdminProceedJudgment'
          }
          this.$api[api](this.form).then(response=>{
            if(response.code == 200){
              this.$emit('save',true)
              this.handleClose()
            }
          }) 
        },
        //修改权利要求记录
        addPatentClaimHistory() {
          var api = ''
          if(this.form.id){
            api = 'updatePatentClaimHistory'
          }else{
            api = 'addPatentClaimHistory'
          }
          this.$api[api](this.form).then(response=>{
            if(response.code == 200){
              this.$emit('save',true)
              this.handleClose()
            }
          }) 
        },
        //补充证据和理由记录
        addSupplyEvidence() {
          var api = ''
          if(this.form.id){
            api = 'updateSupplyEvidence'
          }else{
            api = 'addSupplyEvidence'
          }
          this.$api[api](this.form).then(response=>{
            if(response.code == 200){
              this.$emit('save',true)
              this.handleClose()
            }
          }) 
        },
        // 关闭弹窗
        handleClose() {
            this.showDialog=false
        },
        //切换文件
        onChange(file, fileList){
            if (file.guid && this.form.systemFileList && this.form.systemFileList.length > 0) {
                let index = this.form.systemFileList.findIndex(item => {
                    return item.uid == file.uid
                })
                if (index != -1) {
                    this.form.systemFileList.splice(index, 1, file)
                } else {
                    this.form.systemFileList.push(file.raw)
                }
            } else {
              if(!this.form.systemFileList){
                this.form.systemFileList = []
              }
                this.form.systemFileList.push(file.raw)
            }
        },
        //移除文件
        onRemove(file, fileList){
            if (file.guid) {
                let a = this.form.systemFileList.findIndex(item => {
                    return item.guid == file.guid
                })
                this.form.systemFileList.splice(a, 1)
            } else {
                let a = this.form.systemFileList.findIndex(item => {
                    return item.uid == file.uid
                })
                this.form.systemFileList.splice(a, 1)
            }
        },

    },
  }
  </script>
  
  <style lang="scss">
  .responseDialog{
    .el-dialog__body{
      padding-bottom: 0px;
    }
  }
  </style>