<template>
  <!-- 在线接收 -->
  <div>
    <el-card style="margin-bottom: 5px">
      <el-row>
        <el-col :span="10">
          <span>接收时间 : </span>
          <el-date-picker
            v-model="search.time"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          />
        </el-col>
        <el-col :span="6">
          <span>接收状态 : </span>
          <el-radio-group v-model="search.status">
            <el-radio :label="2">已接收</el-radio>
            <el-radio :label="1">未接收</el-radio>
          </el-radio-group>
        </el-col>
        <el-col :span="6">
          <el-button size="small" type="primary" icon="el-icon-search" @click="searchClick(1)">搜索</el-button>
          <el-button size="small" type="info" icon="el-icon-refresh" @click="searchClick(2)">重置</el-button>
        </el-col>
      </el-row>
    </el-card>
    <el-card :body-style="{ padding: '5px 10px' }">
      <t-table :table-data="tableData" :table-loading="tableLoading" @clickBtn="handleClickBtn(arguments)" />
      <el-pagination
        layout="total, prev, pager, next, sizes"
        :page-size="pagination.pageSize"
        :page-sizes="[10, 50, 100, 500,1000]"
        :current-page="pagination.pageNum"
        :total="pagination.total"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
      />
    </el-card>
    <!-- 离线接收 -->
    <off-receive ref="offReceiveref" :visible="isShowOffReceive" :formParams="formParams" @clickSubmit="handleReceive(arguments)" @clickServe="handleServeReceive" @clickCancel="cancel"></off-receive>
    
    <!-- 接收 -->
    <dia-receive :loading="startLoading" :detectionresultFlag="detectionresultFlag" :tableObj="tableObj" :tableData="tableResultData" :visible="diaReceive" 
    @previewReport="handItemCheck2" @fourCheck="fourCheckResult(0)" @startReceive="handleStartReceive" @handsTest="handleShowtest" @cancel="cancelReceive"></dia-receive> 
    <!-- 接收-四性检测-人工检测 -->
    <dia-handtest  :visible="diaHandtest" :formParams="formHands" @clickSubmit="handleClickHandsTest" @clickCancel="cancelHandTest"></dia-handtest>
    
    <!-- 接收详情 -->
    <receive-detail ref="receiveDetailRef" :visible="isShowReceiveDetail" :tableObj="tableObj" @clickCancel="cancel"></receive-detail>
    <!-- 四性检测报告、登记表 -->
    <dia-preview :visible="diaPreview" :pdf-url="pdfUrl" @cancel="cancelPreview" />
  </div>
</template>
<script>
import diaPreview from "@/views/archives-management/fileManagement/notStoredArchives/components/diaPreview.vue";
import diaHandtest from "@/views/archives-management/fileManagement/notStoredArchives/components/diaHandtest.vue";
import receiveDetail from "./components/detail.vue";
import diaReceive from "./components/diaReceive.vue";
import offReceive from "./components/offReceive.vue";
import tSearch from './components/search.vue';
import tTable from './components/table.vue';
import localStronge from "@/api/localData";
import { clearObjValue,message} from '@/utils/common'
import {selectById,serverReception,selectReceiveInformation,handItemCheck,receivingRegistration,startReceiving,offlineReceive,handCheckResult } from '@/api/archives-management/ungroupedManagement/receive.js'
export default {
  components: {
    tSearch,
    tTable,
    offReceive,
    receiveDetail,
    diaPreview,
    diaHandtest,
    diaReceive
  },
  data() {
    return {
      prefix: process.env.NODE_ENV === 'development' ?
          ("http://" + localStronge.getServiceip().ip + ":" + localStronge.getServiceip().host) : 'http://' + window
          .location.host,
      detectionresultFlag:null, // 检测结果状态
      startLoading:false,
      formHands:{
        status:1,
        desc:''
      },
      isShowReceiveDetail:false,
      formParams:{
        type:1,
        file:'',
        classId:'',
        handedOver:'',
        receivingUnit:'',
        contentDescription:'',
        path:''
      },
      pdfUrl:'',
      diaReport: false, // 报表表单
      diaPreview: false, // 预览表单
      diaReceive:false, // 接收modal
      diaHandtest:false, // 人工检测
      isShowOffReceive:false,
      tableLoading: false,
      tableResultData:[],
      tableData: [],
      pagination: {
        pageNum: 1,
        pageSize: 10,
        total: 0
      },
      search: {
        time: '',
        status:''
      },
      tableObj:{}, // table对象
    }
  },
  mounted() {
    this.getData()
  },
  methods: {
    // 接收-取消
    cancelReceive(){
      this.diaReceive = false;
      this.tableResultData = [];
    },
    // 点击人工检测按钮
    handleShowtest(){
      this.diaHandtest = true;
    },
    // 人工检测-确定
    handleClickHandsTest(row){
      let params = {
        id:this.tableObj.id,
        detection:row.status
      }
      handItemCheck(params).then((res)=>{
        if(res.code == 100){
          // let fileName = res.body.fileName;
          // this.diaPreview = true;
          // this.showPdf(fileName);
          this.diaHandtest = false;
          // 刷新数据
          this.fourCheckResult(row.status);
          // 检验状态
          this.selectById();
          
        }else{
          this.diaHandtest = false;
          message('warning',res.msg,3000)
        }
      }).catch((error)=>{
        this.diaHandtest = false;
      })
    },
    // 人工检测-取消
    cancelHandTest(){
      clearObjValue(this.formParams);
      this.formParams = {
        type:1,
        file:'',
        classId:'',
        handedOver:'',
        receivingUnit:'',
        contentDescription:''
      }
      this.diaHandtest = false;
      this.formHands = {
        status:1,
        desc:''
      }
    },
    // 开始移交
    handleStartReceive(){
      this.startLoading = true;
      let params = {
        id:this.tableObj.id
      }
      startReceiving(params).then((res)=>{
        if(res.code == 100){
          this.startLoading = false;
          message('success',res.msg,3000);
          // 刷新数据
          this.searchClick(1);
        }else{
          this.startLoading = false;
          message('warning',res.msg,3000);
        }
        this.cancelReceive();
      }).catch((error)=>{
        this.startLoading = false;
        this.cancelReceive();
      })
    },
    // 四性检测结果数据
    fourCheckResult(detection){ 
      let params = {
        id:this.tableObj.id,
        detection:detection, // 默认初始化是0
      }
      handCheckResult(params).then((res)=>{
        if(res.code == 100){
          let resultData = res.body.list;
          this.tableResultData = resultData;
        }
      }).then(()=>{
        // 刷新数据
        this.searchClick(1);
        // 查找当前rowid
        this.selectById();
      }).catch((error)=>{
      })
    },
    // 查询批次单条数据
    selectById(){
      selectById({id:this.tableObj.id}).then((res)=>{
        if(res.code == 100){
          let resultObj = res.body.data;
          // 2表示通过，其他表示未通过
          this.detectionresultFlag = resultObj.detectionresult;
        }
      }).catch((error)=>{
      })
    },
    // 四性检测报告关闭
    cancelPreview() {
      this.diaPreview = false;
      this.diaHandtest = false;
    },
    // 点击table详情-接收，接收详情，
    handleClickBtn(params){
      let _type = params[0];
      this.tableObj = params[1];
      this.detectionresultFlag = this.tableObj.detectionresult;
      if(_type == 'receive'){
        this.diaReceive = true;
      }else if(_type == 'detail'){
        this.isShowReceiveDetail = true;
        this.$refs.receiveDetailRef.handleNodeClick(this.tableObj);
      }else if(_type == 'fourResult'){
        // 四性检测报告展示
        this.diaPreview = true;
        this.handItemCheck();
      }else if(_type == 'report'){
        // 登记表展示
        this.receivingRegistration();
      }
    },
    handItemCheck2(){
      let params = {
        id:this.tableObj.id,
        detection:this.detectionresultFlag==2?1:0,
      }
      handItemCheck(params).then((res)=>{
        if(res.code == 100){
          let fileName = res.body.fileName;
          this.diaPreview = true;
          this.showPdf(fileName);
        }else{
          message('warning',res.msg,3000);
        }
      })
    },
    // 四性检测结果--
    handItemCheck(){
      // 地址
      const prefix =
        process.env.NODE_ENV === "development"
          ? "http://" +
            localStronge.getServiceip().ip +
            ":" +
            localStronge.getServiceip().host
          : "http://" + window.location.host;
      this.pdfUrl =
        prefix + "/point-strategy/v/archiveFile/showInventory?filePath=" + this.tableObj.resultpath;
    },
    // 登记表结果展示
    receivingRegistration(){
      let params = {
        id:this.tableObj.id,
      }
      receivingRegistration(params).then((res)=>{
        if(res.code == 100){
          let fileName = res.body.data;
          this.diaPreview = true;
          this.showPdf(fileName)
        }else{
          message('warning',error,3000);
        }
      }).catch((error)=>{
      })
    },
    // pdf预览
    showPdf(fileName) {
      const prefix =
        process.env.NODE_ENV === "development"
          ? "http://" +
            localStronge.getServiceip().ip +
            ":" +
            localStronge.getServiceip().host
          : "http://" + window.location.host;
      this.pdfUrl =
        prefix + "/point-strategy/v/ureport/showPdf?fileName=" + fileName;
    },
    searchClick(val) {
      switch (val) {
        case 1:
          this.pagination.pageNum = 1;
          this.getData();
          break
        case 2:
          clearObjValue(this.search)
          break
      }
    },
    getData() {
      this.tableLoading = true
      this.params = {
        // 其他参数
        startTime:this.search.time ? this.search.time[0]:'',
        endTime:this.search.time ? this.search.time[1]:'',
        status:this.search.status,
        receivingtype:1, // 在线接收 类型
        page: this.pagination.pageNum,
        limit: this.pagination.pageSize,
      }
      selectReceiveInformation(this.params).then((res)=>{
        if(res.code == 100){
          this.tableLoading = false;
          let resultData = res.body.list;
          this.tableData = resultData;
          this.pagination.total = res.body.total;
        }else{
          this.tableLoading = false;
          message('warning',res.msg,3000);
        }
      }).catch((error)=>{
        this.tableLoading = false;
      })
    },
    handleCurrentChange(val) {
      this.pagination.pageNum = val
      this.getData()
    },
    handleSizeChange(val) {
      this.pagination.pageSize = val
      this.getData()
    },
    // 离线接收
    offReceive(){
      this.isShowOffReceive = true;
    },
    // 离线接收-确定
    handleReceive(params){
      let action = params[0]
      let query = params[1]
      let formData = params[2];
      console.log(params)
      this.$axios.post(action + query, formData, {
            headers: {
              'Content-Type': 'multipart/form-data',
              "Token": sessionStorage.getItem('token')
            }
      }).then((res)=>{
        if(res.data.code == 100){
          // 刷新数据
          this.searchClick(1);
          message('success',res.data.msg,3000);
        }else{
          message('warning',res.data.msg,3000);
        }
        
        clearObjValue(this.formParams);
        this.formParams.type = 1;
        this.$refs.offReceiveref.fileList = [];
        this.isShowOffReceive = false;
      }).catch((error)=>{
        this.formParams.type = 1;
        this.$refs.offReceiveref.fileList = [];
        this.isShowOffReceive = false;
      })
    },
    // 服务器接收-确定
    handleServeReceive(params){
      serverReception(params).then((res)=>{
        if(res.code == 100){
          // 刷新数据
          this.searchClick(1);
          message('success',res.msg,3000);
         
        }else{
          message('warning',res.msg,3000);
        }
        clearObjValue(this.formParams);
        this.formParams.type = 1;
        this.isShowOffReceive = false;
        
      }).catch((error)=>{
        clearObjValue(this.formParams);
        this.formParams.type = 1;
        this.isShowOffReceive = false;
      })
    },
    // 离线接收-取消按钮
    cancel(){
      this.isShowOffReceive = false;
      this.isShowReceiveDetail = false;
      clearObjValue(this.formParams);
      this.formParams.type = 1;
      this.$refs.offReceiveref.fileList = [];
    }
  }
}
</script>
