<!--字段，查询列表，点击事件全部没有做-->
<template>
  <div class="examination">
    <div class="common-head-search">
      <el-form :inline="true">
        <el-form-item label="样品类型">
          <el-select v-model="search.sampleTypeCode" clearable filterable collapse-tags size="small" style="width:200px" placeholder="请选择查询内容">
            <el-option v-for="item in sampleType" :key="item.baseCode" :label="item.baseName"
              :value="item.baseCode">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="物料名称">
          <el-input v-model="search.matName" size="small" placeholder="请输入查询内容" style="width: 200px"></el-input>
        </el-form-item>
        <el-form-item label="检验日期">
          <el-date-picker
           v-model="search.declareTime"
           type="daterange"
           range-separator="至"
           start-placeholder="开始日期"
           end-placeholder="结束日期"
           size="small"
           format="yyyy-MM-dd"
           value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" type="goon" size="mini" @click="searchData">查询</el-button>
          <el-button icon="el-icon-refresh" type="info" plain size="mini" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="common-title-div">
      <div class="common-title-name">
        <img style="width: 25px; height: 25px" src='../../../../assets/img/imgScreen/logo.png' />
        报告信息表
      </div>
      <div>
      <el-button icon="el-icon-check" type="goon" size="mini" :loading='creteLoading' @click="createData">生成</el-button>
      <el-button icon="el-icon-upload2" type="goon" size="mini" @click="printData">打印</el-button>
      </div>
    </div>
    <div class="common-table-div" style="margin-bottom:0;margin-top:0">
       <el-table v-loading="dataLoading" ref="dataTable" @selection-change="handleSelectionChange" border :height="theight1" highlight-current-row @row-click="clickRow" :data="table.list" :icore-filter-flag="icoreFilterFlag"
       :header-cell-style="tableHeaderCellStyle" :cell-class-name="tableRowClassName" :summary-method="getSummaries" show-summary
       :exchangeFilterMap="{'valueFlag':{'0':'无效','1':'有效'}}">
        <el-table-column type="index" label="NO" align="center"></el-table-column>
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="BATCH_NO" label="批次号" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="80px" prop="CAR_NO" label="车号" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="120px" prop="MAT_NAME" label="物料" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="120px" prop="MAT_ORIGIN" label="产地" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="120px" prop="cretTypeName" label="证书类型"  align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="120px" prop="SAVE_PATH" label="证书PDF"  align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="80px" prop="PRT_NUM" label="打印次数"  align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="80px"  prop="validFlag" label="状态" align="center">
           <template slot-scope="scope">
             <span v-if="scope.row.validFlag == 0">无效</span>
             <span v-else-if="scope.row.validFlag == 1">有效</span>
           </template>
        </el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="80px" prop="memo" label="备注"  align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="80px" prop="createMan" label="创建人" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="140px" prop="createTime" label="创建时间" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="80px" prop="updateMan" label="修改人" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="140px" prop="updateTime" label="修改时间" align="center"></el-table-column>
      </el-table>
    </div>
    <div class="common-foot-style">
        <el-pagination v-loading="dataLoading" @size-change="pageSizeChange" @current-change="pageCurrentChange"
          :current-page="page.pageNum" :page-sizes="[10, 20, 50, 100, 200]" :page-size="page.pageSize" layout="total, sizes, prev, pager, next, jumper"
          :total="page.totalPage" background>
        </el-pagination>
    </div>
    <div class="common-title-div">
      <div class="common-title-name">
        <img style="width: 25px; height: 25px" src='../../../../assets/img/imgScreen/logo.png' />
        检验明细表
      </div>
    </div>
    <div class="common-table-div" style="margin-bottom:0;margin-top:0">
       <el-table v-loading="dataLoading2" ref="dataTable2" border :height="theight2" highlight-current-row :data="table.list2" :icore-filter-flag="icoreFilterFlag"
       :header-cell-style="tableHeaderCellStyle"  :summary-method="getSummaries" show-summary>
        <el-table-column type="index" label="NO" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="120px" prop="testItemName" label="任务名称" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="120px" prop="anlyItemName" label="分析项目" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="anlyValue" label="检验值" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="80px" prop="unitName" label="单位" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="80px" prop="dspOrder" label="排序" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="memo" label="备注" align="center"></el-table-column>
      </el-table>
    </div>
    <!-- <div class="common-foot-style">
        <el-pagination v-loading="dataLoading2" @size-change="pageSizeChange2" @current-change="pageCurrentChange2"
          :current-page="page2.pageNum" :page-sizes="[10, 20, 50, 100, 200]" :page-size="page2.pageSize" layout="total, sizes, prev, pager, next, jumper"
          :total="page2.totalPage" background>
        </el-pagination>
    </div> -->
  </div>
</template>
<script>
import {
  getCookie
} from '@/utils/util.js';
import {
  cookieUserId,
  cookieUserName
} from '@/config/config.js';
let userName = getCookie(cookieUserName);
let userId = getCookie(cookieUserId);
export default {
  data () {
    return {
      table: {
        list: [],
        list2: [],
        list3: []
      },
      dataLoading: false,
      dataLoading2:false,
      creteLoading:false,
      icoreFilterFlag: true,
      firstRow: '',
      page: {
        pageSize: 50,
        pageNum: 1,
        totalPage: 0
      },
      page2: {
        pageSize: 990,
        pageNum: 1,
        totalPage: 0
      },
      sampleType: [],
      saveData:{},
      multipleSelection3: [],
      multipleSelection4: [],
      search: {
        matName: '',
        sampleTypeCode: '',
        declareTime: ''
      },
      theight1:0,
      theight2:0,
      clickTableData:[]
    }
  },
  created(){
    this.theight1 = window.innerHeight / 2 - 130 < 249 ? 249 : window.innerHeight / 2 - 100;
    this.theight2 = window.innerHeight / 2 - 130 < 249 ? 249 : window.innerHeight / 2 - 100;
  },
  mounted () {
    this.axios.post("pass/baseManagement/v1/limsbaseinfos/queryBaseInfoByBaseCode",{validFlag:1,baseCode:4801}, {individualType: 'json'}).then(res => {
      this.sampleType = res.data;
    })
    this.searchData();
  },
  methods: {
    getSummaries (param) {
      const { columns, data } = param;
      const sums = [];
       columns.forEach((column, index) => {
          if (index === 0) {
            sums[index] = '合计';
            return;
          }
          if (index === 1) {
            sums[index] = data.length;
          } 
       });
        return sums;
    },
    handleSelectionChange(val){
      this.clickTableData = val
    },
    //生成点击事件
    createData () {
      console.log('fjkdfd')
      // if (JSON.stringify(this.saveData)==="{}") {
      //   this.$message.warning("请先点击选中一条报告信息表数据");
      //   return;
      // }
      if (this.clickTableData.length <1) {
        this.$message.warning("请先勾选中要生成的报告信息表数据");
        return
      }
      this.creteLoading = true
      this.axios(
        {
          method: 'post',
          url: 'pass/baseManagement/v1/limscretprods/generate',
          data: this.clickTableData,
          individualType: 'json'
        }
        ).then(res => {
            // responseType: 'arraybuffer',
            // let fileName = getCookie('disposition').split('=')[1];
            // console.log(getCookie('disposition'), 'disposition');
            // let blob = new Blob([res]);
            // // let fileName = fileRow.fileName;
            // let elink = document.createElement('a');
            // elink.download = fileName;
            // elink.style.display = 'none';
            // elink.target = "_blank";
            // elink.href = URL.createObjectURL(blob);
            // document.body.appendChild(elink);
            // elink.click();
            // URL.revokeObjectURL(elink.href); // 释放URL对象
            // document.body.removeChild(elink);
            let fileRow = {}
            fileRow = res.data
            this.axios.get('pass/baseManagement/v1/file/download/'+ fileRow.fileName ,{responseType: 'blob'},{headers: {'content-type': 'application/x-www-form-urlencoded'}}).then(res => {
            let blob = new Blob([res]);
            let fileName = fileRow.fileName;
            let elink = document.createElement('a');
            elink.download = fileName;
            elink.style.display = 'none';
            elink.target = "_blank";
            elink.href = URL.createObjectURL(blob);
            document.body.appendChild(elink);
            elink.click();
            URL.revokeObjectURL(elink.href); // 释放URL对象
            document.body.removeChild(elink);
        })  
        this.creteLoading = false
      }).catch(e => {
        this.creteLoading = false;
        this.$message.error(e.message)
      });
    },
    printData () {},//打印点击事件
    searchData () {
      this.page.pageNum = 1;
      this.page.pageSize = 50;
      this.getDataList()
    },
    getDataList () {
      let starTime = ''
      let endTime = ''
      this.saveData = {}
      this.table.list2 = []
      if (!this.search.declareTime) {
        starTime = ''
        endTime = ''
      }else{
        starTime = this.search.declareTime[0] + ' 00:00:00'
        endTime = this.search.declareTime[1] + ' 23:59:59'
      }
      let obj = {
        'object':{
          sampleTypeCode:this.search.sampleTypeCode,
          matName:this.search.matName,
          cretTypeCode:482002
        },
        'startTime':starTime,
        'endTime':endTime,
        'pageIndex':this.page.pageNum,
        'pageSize':this.page.pageSize
      }
      this.dataLoading = true;
      this.axios.post('pass/baseManagement/v1/limscretprods/querylimcretprod',obj,{individualType: 'json'}).then(res => {
        if (res.code == 0 || res.code == 200) {
          this.table.list = res.data.list;
          this.page.totalPage = res.data.total;
          if (this.table.list.length > 0) {
            this.$refs.dataTable.setCurrentRow(this.table.list[0])
            this.clickRow(this.table.list[0])
          }
        }else{
          this.$message.error(res.message)
        }
        this.dataLoading = false;
      }).catch(e => {
        this.dataLoading = false;
        this.$message.error(e.message)
      });
    },
    //点击选中报告信息表数据
    clickRow (val) {
      this.saveData = val
      this.page2.pageNum = 1
      this.page2.pageSize = 50
      this.getDataList2 (this.saveData.id)
    },
    getDataList2 (objId) {
      let id = objId ? objId : ''
      let obj = {
        'object':{
          ctId:id
        },
        'pageIndex':this.page2.pageNum,
        'pageSize':this.page2.pageSize
      }
      this.dataLoading2 = true;
       this.axios.post('pass/baseManagement/v1/limscretprodds/querylimsCprodd',obj,{individualType: 'json'}).then(res => {
        if (res.code == 0 || res.code == 200) {
          this.table.list2 = res.data.list;
          this.page2.totalPage = res.data.total;
        }else{
          this.$message.error(res.message)
        }
        this.dataLoading2 = false;
      }).catch(e => {
        this.dataLoading2 = false;
        this.$message.error(e.message)
      });
    },
    reset () {
      this.search.matName = '';
      this.search.sampleTypeCode = '';
      this.search.declareTime = '';
      this.searchData ();
    },
    pageSizeChange (size) {
      this.page.pageSize = size;
      this.getDataList();
    },
    pageCurrentChange (index) {
      this.page.pageNum = index;
      this.getDataList();
    },
    pageSizeChange2 (size) {
      this.page2.pageSize = size;
      this.getDataList2 (this.saveData.objId);
    },
    pageCurrentChange2 (index) {
      this.page2.pageNum = index;
      this.getDataList2 (this.saveData.objId);
    },
    addScreen () {
      this.$nextTick(() => {
          let ss = document.createElement('div');
          ss.innerHTML = '筛选';
          ss.style = 'text-align: center;'
          this.$refs.dataTable.$el.getElementsByClassName('el-table__fixed')[0].getElementsByTagName('td')[0].appendChild(ss);
       });
    },
    addScreen2 () {
     this.$nextTick(() => {
          let ss = document.createElement('div');
          ss.innerHTML = '筛选';
          ss.style = 'text-align: center;'
          this.$refs.dataTable2.$el.getElementsByClassName('el-table__fixed')[0].getElementsByTagName('td')[0].appendChild(ss);
       });
    },
    tableRowClassName ({ row, column, rowIndex, columnIndex}) {
      if (columnIndex === 8) {
      if (row.validFlag !== '1') {
        return 'error-row';
      }
      return '';
      }
    },
    tableHeaderCellStyle () {
      return 'color: rgb(255,255,255);border-bottom: 1px solid #DCDFE7;'
    }
  }
}
</script>
<style  scoped>
</style>
