<template>
  <div class="main-content abnormal applyAccept">
    <!-- <DetailHead result="查询结果" /> -->
    <div class="content-body">
      <el-form ref="form" :model="form" label-width="210px" label-position="right" :rules="formRules">
        <div class="form">
          <div class="tab">
            <div class="tab-title"><label>主体信息</label></div>
          </div>
          <div class="form-body">
            <el-row>
              <el-form-item label="主体名称">
                <router-link target="_blank" :to="{path:'/comprehensive/details',query:{pripid:tsBizDissentModel.pripid,shrink:true}}" style="color: #1F65FF">
                  {{ tsBizDissentModel.entname }}
                </router-link>
              </el-form-item>
            </el-row>
            <el-row>
              <el-form-item label="统一社会信用代码/注册号" class="bt0">
                {{ tsBizDissentModel.uniscid || tsBizDissentModel.regno }}
              </el-form-item>
            </el-row>
            <el-row>
              <el-form-item label="法定代表人" class="bt0">
                {{ tsBizDissentModel.lerepname }}
              </el-form-item>
            </el-row>
          </div>
        </div>

        <div class="form">
          <div class="tab">
            <div class="tab-title"><label>列入异常信息</label></div>
          </div>
          <div class="form-body">
            <el-table :data="tsAbnList" border style="width: 100%">
              <el-table-column prop="abntime" label="列入日期" min-width="100" />
              <el-table-column prop="spereasons" :show-overflow-tooltip="true" label="列入原因" min-width="200" />
              <el-table-column prop="intoDecorg" label="列入决定机关" min-width="100" />
              <el-table-column prop="writsno" label="列入决定书文号" min-width="100" />
              <el-table-column prop="remdate" label="移出日期" min-width="100" />
              <el-table-column prop="remreasons" :show-overflow-tooltip="true" label="移出原因" min-width="200" />
              <el-table-column prop="reDecorg" label="移出决定机关" min-width="100" />
            </el-table>
          </div>
        </div>

        <div class="form">
          <div class="tab">
            <div class="tab-title"><label>异议信息</label></div>
          </div>
          <el-row v-if="tsAbnModel.operatestatus === '0'">
            <el-form-item label="提示" class="pd0" prop="contacts" style="border-bottom: 0">
              <div style="color: red">该经营异常记录已移出</div>
            </el-form-item>
          </el-row>
          <el-row>
            <el-form-item label="联系人" class="pd0" prop="contacts" style="border-bottom: 0">
              <el-input v-model="form.contacts" placeholder="请输入联系人姓名" style="width:99%;margin-left:5px" />
            </el-form-item>
          </el-row>
          <el-row>
            <el-form-item label="联系电话" class="pd0" prop="telephone" style="border-bottom: 0;">
              <el-input v-model="form.telephone" placeholder="请输入联系电话" style="width:99%;margin-left:5px" @blur="telephoneValid" />
            </el-form-item>
          </el-row>
          <el-row>
            <el-form-item label="备注" class="pd0" prop="applyRemark" style="border-bottom: 0;">
              <el-input v-model="form.applyRemark" maxlength="500" style="width:99%;margin-left:5px" show-word-limit type="textarea" :autosize="{ minRows: 2}" placeholder="请输入备注(选填)" />
            </el-form-item>
          </el-row>
          <el-row>
            <el-form-item label="材料上传" class="upload">
              <el-upload
                :action="uploadPath"
                :data="{ bizseq: bizseq }"
                :on-remove="removeFile"
                :on-success="uploadCallback"
                :on-preview="previewFile"
                :before-upload="beforeFileUpload"
                :before-remove="beforeRemove"
                accept=".doc,.docx,.xlsx,.xls,.pdf,.jpg,.zip,.rar,.7z"
                multiple
                :file-list="fileList"
                style="max-width:800px;display: inline-block"
                class="uploadFileList"
              >
                <div style="display: inline-block;">
                  <el-button size="small" style="display: inline-block;margin-right:10px" type="primary">点击上传</el-button>
                  <div slot="tip" style="display: inline-block;" class="el-upload__tip">只能上传jpg/png/pdf/doc/docx/xls/xlsx/rar/zip/7z文件，且单个文件不能超过20M</div>
                </div>
              </el-upload>
            </el-form-item>
          </el-row>
          <el-row class="p10">
            <el-form-item label="异议核实情况" prop="situation" class="bt0">
              <el-input v-model="form.situation" type="textarea" placeholder="请输入异议核实情况" style="display:inline-block;width:100%" :autosize="{ minRows: 3, maxRows: 6 }" maxlength="500" show-word-limit />
            </el-form-item>
          </el-row>
        </div>

        <div class="form">
          <div class="tab">
            <div class="tab-title"><label>处理意见</label></div>
          </div>
          <div class="form-body history">
            <div v-if="historyLog.length == 0" class="none">暂无历史意见</div>
            <el-steps direction="vertical">
              <el-step v-for="(each,index) in historyLog" :key="'historyLog'+index" :title="each.step + '人：' + each.handler + ' ' + each.step +'日期：' + each.handledate " :description=" each.opinioncontent " status="finish" />
            </el-steps>
          </div>
        </div>
        <div class="form">
          <div class="tab">
            <div class="tab-title"><label>受理意见</label></div>
          </div>
          <div class="form-body">
            <el-row class="isAccept">
              <el-form-item label="是否受理" prop="acceptstat">
                <el-radio v-model="form.acceptstat" label="1">受理</el-radio>
                <el-radio v-model="form.acceptstat" label="0">结束流程</el-radio>
                <!--                <el-radio v-model="form.acceptstat" label="0">拟不予受理</el-radio>-->
              <!-- <el-radio v-model="form.nextNodeID" label="acceptDissentEnter">回退</el-radio> -->
              </el-form-item>
            </el-row>
            <div v-if="form.acceptstat === '1'">
              <el-row>
                <el-form-item label="受理意见" prop="opinion" class="bt0">
                  <el-col :span="24">
                    <el-input v-model="form.opinion" type="textarea" placeholder="请输入受理意见" :autosize="{ minRows: 2, maxRows: 6 }" />
                  </el-col>
                  <el-col :span="12">
                    <label>常用语</label>
                    <el-select v-model="opinion" placeholder="请选择" clearable class="custom" style="width:100%">
                      <el-option
                        v-for="item in opinionOptions"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                      />
                    </el-select>
                  </el-col>
                </el-form-item>
              </el-row>
              <el-row>
                <el-form-item label="异议业务经办人" prop="reviewerId" class="bt0">
                  <el-col :span="12">
                    <el-select v-model="form.reviewerId" style="width:100%" filterable clearable placeholder="请选择异议业务经办人" class="custom">
                      <el-option
                        v-for="item in reviewer"
                        :key="'reviewer'+item.primaryKey"
                        :label="item.name"
                        :value="item.primaryKey"
                      />
                    </el-select>
                  </el-col>
                </el-form-item>
              </el-row>
            </div>
            <div v-if="form.acceptstat === '0'">
              <el-row>
                <el-form-item label="结束原因" prop="opinion" class="bt0">
                  <el-col :span="24">
                    <el-input v-model="form.opinion" type="textarea" placeholder="请输入结束原因" :autosize="{ minRows: 2, maxRows: 6 }" />
                  </el-col>
                </el-form-item>
              </el-row>
            </div>

          </div>
        </div>
      </el-form>
      <div class="submit-box">
        <el-button type="primary" :loading="submitLoading" @click="submitForm">提交</el-button>
        <el-button @click="$router.back()">返回</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import DetailHead from '@/components/DetailHead'
import { removeAttachementById, listAttachmentByModelParam } from '@/api/attachment'
import { updatebizDissent, getBizDissentAndWrirsno } from '@/api/abnormal'
import { queryUsersPermission } from '@/api/user'
import { validBeforeUpload } from '@/api/fileUpload'
import { taskOpinion } from '@/api/task'
import { isMenHu } from '@/utils/utils'

export default {
  components: {
    DetailHead
  },
  data() {
    return {
      tsAbnModel: '',
      tsAbnList: [],
      abnReaCn: '',
      abnTime: '',
      uploadPath: `${process.env.VUE_APP_BASE_API}/attachment/uploadAdvanceWithAttachtype`,
      bizseq: '',
      workflowId: '',
      tasklistid: '',

      tsBizDissentModel: {
        contacts: '', // 联系人
        telephone: '', // 电话
        applyRemark: '' // 备注
      },
      submitLoading: false,
      wrirsno: '',
      contacts: '',
      // 选中数组
      multipleSelection: [],
      form: {
        situation: '',
        contacts: '',
        telephone: '',
        remarks: '',
        dissentid: '',
        enterType: 'company',
        spereasons: '',
        opinion: '',
        acceptstat: '1',
        applyRemark: '' // 申请受理备注
      },
      formRules: {
        telephone: [
          { required: true, message: '请输入联系电话', trigger: 'blur' }
        ],
        contacts: [
          { required: true, message: '请输入联系人姓名', trigger: 'blur' }
        ],
        opinion: [
          { required: true, message: '请输入经办意见', trigger: ['blur', 'change'] }
        ],
        reviewerId: [
          { required: true, message: '请选择审核人' }
        ]
      },

      opinion: '',
      reasonOptions: [],
      opinionOptions: [],
      historyLog: [],
      exmainerOptions: [],
      fileList: [],
      user: JSON.parse(sessionStorage.getItem('user')),
      reviewer: []
    }
  },
  watch: {
    'telephone': {
      handler(newVal, oldVal) {
        this.$refs.caseForm.validateField('telephone')
      },
      deep: true
    },
    // 意见渲染
    opinion() {
      let name = ''
      this.opinionOptions.forEach(each => {
        if (this.opinion === each.value) {
          name = each.label
        }
      })
      this.form.opinion = name
    }
  },
  mounted() {
    this.tasklistid = this.$route.query.tasklistid
    this.bizseq = this.$route.query.bizseq // 获取跳转携带的参数bizseq
    this.workflowId = this.$route.query.workflowId
    // 加载常用语
    this.opinionOptions = this.$util.getCommonWords(this.user.primaryKey)
    // 加载处办记录
    taskOpinion(this.workflowId).then(res => {
      if (res.code === 0) {
        res.data.forEach(each => {
          switch (each.opiniontype) {
            case '0': each.step = '监管所受理'
              break
            case '1': each.step = '受理审核'
              break
            case '2': each.step = '送核准经办'
              break
            case '3': each.step = '异议经办'
              break
            case '4': each.step = '异议审核'
              break
            case '5': each.step = '异议审批'
              break
          }
          this.historyLog.push(each)
        })
      } else {
        this.$message.error('加载处办记录失败')
      }
    })

    // startbizDissent(this.tasklistid, this.abnlistid, 'company').then(res => {
    //   if (res.code === 0) {
    //     this.tsBizDissentModel = res.data.tsBizDissentModel
    //     this.wrirsno = res.data.wrirsno
    //     this.bizseq = res.data.bizseq
    //     this.tasklistid = res.data.tasklistid
    //     this.form.contacts = this.tsBizDissentModel.contacts
    //     this.form.remarks = this.tsBizDissentModel.remarks
    //     this.form.telephone = this.tsBizDissentModel.telephone
    //   }
    // })
    getBizDissentAndWrirsno(this.bizseq).then(res => {
      if (res.code === 0) {
        this.tsBizDissentModel = res.data.tsBizDissentModel
        this.form.contacts = res.data.tsBizDissentModel.contacts
        this.form.telephone = res.data.tsBizDissentModel.telephone
        this.abnReaCn = res.data.tsBizDissentModel.abnReaCn
        this.abnTime = res.data.tsBizDissentModel.abnTime
        this.form.situation = res.data.tsBizDissentModel.situation === null ? '' : res.data.tsBizDissentModel.situation
        this.tsAbnList.push(res.data.tsAbnList)
        this.tsAbnModel = res.data.tsAbnList
        if (this.$util.isNotEmpty(res.data.tsBizDissentModel.acceptstat)) {
          this.$set(this.form, 'acceptstat', res.data.tsBizDissentModel.acceptstat)
        }
        this.form.applyRemark = this.tsBizDissentModel.applyRemark === null ? '' : this.tsBizDissentModel.applyRemark
        this.wrirsno = res.data.wrirsno
      }
    })

    // 加载审核人
    this.loadReviewer()

    // 加载受理环节附件清单
    listAttachmentByModelParam({ bizseq: this.bizseq }).then(data => {
      data.forEach(each => {
        each.id = each.attachmentid
        each.name = each.filename
        each.url = `${process.env.VUE_APP_DOWNLOAD_URL}/` + each.temppath
      })
      this.fileList = data
    })
  },
  methods: {
    // 联系电话校验
    telephoneValid() {
      let flag = false
      if (this.$util.isEmpty(this.form.telephone)) {
        return this.$message.warning('联系电话不能为空')
      }
      if (this.form.telephone.indexOf('1') === 0) { // 验证手机号码
        var mobile = /^[1][3,4,5,6,7,8,9][0-9]{9}$/
        flag = mobile.test(this.form.telephone)
      } else { // 判断是否是座机电话
        var landline = /^(([0\+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/
        flag = landline.test(this.form.telephone)
      }
      if (!flag) {
        return this.$message.warning('请输入正确格式的联系电话')
      }
      return true
    },
    // 上传文件之前校验大小
    beforeFileUpload(file) {
      var that = this
      return validBeforeUpload(file, that)
    },
    // 移除文件前
    beforeRemove(file, fileList) {
      if (file && file.status === 'success') {
        return this.$confirm(`确定移除 ${file.name} ？`)
      }
    },
    // 上传文件预览
    previewFile(file) {
      window.open(file.url, '_blank')
    },
    // 发送提交
    submitForm() {
      if (this.fileList.length === 0) {
        return this.$message.warning('请上传相关核实材料！')
      }
      this.$refs.form.validate(valid => {
        if (!valid) return this.$message.warning('请填写完所有必填信息！')
        const val = this.telephoneValid()
        if (val === true) {
          this.$confirm('是否确认发送？', '提示').then(() => {
            this.submitLoading = true
            this.form.bizseq = this.bizseq
            this.form.workflowid = this.workflowId
            this.form.opiniontype = '0' // 经办人意见 (审批部门)
            this.form.handler = this.user.username
            this.form.handlerid = this.user.primaryKey
            this.form.opinioncontent = this.form.opinion // 意见内容
            this.form.tasklistid = this.tasklistid
            this.form.nextPerformerids = this.form.reviewerId
            this.form.dissentid = this.tsBizDissentModel.dissentid
            this.form.isagree = '1'

            for (const i in this.reviewer) {
              if (this.reviewer[i].primaryKey === this.form.reviewerId) {
                this.form.nextPerformerNames = this.reviewer[i].name
              }
            }
            // form.acceptstat
            if (this.form.acceptstat === '1') { // 受理
              this.form.nextNodeID = 'approveDissentAccept'
            } else if (this.form.acceptstat === '0') {
              this.form.nextNodeID = 'overFinish'
            }
            updatebizDissent(this.form).then(res => {
              this.submitLoading = false
              if (res.code === 0) {
                this.$message.success('提交成功')
                if (isMenHu()) {
                  // 信创门户环境跳转回待办
                  window.location.href = '/aiccs/#/todo/list'
                } else {
                  this.$router.back()
                }
              }
            })
          }).catch(() => {})
        }
      })
    },
    uploadCallback(res, file) {
      if (res.code === 0) {
        file.id = res.data.attachmentid
        this.fileList.push(file)
      }
    },
    // 移除文件回调
    removeFile(file) {
      if (file && file.status === 'success') {
        removeAttachementById(file.id).then(res => {
          if (res.code === 0) {
            this.$message.success('删除成功')
            // 加载受理环节附件清单
            listAttachmentByModelParam({ bizseq: this.bizseq }).then(data => {
              data.forEach(each => {
                each.id = each.attachmentid
                each.name = each.filename
                each.url = `${process.env.VUE_APP_DOWNLOAD_URL}/` + each.temppath
              })
              this.fileList = data
            })
          } else {
            this.$message.error(res.msg)
          }
        })
      }
    },
    // 加载审核人
    loadReviewer() {
      queryUsersPermission('exptDissentCheckEnter', this.bizseq).then(res => {
        if (res.code === 0) {
          this.reviewer = res.data
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.main-content{
  font-size: $table-content-font-size;
  .back-btn{
    position:absolute;
    right:40pt;
  }
  .content-header,.content-body{
    background: white;
    padding:20PX 10PX;
    border:1PX solid #eee;
  }
  .content-header{
    height:60PX;
    display: flex;
    align-items: center;
    .title{
      font-weight: 700;
      font-size: $font-size-sub-title;
    }
  }
  .content-body{
    padding:20pt;
      .tab{
        .tab-title{
          label{
            display:inline-block;
          }
        }
      }
    .form{
      margin-bottom: 20pt;
    }
  }

}
.el-row{
  border-left: 1px solid $color-border;
}
.bt0 {
  /deep/.el-form-item__label{
    border-top: 0!important;
  }
  /deep/.el-form-item__content{
    border-top: 0!important;
  }
}
</style>
<style lang="scss">
.main-content.abnormal.applyAccept{
  .el-form-item{
    margin-bottom:0;
    background: $color-form-label;
    .el-form-item__label{
      padding: 20px 10px 10px 10px;
      line-height: 0!important;
      border-top: 1px solid $color-border;
      font-size: $table-content-font-size;
    }
    .el-form-item__content{
      background: white;
      padding: 10px;
      font-size: $table-content-font-size;
      border-top: 1px solid $color-border;
      border-right: 1px solid $color-border;
      border-left: 1px solid $color-border;
    }
  }
  .pd0{
      .el-form-item__content{
        .el-input__inner{
          margin-bottom: 0px;
        }
      }
    }
  .el-form-item:last-child{
    border-bottom: 1px solid $color-border;
  }
  .form-body.history{
    padding:15px;
    border:1px solid $color-border;
    font-size: $table-content-font-size;
  }
  .form-body.history .none{
    font-size: $table-content-font-size;
  }
  .el-form-item.is-error .custom .el-input__inner{
    border-color:$color-border;
  }
  .el-table-column--selection .cell {
    padding-right: 0px;
  }
  .no {
    padding-left: 0px;
  }
  .el-table_9_column_61.is-left.is-leaf{
    .cell{
      padding-left: 0px;
      padding-right: 0px;
    }
  }
  .el-pagination{
    text-align: right;
  }
  .dialog-box{
    text-align: right;
  }
  .el-textarea.is-disabled .el-textarea__inner{
    color: #1c1e23;
  }
  .el-input__inner{
    margin-bottom: 5px;
  }
  .uploadFileList{
    .el-upload-list__item{
      display: inline-block;
      width: auto;
    }
  }
  .el-step__description{
    font-size: 18px;
  }
}
.isAccept .el-form-item__content{
  padding: 0 10px!important;
}
</style>
