<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">
          <el-form-item label="案件阶段">
            <span>{{ type.casePhase }}</span>
          </el-form-item>
          <template v-if="type.type==17 || type.type==27 ||type.type ==28">
            <el-form-item label="案件子阶段">
              <el-input  v-model="form.caseChildPhase" autocomplete="off" placeholder="请输入案件子阶段名称"></el-input>
            </el-form-item>
            <el-form-item label="文档类型">
              <el-radio-group v-model="form.documentType">
                <el-radio label="收文">收文</el-radio>
                <el-radio label="交文">交文</el-radio>
                <el-radio label="内部存档">内部存档</el-radio>
              </el-radio-group>
            </el-form-item>
          </template>
          <template v-else>
            <el-form-item label="案件子阶段" v-if="type.type!=6">
              <span>{{ type.caseChildPhase }}</span>
            </el-form-item>
            <el-form-item label="文档类型">
              <span>{{ type.documentType }}</span>
            </el-form-item>
          </template>
          <el-form-item :label="type.time || (form.documentType?`${form.documentType}日`:'时间')">
            <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 == 15 || type.type == 25">
            <el-form-item  label="应到时间">
              <el-date-picker type="datetime" placeholder="请选择" value-format="yyyy-MM-dd HH:mm" format="yyyy-MM-dd HH:mm" v-model="form.beTime" style="width: 100%"></el-date-picker>
            </el-form-item>
            <el-form-item  label="应到处所">
              <el-input type="textarea" v-model="form.bePosition" autocomplete="off" placeholder="请输入应到处所"></el-input>
            </el-form-item>
          </template>
          <template v-if="type.type == 31">
            <el-form-item  label="口审日期">
              <el-date-picker type="datetime" placeholder="请选择" value-format="yyyy-MM-dd HH:mm" format="yyyy-MM-dd HH:mm" v-model="form.oralDate" style="width: 100%"></el-date-picker>
            </el-form-item>
          </template>
          <template v-if=" type.type == 31">
            <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>
          </template>
          <template v-if="type.type==6">
            <!-- <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 ==7">
            <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="无效结果" prop="cronIds">
                  <el-checkbox-group v-model="form.cronIds" class="currentCheckbox">
                      <el-checkbox v-for="item in conclusion" :key="parseInt(item.value)" :label="parseInt(item.value)">{{
                      item.label }}</el-checkbox>
                  </el-checkbox-group>
                </el-form-item>
                <el-form-item label="是否提行政诉讼及理由">
                  <el-input type="textarea" v-model="form.invalidStageReason" autocomplete="off"  placeholder="请输入是否提行政诉讼及理由"></el-input>
                </el-form-item>
            </div>
          </template>
          <template v-if="type.type ==16 || type.type ==26">
            <div>
              <el-form-item label="行诉结果" prop="conclusion">
                  <el-radio-group v-model="form.conclusion">
                      <el-radio v-for="(value,key) in litigationResult[type.type]" :key="key" :label="key">{{
                        value }}</el-radio>
                  </el-radio-group>
                </el-form-item>
            </div>
          </template>
          <template v-if="type.type ==17 || type.type ==27 ||type.type ==28">
            <div>
                <el-form-item label="文档名称">
                  <el-input v-model="form.documentName" autocomplete="off" placeholder="请输入文档名称"></el-input>
                </el-form-item>
            </div>
          </template>
          <template v-if="actType == 0 && evidenceRiskFlowType.indexOf(Number(type.type)) != -1">
            <div>
                <el-form-item label="证据风险及应对措施">
                  <el-input type="textarea" v-model="form.evidenceRiskResponse" autocomplete="off"  placeholder="请输入证据风险及应对措施"></el-input>
                </el-form-item>
            </div>
          </template>
          <template v-if="type.type == 16">
            <div>
                <el-form-item label="是否提行政诉讼二审及理由">
                  <el-input type="textarea" v-model="form.firstInstanceStageReason" 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>
          <template v-if="$permission.FunPermissions('xiaoshi/sendEmail')">
            <el-form-item label="邮件通知：">
              <div style="min-height: 40px;display:flex;align-items:center;flex-wrap: wrap;">
                <el-switch
                  v-model="form.ifSendEmail"
                  active-color="#13ce66"
                  inactive-color="#ff4949"
                  :active-value="true"
                  :inactive-value="false">
                </el-switch>
                <template v-if="form.ifSendEmail">
                  <div class="margin-left_10" v-for="item in sendEmailPerson" :key="item.email">{{ item.name +'<' + item.email + '>'}}</div>
                </template>
                
              </div>
              
            </el-form-item>
            <el-form-item label="临时通知人员：" v-if="form.ifSendEmail">
              <div style="display:flex;align-items:center;flex-wrap: wrap;">
                <div v-for="(item,index) in otherPerson" :key="index" class="otherPerson">
                  <div v-if="item.name">{{ item.name +'<' + item.email + '>'}}</div>
                  <div v-else>{{ item.email }}</div>
                  <div class="delOtherPerson">
                    <i class="el-icon-close" style="color:red" @click="delOtherPerson(item,index)"></i>
                  </div>
                </div>
                <div class="margin-left_10">
                  <el-button size="small" @click="addNewEmail" icon="el-icon-plus">添加</el-button>
                </div>
              </div>
            </el-form-item>
          </template>
          
          <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" :loading="loading" @click="finish" >确 定</el-button>
        </span>
      </el-dialog>

      <addEmailVue ref="addEmailVue" @newEmail="newEmail"></addEmailVue>
    </div>
</template>
  
<script>
import addEmailVue from '../IPREmail/components/dialog/addEmail.vue'
const defaultSendEmail = ['6','1','7','8','16','3','17','27','28']
  export default {
    components:{
      addEmailVue
    },
    props:{
        projectId:{
          default:0
        },
        actType:{}
    },
    data() {
      return {
        showDialog: false,
        loading: false,
        form: {},
        rules: {
          name: [
            { required: true, message: '请输入案件子阶段名称', trigger: 'blur' }
          ],
          cronIds:[
            { required: true, message: '请选择无效结果', trigger: 'change' }
          ],
          conclusion:[
            { required: true, message: '请选择行诉结果', trigger: 'change' }
          ]
        },
        file: [],
        isFinish: false,
        type:{},
        types:{
            6:{
                type:'6',
                title:'口审记录',
                time:'口审时间',
                timeField:'oralTrailTime'
            },
            31:{
                type:'31',
                title:'口审记录',
                time:'收文日',
                timeField:'oralTrailTime'
            },
            1:{
                type:'1',
                title:'无效请求书',
                time:'提出无效时间',
                timeField:'invalidRequestDate'
            },
            5:{
                type:'5',
                title:'陈述意见书',
                time:'陈述答复时间',
                timeField:'stateReplyTime'
            },
            7:{
                type:'7',
                title:'无效决定书',
                time:'无效决定时间',
                timeField:'invalidDecisionTime'
            },
            8:{
                type:'8',
                title:'行政诉讼书',
                time:'发起诉讼时间',
                timeField:'proceedingTime'
            },
            18:{
                type:'18',
                title:'行政诉讼书',
                time:'发起诉讼时间',
                timeField:'proceedingTime'
            },
            16:{
                type:'16',
                title:'行政诉讼判决书',
                time:'法院判决时间',
                timeField:'judgmentTime'
            },
            26:{
                type:'26',
                title:'行政诉讼判决书',
                time:'法院判决时间',
                timeField:'judgmentTime'
            },
            2:{
                type:'2',
                title:'权要修改记录',
                time:'权要修改时间',
                timeField:'claimHistoryTime'
            },
            3:{
                type:'3',
                title:'补充证据和理由记录',
                time:'补充证据和理由时间',
                timeField:'supplyEvidenceTime'
            },
            17:{
                type:'17',
                title:'其他文档',
                time:'文档时间',
                timeField:'documentTime'
            },
            27:{
                type:'27',
                title:'其他文档',
                time:'文档时间',
                timeField:'documentTime'
            },
            28:{
                type:'28',
                title:'其他文档',
                time:'文档时间',
                timeField:'documentTime'
            },
        },
        sendEmailPerson:[],
        otherPerson:[],
        litigationResult:{
          16:{},
          26:{}
        },
        evidenceRiskFlowType:[29,30],
      }
    },
    watch: {
    },
    computed:{
      conclusion() {
        return this.$store.state.dictMessage.dictMessage.REINVALID_ASSESS
      },
    },
    created() {
      let litigationResult = this.$constants.litigationResult
      let r16 = ['1','2','3']
      let r26 = ['4','5','6']
      for(let i = 0;i<r16.length;i++){
        this.litigationResult['16'][r16[i]] = litigationResult[r16[i]]
      }
      for(let i = 0;i<r26.length;i++){
        this.litigationResult['26'][r26[i]] = litigationResult[r26[i]]
      }
    },
    mounted() {
  
    },
    methods: {
        open(type,form) {
          this.changeEvidenceRisk = false
            let type1 = this.types[type.flowType] || {
              type:type.flowType,
              time:type.documentType? `${type.documentType}日`:'时间',
              timeField:'occurredTime'
            }
            this.type = {
              ...type1,
              title : type.flowName,
              ...type
            }
            if(!form.cronIds){
              form.cronIds = []
            }
            this.form = JSON.parse(JSON.stringify(form))
            if(type.id){
              this.form.assoCasePhaseId = type.id
            }
            if(type.flowType){
              this.form.flowType = type.flowType
            }
            this.otherPerson = []
            if(this.$permission.FunPermissions('xiaoshi/sendEmail')){
              this.$set(this.form,'ifSendEmail',true)
              // if(defaultSendEmail.indexOf(type.flowType+'')!=-1){
              //   this.$set(this.form,'ifSendEmail',true)
              // }else{
              //   this.$set(this.form,'ifSendEmail',false)
              // }
              this.getSendEmailPerson()
            }
            this.showDialog=true
        },
        //添加临时发送邮箱
        addNewEmail(){
          this.$refs.addEmailVue.open()
        },
        newEmail(data){
          if(!data.email){
            return
          }
          data.email = data.email.trim()
          var index = this.otherPerson.findIndex(item=>{
            return item.email == data.email
          })
          if(index == -1){
            this.otherPerson.push(data)
          }else{
            this.$message.warning('该邮箱已存在')
          }
          
        },
        //删除临时发送邮箱
        delOtherPerson(item,index){
          this.otherPerson.splice(index,1)
        },
        //获取需要发送邮件的人员
        async getSendEmailPerson(){
          var [defaultPerson,handlePerson] = await Promise.allSettled([this.getDefaultPersons(),this.getHandlePerson()])
          defaultPerson = defaultPerson.status =='fulfilled'?defaultPerson.value:{}
          handlePerson = handlePerson.status =='fulfilled'?handlePerson.value:{}
          var data = []
          if(defaultPerson.data && defaultPerson.data.length>0){
            data = defaultPerson.data
          }
          if(handlePerson && handlePerson.length){
            for(let i = 0;i<handlePerson.length;i++){
              let handlePerson1 = handlePerson[i]
              if(handlePerson1.isDelete){
                continue;
              }
              let index = data.findIndex(item=>{
                return item.id == handlePerson1.iprPersonId
              })
              if(index == -1){
                data.push(
                  {
                    id:handlePerson1.iprPersonId,
                    name:handlePerson1.name,
                    email:handlePerson1.email
                  }
                )
              }
            }
          }
          this.sendEmailPerson = data
        },
        //获取默认发送邮件人员
        getDefaultPersons(){
            var params = {
                current:1,
                size:100,
                ifDefault:true
            }
            return this.$api.iprPersonQuery(params).then(response=> response.data)
        },
        //获取配案人员
        getHandlePerson(){
            var params = {
              reportId:this.projectId
            }
            return this.$api.matchCasePersonQuery(params).then(response=> response.data)
        },
        // 弹窗确定
        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.extraEmailDTOS = this.otherPerson
                    this.form.fileGuids = guids
                    if(!this.projectId){
                      this.$emit('save',this.form)
                      this.handleClose()
                      return false
                    }
                    this.loading = true
                    switch(this.type.type + ''){
                        case '6':
                        case '31':
                            this.addOralTrail()
                            break;
                        case '1':
                            this.addInvalidProcess()
                            break;
                        case '5':
                            this.addStateOpinion()
                            break;
                        case '7':
                            this.addInvalidDecisionFile()
                            break;
                        case '8':
                        case '18':
                            this.addAdminProceed()
                            break;
                        case '16':
                        case '26':
                            this.addAdminProceedJudgment()
                            break;
                        case '2':
                            this.addPatentClaimHistory()
                            break;
                        case '3':
                            this.addSupplyEvidence()
                            break;
                        case '17':
                        case '27':
                        case '28':
                            this.addOtherDocuments()
                            break;
                        default:
                            this.addReportAffairCase()
                          break;
                    }
                } else {
                    return false;
                }
            });
            
        },
        //其他文档信息的上传
        addReportAffairCase(){
          var api = ''
          if(this.form.id){
            api = 'updateReportAffairCase'
          }else{
            api = 'addReportAffairCase'
          }
          this.$api[api](this.form).then(response=>{
            if(response.code == 200){
              this.$emit('save',this.form)
              this.handleClose()
            }
          }).catch(error=>{
            this.loading = 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',this.form)
              this.handleClose()
            }
          }).catch(error=>{
            this.loading = false
          })  
        },
        // 上传口审记录
        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',this.form)
              this.handleClose()
            }
          }).catch(error=>{
            this.loading = false
          })  
        },
        // 上传无效决定书
        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',this.form)
              this.handleClose()
            }
          }).catch(error=>{
            this.loading = false
          })  
        },
        // 上传行政诉讼书
        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',this.form)
              this.handleClose()
            }
          }).catch(error=>{
            this.loading = false
          })  
        },
        // 上传行陈述意见书
        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',this.form)
              this.handleClose()
            }
          }).catch(error=>{
            this.loading = false
          })  
        },
        // 上传行陈述意见书
        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',this.form)
              this.handleClose()
            }
          }).catch(error=>{
            this.loading = false
          })  
        },
        // 上传其他文档
        addOtherDocuments() {
          var api = ''
          if(this.form.id){
            api = 'updateOtherDocuments'
          }else{
            api = 'addOtherDocuments'
          }
          this.$api[api](this.form).then(response=>{
            if(response.code == 200){
              this.$emit('save',this.form)
              this.handleClose()
            }
          }).catch(error=>{
            this.loading = false
          })  
        },
        //修改权利要求记录
        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',this.form)
              this.handleClose()
            }
          }).catch(error=>{
            this.loading = false
          }) 
        },
        //补充证据和理由记录
        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',this.form)
              this.handleClose()
            }
          }).catch(error=>{
            this.loading = false
          }) 
        },
        // 关闭弹窗
        handleClose() {
          this.loading = false
            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
                })
                if(a!=-1){
                  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>
  <style lang="scss" scoped>
    .otherPerson{
      display: flex;
      align-items: center;
      cursor: pointer;
      .delOtherPerson{
        opacity: 0;
        margin-left:10px
      }
    }
    .otherPerson:hover{
      .delOtherPerson{
        opacity: 1;
      }
    }
  </style>