<template>
  <div class="main-content abnormal">
    <!-- <div class="content-header">
      <span class="title">农专社经营异常名录列入：审批</span>
      <el-button type="warning" icon="el-icon-caret-left" size="mini" class="back-btn" @click="$router.back()">返回上一层</el-button>
    </div> -->
    <!-- <DetailHead /> -->
    <div class="content-body">
      <el-form ref="form" :model="form" label-width="180px" label-position="right" :rules="formRules">
        <div class="form">
          <div v-if="bizList.length === 1" class="tab">
            <div class="tab-title">
              <label>
                主体信息
              </label>
            </div>
          </div>
          <div class="form-body">
            <el-row v-if="bizList.length === 1">
              <el-col :span="12">
                <el-form-item label="统一社会信用代码" class="bt0">
                  <span>{{ bizList[0].uniscid }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="主体名称" class="bt0">
                  {{ bizList[0].entname }}
                </el-form-item>
              </el-col>
            </el-row>
            <el-row v-if="bizList.length === 1">
              <el-col :span="24">
                <el-form-item label="经营地址" class="bt0">
                  <span>{{ bizList[0].address }}</span>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row v-if="bizList.length === 1">
              <el-col :span="12">
                <el-form-item label="成立日期" class="bt0">
                  <span>{{ bizList[0].estDate }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="法定代表人" class="bt0">
                  {{ bizList[0].lerepname }}
                </el-form-item>
              </el-col>
            </el-row>
            <el-row v-if="bizList.length === 1">
              <el-col :span="12">
                <el-form-item label="主体类型" class="bt0">
                  <span>{{ bizList[0].entTypeName }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="主体状态" class="bt0">
                  {{ bizList[0].regstateCn }}
                </el-form-item>
              </el-col>
            </el-row>
          </div>
          <div class="tab">
            <div class="tab-title"><label>异常信息</label></div>
          </div>
          <div class="form-body">
            <el-row class="lh0">
              <el-form-item v-if="searchNameP === '' && bizList.length > 1" label="列入异常主体信息">
                <el-button size="medium" type="primary" @click="showEntbaseList">查看异常主体名单<i class="el-icon el-icon-s-order" style="margin-left:5px" /></el-button>
                <el-button size="medium" type="primary" @click="exportEntListExcel">导出Excel<i class="el-icon" style="margin-left:5px" /></el-button>
                <el-button v-if="remList.length > 0" size="medium" type="primary" @click="showRemEntList">查看已剔除名单<i class="el-icon el-icon-s-order" style="margin-left:5px" /></el-button>
              </el-form-item>
            </el-row>
            <el-row>
              <el-form-item v-if="searchNameP !== '' && entbase.length>1" label="列入异常主体信息" class="bt0">
                <el-button type="primary" @click="showEntbaseList">查看异常主体名单<i class="el-icon el-icon-s-order" style="margin-left:5px" /></el-button>
              </el-form-item>
            </el-row>
            <el-row>
              <el-form-item v-if="searchNameP !== '' && entbase.length === 1" label="主体名称" class="bt0">
                <span>{{ dataShow[0].entname }}</span>
              </el-form-item>
            </el-row>
            <el-row>
              <el-form-item label="列入原因" prop="reason" class="bt0">
                <el-col :span="24">
                  <el-input v-model="form.reason" type="textarea" style="display:inline-block;width:100%" disabled />
                </el-col>
              </el-form-item>
            </el-row>
            <el-row>
              <el-form-item label="决定书内容" prop="decisionContent" class="bt0">
                <el-col :span="24">
                  <el-input v-model="decisionContent" type="textarea" style="display:inline-block;width:100%" disabled :autosize="{ minRows: 2, maxRows: 6 }" />
                </el-col>
              </el-form-item>
            </el-row>
            <el-row>
              <el-form-item label="备注" prop="remark" class="bt0">
                <el-col :span="24">
                  <el-input v-model="remark" type="textarea" style="display:inline-block;width:100%" :autosize="{ minRows: 2, maxRows: 6 }" disabled />
                </el-col>
              </el-form-item>
            </el-row>
            <el-row class="p0">
              <el-form-item label="材料列表" class="bt0">
                <ul style="list-style:none;padding:0;margin:5px">
                  <li v-for="item in fileList" :key="item.id" style="line-height: 30px">
                    <el-link class="list-style" :href="item.url" target="_blank">{{ item.name }}</el-link>
                  </li>
                </ul>
              </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 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>
      </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>
    <el-dialog
      title="异常主体名单"
      :visible.sync="showEntbase"
      width="90%"
    >
      <el-table
        v-loading="loading"
        :data="bizList"
        tooltip-effect="dark"
        style="width: 100%; min-height: 380px"
        :fit="true"
        :header-cell-style="{background:'#8cc3fb',color:'#fff'}"
        :row-class-name="tableRowClassName"
        @selection-change="handleSelectionChange"
      >
        <!-- <el-table-column class="no" type="selection" min-width="20" align="center" /> -->
        <el-table-column prop="index" label="序号" min-width="50" align="center" />
        <el-table-column prop="uscc" label="统一社会信用代码/注册号" min-width="160" align="left" />
        <el-table-column prop="entname" :show-overflow-tooltip="true" label="主体名称" min-width="210" align="left" />
        <el-table-column prop="address" :show-overflow-tooltip="true" label="经营地址" min-width="180" align="left" />
        <el-table-column prop="estDate" :show-overflow-tooltip="true" label="成立日期" min-width="80" align="left" />
        <el-table-column prop="lerepname" label="法定代表人" min-width="80" align="left" />
        <el-table-column prop="entTypeName" :show-overflow-tooltip="true" label="企业类型" min-width="120" align="left" />
        <el-table-column prop="regstateCn" :show-overflow-tooltip="true" label="企业状态" min-width="100" align="left" />
      </el-table>
      <el-row class="bl0">
        <el-col>
          <el-pagination
            :current-page="pageParam.curPage"
            small
            layout="prev, pager, next"
            :total="pageParam.total"
            @size-change="pageSizeChange"
            @current-change="pageIndexChange"
          />
        </el-col>
      </el-row>
    </el-dialog>
    <el-dialog
      title="已剔除主体名单"
      :visible.sync="showRemEnt"
      width="90%"
    >
      <el-table
        v-loading="loading"
        :data="remList"
        tooltip-effect="dark"
        style="width: 100%; min-height: 380px"
        :fit="true"
        :header-cell-style="{background:'#8cc3fb',color:'#fff'}"
        :row-class-name="tableRowClassName"
      >
        <el-table-column prop="index" label="序号" min-width="50" align="center" />
        <el-table-column prop="uscc" label="统一社会信用代码/注册号" min-width="160" align="left" />
        <el-table-column prop="entname" :show-overflow-tooltip="true" label="主体名称" min-width="210" align="left" />
        <el-table-column prop="removeReason" :show-overflow-tooltip="true" label="剔除原因" min-width="180" align="left" />
        <el-table-column
          prop="removeTime"
          :show-overflow-tooltip="true"
          label="剔除时间"
          min-width="80"
          align="left"
        />
        <el-table-column prop="removeHandler" label="剔除处理人" min-width="80" align="left" />
      </el-table>
      <div>
        <el-row class="bl0">
          <el-col :span="12">
            <el-pagination
              :current-page="remPageParam.curPage"
              small
              layout="prev, pager, next"
              :total="remPageParam.total"
              @size-change="remPageSizeChange"
              @current-change="remPageIndexChange"
            />
          </el-col>
          <el-col :span="12">
            <div class="dialog-box">
              <el-button type="primary" size="mini" @click="showRemEntList">确定</el-button>
            </div>
          </el-col>
        </el-row>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import DetailHead from '@/components/DetailHead'
import HandleDialog from '@/components/HandleDialog'
import { removeAttachementById, listAttachmentByBizseq } from '@/api/attachment'
import { nextAbnormaInclusionProcess, getBizSpeListPage, searchBizSpeByBizSeqId, listAbnormalByBizSeqAndEntName } from '@/api/abnormal'
import { taskOpinion } from '@/api/task'
import { parseTime } from '@/utils'
import { getConstant } from '@/api/system'
import { isMenHu } from '@/utils/utils'

export default {
  components: {
    HandleDialog: HandleDialog,
    DetailHead
  },
  data() {
    return {
      remPageParam: {
        customParamMap: {
          bizseq: '',
          searchCode: '',
          entname: '',
          type: 'rem'
        },
        curPage: 1,
        total: 0,
        pageSize: 10
      },
      remList: [],
      showRemEnt: false,
      removeReason: '',
      searchNameP: '',
      decisionContent: '',
      submitLoading: false,
      remark: '',
      uploadPath: `${process.env.VUE_APP_BASE_API}/attachment/uploadAdvance`,
      showEntbase: false,
      batchId: this.$route.query.batch,
      entbase: [],
      // 数组内套数组，每个数组就是一页数据
      totalPageData: [],
      // 共几页
      pageNum: '',
      // 每页显示数量
      pageSize: 10,
      // 当前显示页码
      currentPage: 1,
      // 总共多少条数据
      total: 1,
      // 当前要显示的数据
      dataShow: [],
      form: {
        enterType: 'farmer',
        nextNodeID: 'endExptlist',
        spereasons: '',
        opinion: '',
        reason: ''
      },
      formRules: {
        opinion: [
          { required: true, message: '请输入审批意见', trigger: 'blur' }
        ]
      },
      reason: '',
      opinion: '',
      reasonOptions: [],
      opinionOptions: [],
      historyLog: [],
      exmainerOptions: [],
      fileList: [],
      user: JSON.parse(sessionStorage.getItem('user')),
      reviewer: [],
      pageParam: {
        customParamMap: {
          bizseq: '',
          searchCode: '',
          entname: '',
          type: 'biz'
        },
        curPage: 1,
        total: 0,
        pageSize: 10
      },
      bizList: [], // 异常主体名单分页列表
      loading: false
    }
  },
  watch: {
    // 原因渲染
    reason() {
      this.form.specause = this.reason
      let name = ''
      this.reasonOptions.forEach(each => {
        if (this.reason === each.value) {
          name = each.label
        }
      })
      this.form.reason = name
    },
    // 意见渲染
    opinion() {
      let name = ''
      this.opinionOptions.forEach(each => {
        if (this.opinion === each.value) {
          name = each.label
        }
      })
      this.form.opinion = name
    }
  },
  mounted() {
    this.bizseq = this.$route.query.bizseq
    this.workflowId = this.$route.query.workflowId
    this.tasklistid = this.$route.query.tasklistid
    this.searchNameP = this.$route.query.searchName
    if (this.searchNameP === undefined) {
      this.searchNameP = ''
    }
    // 加载常用语
    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
          }
          this.historyLog.push(each)
        })
      } else {
        this.$message.error('加载处办记录失败')
      }
    })
    // 加载农专社列入原因
    getConstant('CD77').then(res => {
      if (res.code === 0) {
        const list = []
        res.data.forEach(each => {
          list.push({
            value: each.code,
            label: each.name
          })
        })
        this.reasonOptions = list

        if (this.$util.isNotEmpty(this.reason)) {
          let name = ''
          this.reasonOptions.forEach(each => {
            if (this.reason === each.value) {
              name = each.label
            }
          })
          this.form.reason = name
        }
      }
    })
    // 加载异常名录主体
    this.loadBizSpeListPage()
    this.loadRemListPage()
    // if (this.searchNameP === '') {
    //   listAbnormalByBizSeq(this.bizseq).then(res => {
    //     if (res.code === 0) {
    //       // 添加分页index
    //       let i = 1
    //       const pageStart = (this.currentPage - 1) * this.pageSize
    //       res.data.forEach(each => {
    //         each.index = pageStart + i++
    //         each.name = each.entname
    //         each.uscc = each.regno
    //         each.typeName = each.entTypeName
    //         // this.$set(this, 'reason', each.specause)
    //         this.form.spereasons = each.spereasons
    //         this.entbase.push(each)
    //       })
    //       this.reason = this.entbase[0].specause
    //       this.remark = this.entbase[0].remark === 'null' ? '' : this.entbase[0].remark
    //       this.$nextTick(() => {
    //         this.decisionContent = this.entbase[0].decisionContent
    //       })
    //       // 分页处理
    //       this.total = this.entbase.length
    //       // 计算页数
    //       this.calcPageData()
    //     } else {
    //       this.$message.error('加载异常名录主体信息失败')
    //     }
    //   })
    // } else {
    //   listAbnormalByBizSeqAndEntName(this.bizseq, this.searchNameP).then(res => {
    //     if (res.code === 0) {
    //       // 添加分页index
    //       let i = 1
    //       const pageStart = (this.currentPage - 1) * this.pageSize
    //       res.data.forEach(each => {
    //         each.index = pageStart + i++
    //         each.name = each.entname
    //         each.uscc = each.regno
    //         each.typeName = each.entTypeName
    //         // this.$set(this, 'reason', each.specause)
    //         this.form.spereasons = each.spereasons
    //         this.entbase.push(each)
    //       })
    //       this.reason = this.entbase[0].specause
    //       this.remark = this.entbase[0].remark === 'null' ? '' : this.entbase[0].remark
    //       this.$nextTick(() => {
    //         this.decisionContent = this.entbase[0].decisionContent
    //       })
    //       // 分页处理
    //       this.total = this.entbase.length
    //       // 计算页数
    //       this.calcPageData()
    //     } else {
    //       this.$message.error('加载异常名录主体信息失败')
    //     }
    //   })
    // }

    // 加载附件清单
    listAttachmentByBizseq(this.bizseq).then(data => {
      const list = []
      data.forEach(each => {
        if (each.attachtype !== '0') {
          each.id = each.attachmentid
          each.name = each.filename
          each.url = `${process.env.VUE_APP_DOWNLOAD_URL}/` + each.temppath
          list.push(each)
        }
      })
      this.fileList = list
    })
  },
  methods: {
    loadBizSpeListPage() {
      if (this.loading) return
      this.loading = true
      this.pageParam.customParamMap.bizseq = this.bizseq
      getBizSpeListPage(this.pageParam).then((res) => {
        const list = []
        let i = 1
        const pageStart =
          (this.pageParam.curPage - 1) * this.pageParam.pageSize
        res.data.records.forEach((each) => {
          each.index = pageStart + i++
          each.uscc = each.uniscid ? each.uniscid : each.regno
          list.push(each)
        })
        this.bizList = list
        this.loading = false
        this.pageParam.total = res.data.total
        this.reason = this.bizList[0].specause
        this.remark = this.bizList[0].remark === 'null' ? '' : this.bizList[0].remark
        if (this.bizList[0].decisionContent !== null) {
          this.$nextTick(() => {
            this.decisionContent = this.bizList[0].decisionContent
          })
        }
        const selection = JSON.parse(JSON.stringify(this.multipleSelection))
        this.$nextTick(() => {
          for (const i in list) {
            const each = list[i]
            for (const j in selection) {
              if (each.pripid === selection[j].pripid) {
                this.$refs.bizList.toggleRowSelection(each, true)
              }
            }
          }
        })
      })
    },
    // 加载已剔除名单
    loadRemListPage() {
      this.loading = true
      this.remPageParam.customParamMap.bizseq = this.bizseq
      getBizSpeListPage(this.remPageParam).then((res) => {
        const list = []
        let i = 1
        const pageStart =
          (this.remPageParam.curPage - 1) * this.remPageParam.pageSize
        res.data.records.forEach((each) => {
          each.index = pageStart + i++
          each.uscc = each.uniscid ? each.uniscid : each.regno
          list.push(each)
        })
        this.remList = list
        this.loading = false
        this.remPageParam.total = res.data.total
      })
    },
    // 剔除名单列表分页大小改变
    remPageSizeChange(pageSize) {
      this.remPageParam.pageSize = pageSize
      this.loadRemListPage()
    },
    // 剔除名单列表当前分页改变
    remPageIndexChange(curPage) {
      this.remPageParam.curPage = curPage
      this.loadRemListPage()
    },
    parseTime(...args) {
      return parseTime(...args)
    },
    // 分页大小改变
    pageSizeChange(pageSize) {
      this.pageParam.pageSize = pageSize
      this.loadBizSpeListPage()
    },
    // 当前分页改变
    pageIndexChange(curPage) {
      this.pageParam.curPage = curPage
      this.loadBizSpeListPage()
    },
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2 === 1) {
        return 'warning-row'
      }
    },
    // 主体名单导出Excel
    exportEntListExcel() {
      this.$confirm('是否确认导出', '提示').then(() => {
        const url = location.origin + process.env.VUE_APP_BASE_API + '/bizSpeList/exportEntList?bizseq=' + this.bizseq
        window.open(url)
      })
    },
    // 上传文件预览
    previewFile(file) {
      window.open(file.url, '_blank')
    },
    // 发送提交
    submitForm() {
      this.$refs.form.validate(valid => {
        if (!valid) return
        this.$confirm('是否确认提交？', '提示').then(() => {
          this.submitLoading = true
          this.form.bizseq = this.bizseq
          this.form.workflowid = this.workflowId
          this.form.opiniontype = '2'
          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.nextPerformerNames = this.user.username
          nextAbnormaInclusionProcess(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(() => {})
      })
    },
    // 查看异常主体名单
    showEntbaseList() {
      this.showEntbase = true
    },
    // 查看已剔除主体名单
    showRemEntList() {
      this.showRemEnt = !this.showRemEnt
    },
    // 移除文件回调
    removeFile(file) {
      removeAttachementById(file.id).then(res => {
        if (res.code === 0) {
          this.$message.success('删除成功')
        } else {
          this.$message.error(res.msg)
        }
      })
    }
  }
}
</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;
    .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{
  .el-form-item{
    margin-bottom:0;
    background: $color-form-label;
    .el-form-item__label{
      padding-left: 10px;
      border-top: 1px solid $color-border;
      font-size: $table-content-font-size;
    }
    .el-form-item__content{
      background: white;
      padding: 5px;
      font-size: $table-content-font-size;
      border-top: 1px solid $color-border;
      border-right: 1px solid $color-border;
      border-left: 1px solid $color-border;
    }
  }
  .el-form-item:last-child{
    border-bottom: 1px solid $color-border;
  }
  .form-body.history{
    padding:15px;
    border:1px solid $color-border;
  }
  .el-form-item.is-error .custom .el-input__inner{
    border-color:$color-border;
  }
  .el-textarea.is-disabled .el-textarea__inner{
    color: #1c1e23;
  }
  .el-step__description{
    font-size: 18px;
  }
}
</style>
