<template>
  <div class="examination">
    <div class="common-head-search">
      <el-form :inline="true">
        <el-form-item label="供应商">
          <el-input v-model="search.suppName" @keyup.enter.native="searchData()" size="small" placeholder="请输入查询内容" style="width: 150px;"></el-input>
        </el-form-item>
        <el-form-item label="合同号">
          <el-input v-model="search.contractNo" @keyup.enter.native="searchData()" size="small" placeholder="请输入查询内容" style="width: 150px;"></el-input>
        </el-form-item>
        <!-- <el-form-item label='计划单位'>
          <el-select v-model="search.companyNo" filterable clearable style="width:100%" collapse-tags size="small">
            <el-option v-for="item in companyNameType" :key="item.orgCode" :label="item.orgName" :value="item.orgCode">
            </el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item>
          <el-button icon="el-icon-search" type="goon" size="mini" :loading='dataLoading' @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-circle-plus-outline" type="goon" size="mini" v-privilege="activeMenu + 'ADD'" @click="addData">新增</el-button>
      <el-button icon="el-icon-edit" type="goon" size="mini" v-privilege="activeMenu + 'PUT'" @click="modifyData">修改</el-button>
      <el-button icon="el-icon-remove-outline" type="danger" size="mini" v-privilege="activeMenu + 'DELETE'" @click="deleteData">删除</el-button>
      </div>
    </div>
    <div class="common-table-div" id="topTable" style="margin-bottom:0;margin-top:0">
       <el-table id="excelTable" v-loading="dataLoading" ref="dataTable2" border :height="height_top" highlight-current-row :data="table.list" :icore-filter-flag="icoreFilterFlag"
       :header-cell-style="tableHeaderCellStyle"  @cell-click='searchFollow' :summary-method="getSummaries" show-summary>
        <el-table-column type="index" label="NO" align="center"></el-table-column>
        <el-table-column :show-overflow-tooltip="true" sortable min-width="100px" prop="id" label="编号" align="center"></el-table-column>
        <el-table-column :show-overflow-tooltip="true" sortable min-width="80px" prop="contractNo" label="合同号" align="center"></el-table-column>
        <el-table-column :show-overflow-tooltip="true" sortable min-width="140px" prop="suppName" label="供应商" align="center"></el-table-column>
        <el-table-column :show-overflow-tooltip="true" sortable min-width="140px" prop="anlyItemName" label="分析项目" align="center"></el-table-column>
        <el-table-column :show-overflow-tooltip="true" sortable min-width="80px" prop="anlyValue" label="固定值" align="center"></el-table-column>
        <el-table-column :show-overflow-tooltip="true" sortable min-width="80px" prop="unitName" label="单位" align="center"></el-table-column>
        <el-table-column :show-overflow-tooltip="true" sortable min-width="80px" prop="dataTypeName" label="数据类型" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" label="下载、预览" align="center">
          <template slot-scope="scope">
            <el-tooltip content="下载文件" placement="bottom" effect="light">
            <el-button
            :disabled='!scope.row.cretImg1'
             type="goon" size="mini" icon="el-icon-download" circle
             @click="downloadFile(scope.$index, scope.row.id, scope.row.cretImg.fileName,  scope.row.cretImg.oldFileName, 'LIMS_CRET_TMP_M')">
           </el-button>
            </el-tooltip>
           <el-tooltip content="预览照片" placement="bottom" effect="light">
            <el-button
            :disabled='!scope.row.cretImg1'
             type="warning" size="mini"  icon="el-icon-view" circle
             @click="imgPreView(scope.$index, scope.row.id, scope.row.cretImg.fileName, 'LIMS_CRET_TMP_M')">
           </el-button>
           </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" sortable min-width="140px" prop="companyName" label="检验单位" align="center"></el-table-column>
        <el-table-column :show-overflow-tooltip="true" sortable min-width="140px" prop="deptName" label="检验车间" align="center"></el-table-column>
        <el-table-column :show-overflow-tooltip="true" sortable min-width="140px" prop="sectionName" label="检验班组" align="center"></el-table-column>
        <el-table-column :show-overflow-tooltip="true" sortable min-width="140px" prop="smpCompanyName" label="采样单位" align="center"></el-table-column>
        <el-table-column :show-overflow-tooltip="true" sortable min-width="140px" prop="smpDeptName" label="采样车间" align="center"></el-table-column>
        <el-table-column :show-overflow-tooltip="true" sortable min-width="140px" prop="smpSectionName" label="采样班组" align="center"></el-table-column>
        <el-table-column :show-overflow-tooltip="true" sortable min-width="120px" prop="memo" label="备注" align="center"></el-table-column>
        <el-table-column :show-overflow-tooltip="true" sortable min-width="95px" prop="createMan" label="创建人" align="center"></el-table-column>
        <el-table-column :show-overflow-tooltip="true" sortable min-width="160px" prop="createTime" label="创建时间" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="95px"  prop="updateMan" label="修改人" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="160px" prop="updateTime" label="修改时间" align="center"></el-table-column>
      </el-table>
    </div>
    <div class="common-foot-style">
        <el-pagination  @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> -->
    <el-row>
      <el-col :span='12'>
        <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-circle-plus-outline" type="goon" size="mini" v-privilege="activeMenu + 'ADD'" @click="addData">新增</el-button>
          <el-button icon="el-icon-edit" type="goon" size="mini" v-privilege="activeMenu + 'PUT'" @click="modifyData">修改</el-button>
          <el-button icon="el-icon-remove-outline" type="danger" size="mini" v-privilege="activeMenu + 'DELETE'" @click="deleteData">删除</el-button>
          </div>
        </div>
        <div class="common-table-div" id="topTable" style="margin-bottom:0;margin-top:0">
           <el-table id="excelTable" v-loading="dataLoading" ref="dataTable2" border :height="height_top" highlight-current-row :data="table.list" :icore-filter-flag="icoreFilterFlag"
           :header-cell-style="tableHeaderCellStyle"  @row-click='searchFollow' :summary-method="getSummaries" show-summary>
            <el-table-column type="index" label="NO" align="center"></el-table-column>
            <el-table-column :show-overflow-tooltip="true" sortable min-width="100px" prop="id" label="编号" align="center"></el-table-column>
            <el-table-column :show-overflow-tooltip="true" sortable min-width="128px" prop="contractNo" label="合同号" align="center"></el-table-column>
            <el-table-column :show-overflow-tooltip="true" sortable min-width="140px" prop="suppName" label="供应商" align="center"></el-table-column>
            <el-table-column :show-overflow-tooltip="true" sortable min-width="80px" prop="matShapeName" label="试样形状" align="center"></el-table-column>
            <el-table-column :show-overflow-tooltip="true" sortable min-width="140px" prop="testItemName" label="测试项目" align="center"></el-table-column>
            <el-table-column :show-overflow-tooltip="true" sortable min-width="100px" prop="lotsNo" label="到货批次号" align="center"></el-table-column>
            <el-table-column :show-overflow-tooltip="true" sortable min-width="120px" prop="memo" label="备注" align="center"></el-table-column>
            <el-table-column :show-overflow-tooltip="true" sortable min-width="95px" prop="createMan" label="创建人" align="center"></el-table-column>
            <el-table-column :show-overflow-tooltip="true" sortable min-width="130px" prop="createTime" label="创建时间" align="center"></el-table-column>
            <el-table-column sortable :show-overflow-tooltip="true" min-width="95px"  prop="updateMan" label="修改人" align="center"></el-table-column>
            <el-table-column sortable :show-overflow-tooltip="true" min-width="130px" prop="updateTime" label="修改时间" align="center"></el-table-column>
          </el-table>
        </div>
        <div class="common-foot-style">
            <el-pagination  @size-change="pageSizeChange" @current-change="pageCurrentChange"
              :current-page="page.pageNum" :page-sizes="[100, 200, 500, 800, 1000]" :page-size="page.pageSize" layout="total, sizes, prev, pager, next, jumper"
              :total="page.totalPage" background>
            </el-pagination>
        </div>
      </el-col>
      <el-col :span='12'>
        <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-circle-plus-outline" type="goon" :loading='addLoading2' size="mini" v-privilege="activeMenu + 'ADD'" @click="addData2">新增</el-button>
          <el-button icon="el-icon-edit" type="goon" size="mini" v-privilege="activeMenu + 'PUT'" @click="modifyData2">修改</el-button>
          <el-button icon="el-icon-remove-outline" type="danger" :loading='deleteLoading2' size="mini" v-privilege="activeMenu + 'DELETE'" @click="deleteData2">删除</el-button>
          </div>
        </div>
        <div class="common-table-div" id="topTable" style="margin-bottom:0;margin-top:0">
           <el-table id="excelTable" v-loading="dataLoading2" @selection-change="selectList" ref="dataTable2" border :height="height_right" 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 type="selection" width="55" align="center"></el-table-column>
            <el-table-column :show-overflow-tooltip="true" sortable min-width="120px" prop="anlyItemName" label="分析项目名称" align="center"></el-table-column>
            <el-table-column :show-overflow-tooltip="true" sortable min-width="80px" prop="anlyValue" label="默认值" align="center"></el-table-column>
            <el-table-column :show-overflow-tooltip="true" sortable min-width="80px" prop="unitName" label="数值单位" align="center"></el-table-column>
            <el-table-column :show-overflow-tooltip="true" sortable min-width="80px" prop="dspOrder" label="显示顺序" align="center"></el-table-column>
            <el-table-column :show-overflow-tooltip="true" sortable min-width="80px" prop="dataTypeName" label="结果类型" align="center"></el-table-column>
            <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" label="下载、预览" align="center">
              <template slot-scope="scope">
                <el-tooltip content="下载文件" placement="bottom" effect="light">
                <el-button
                :disabled='!scope.row.cretImg1'
                 type="goon" size="mini" icon="el-icon-download" circle
                 @click="downloadFile(scope.$index, scope.row.id, scope.row.cretImg.fileName,  scope.row.cretImg.oldFileName, 'LIMS_CRET_TMP_M')">
               </el-button>
                </el-tooltip>
               <el-tooltip content="预览照片" placement="bottom" effect="light">
                <el-button
                :disabled='!scope.row.cretImg1'
                 type="warning" size="mini"  icon="el-icon-view" circle
                 @click="imgPreView(scope.$index, scope.row.id, scope.row.cretImg.fileName, 'LIMS_CRET_TMP_M')">
               </el-button>
               </el-tooltip>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-col>
    </el-row>
    <div is="alertComponets" :showFlag="showFlag" :Params="Params" @refresh='refresh'></div>
    <div is="alertComponents2" :showFlag2="showFlag2" :Params2="Params2" @refresh2='refresh2'></div>
  </div>
</template>
<script>
import {getCookie,formatDate} from '@/utils/util.js';
import {cookieUserId, cookieUserName} from '@/config/config.js';
import alertComponets from './alertComponents';
import alertComponents2 from './alertComponents2';
let userName = getCookie(cookieUserName);
let userId = getCookie(cookieUserId);
export default {
  components: {alertComponets,alertComponents2},
  data () {
    return {
      table: {
        list: [],
        list2: []
      },
      dataLoading: false,
      dataLoading2: false,
      addLoading2:false,
      saveRow:{},
      icoreFilterFlag: true,
      stopLoading:false,
      startLoading:false,
      submitLoading:false,
      deleteLoading:false,
      deleteLoading2:false,
      page: {
        pageSize: 200,
        pageNum: 1,
        totalPage: 0
      },
      page2: {
        pageSize: 200,
        pageNum: 1,
        totalPage: 0
      },
      list: [],
      planSave:{},
      planId: '',
      height_top:0,
      height_right:0,
      height_bot:0,
      showFlag: false,
      Params: {},
      showFlag2: false,
      Params2: {},
      saveList:[],
      companyNameType: [],
      companyNameType1: [],
      sampleTypeNameType: [],
      multipleSelection3: [],
      multipleSelection4: [],
      search: {
        suppName: '',
        contractNo: ''
      },
      action_prod:window.location.protocol + '//' + window.location.host,
      activeMenu:''
    }
  },
  created() {
    this.height_top = window.innerHeight - 170
    this.height_right = window.innerHeight - 130
    this.activeMenu = window.top.localStorage.getItem('activeMenu')
  },
  mounted () {
    this.axios.post("pass/baseManagement/v1/sysorgs/querySysOrgCompanyAll",{}, {individualType: 'json'}).then(response => {
      this.companyNameType = response.data
    });
    this.searchData();
    // this.getDataList2();
    // this.addScreen();
    this.addScreen2()
  },
  methods: {
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
        }
        if (index === 1) {
          sums[index] = data.length;
        } 
      });
      return sums;   
    },
    refresh () {
      this.mainData();
    },
    refresh2 () {
      this.depData(this.saveRow)
    },
    addData () {
      this.showFlag = !this.showFlag;
      this.Params = null;
    },
    async addData2 () {
      if (JSON.stringify(this.saveRow)==="{}") {
       this.$message.warning('请选择你要新增的主数据！')
       return
      }
      if (!this.saveRow.testItemNo) {
        this.$message.warning('主数据无测试项目,无法新增分析项目')
        return
      }
      this.addLoading2 = true
      let result = await this.axios.post('pass/baseManagement/v1/limsstdtestitemds/queryLimsStdTestItemDPage',{object: {testItemNo: this.saveRow.testItemNo,validFlag: "1"}, pageIndex: 1, pageSize: 990}, {individualType: 'json'})
      if (result.data.list.length > 0) {
        result.data.list.forEach(item =>{item.id = ''; item.daId = this.saveRow.daId ;item.anlyValue = item.defaultValue})
        this.axios.post('pass/testManagement/v1/limstestdatads/insertupdate',result.data.list, {individualType: 'json'}).then(res =>{
          if (res.code == 0 || res.code == 200) {
            this.depData(this.saveRow)
            this.$message.success(res.message)

          }else{
            this.$message.error(res.message)
          }
          this.addLoading2 = false
        }).catch(e =>{
          this.addLoading2 = false
          this.$message.error(e.message)
        })
      }else{
        this.addLoading2 = false
        this.$message.warning('当前主数据没有分析项目数据,请前往“测试项目配置”中配置!')
      }
    },
    deleteData2 () {
      if (this.saveList.length < 1) {
       this.$message.warning('请勾选您要删除的数据！')
       return
      }
      let ids = this.saveList.map(item => item.id)
      this.deleteLoading2 = true
      this.axios.post('pass/testManagement/v1/limstestdatads/delete',ids, {individualType: 'json'}).then(res => {
        this.deleteLoading2 = false
        if (res.code == 0 || res.code ==200) {
          this.$message.success(res.message)
          this.depData(this.saveRow)
        } else {
         this.$message.error(res.message)
        }
      }).catch(e => {
        this.deleteLoading2 = false
        this.$message.error(e.message)
      })
    },
    modifyData2 () {
      if (this.saveList.length != 1) {
       this.$message.warning('请勾选一条您要修改的数据！')
       return
      }
      this.showFlag2 = !this.showFlag2;
      this.Params2 = this.saveList[0]
    },
    selectList(val){
      this.saveList = val
    },
    modifyData () {
      if (JSON.stringify(this.saveRow)==="{}") {
       this.$message.warning('请选择你要修改的数据！')
       return
      }
      this.showFlag = !this.showFlag;
      this.Params = this.saveRow
    },
    //删除
    deleteData () {
      if (JSON.stringify(this.saveRow)==="{}") {
       this.$message.warning('请选择你要删除的数据！')
       return
      }
      this.stopLoading = true
      this.axios.post('pass/testManagement/v1/limstestdatas/delete', [this.saveRow.id], {individualType: 'json'}).then(res => {
        this.stopLoading = false
        if (res.code == 0 || res.code ==200) {
          this.$message.success(res.message)
          this.searchData()
        } else {
         this.$message.error(res.message)
        }
      }).catch(e => {
        this.stopLoading = false
        this.$message.error(e.message)
      })
    },
    searchData () {
      this.page.pageNum = 1;
      this.page.pageSize = 200
      this.mainData()  
    },
    mainData(){
      this.saveRow = {}
      this.table.list2 = []
      this.dataLoading = true;
      let obj = {
        object:{
          suppName:this.search.suppName,
          contractNo:this.search. contractNo,
        },
        pageIndex:this.page.pageNum,
        pageSize:this.page.pageSize
      }
      this.axios.post('pass/testManagement/v1/limstestdatas/querylimsTestData', obj, {individualType: 'json'}).then(res => {
        this.dataLoading = false;
        if (res.code == 0 || res.code == 200) {
          this.table.list = res.data.list;
          this.page.totalPage = res.data.total;
          this.table.list.forEach(item =>{
            if (!item.savePath1) {
              item.cretImg = {}
              item.savePath1 = {}
            }else{
              item.cretImg = JSON.parse(item.savePath1)
            }
            item.cretImg1 = item.cretImg.oldFileName ? item.cretImg.oldFileName : ''
          })
        }else{
          this.$message.error(res.message)
        }
      }).catch(e => {
        this.dataLoading = false;
        this.$message.error(e.message)
      });
    },
    searchFollow(row) {
      this.saveRow = row
      this.depData(this.saveRow)
    },
    depData(row){
      let daId = row.daId ?row.daId : ''
      this.dataLoading2 = true;
      this.axios.post('pass/testManagement/v1/limstestdatads/querylimsTestDataD', {object:{daId:daId},pageIndex:1,pageSize:999}, {individualType: 'json'}).then(res => {
        this.dataLoading2 = false;
        if (res.code == 0 || res.code == 200) {
          this.table.list2 = res.data.list;
          this.table.list2.forEach(item =>{
            if (!item.savePath1) {
              item.cretImg = {}
              item.savePath1 = {}
            }else{
              item.cretImg = JSON.parse(item.savePath1)
            }
            item.cretImg1 = item.cretImg.oldFileName ? item.cretImg.oldFileName : ''
          })
        }else{
          this.$message.error(res.message)
        }
      }).catch(e => {
        this.dataLoading2 = false;
        this.$message.error(e.message)
      });
    },
    reset () {
      this.search.suppName = '';
      this.search.contractNo = ''
      this.searchData();
    },
    pageSizeChange (size) {
      this.page.pageSize = size;
      this.mainData();
    },
    pageCurrentChange (index) {
      this.page.pageNum = index;
      this.mainData();
    },
    handleSelectionChange2 (val) {
      this.multipleSelection4 = val;
    },
    pageSizeChange2 (size) {
      this.page2.pageSize = size;
    },
    pageCurrentChange2 (index) {
      this.page2.pageNum = index;
    },
    // 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);
       });
    },
    downloadFile (index, objId, objFileName, oldFileName, objType) {
      let obj = {
        objId: objId,
        objName: objType,
        fileName: objFileName//文件新名字
      }
      let fileRow = {}
      this.axios.post('pass/baseManagement/v1/limslrcfilesaves/queryListLrcFileSave', obj, {individualType: 'json'}).then(res => {
        if (res.code == 0 || res.code == 200) {
          if (res.data.length < 1) {
            this.$message.warning('暂无图片数据')
            return
          }
          fileRow = res.data[0]
          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 = oldFileName;
            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.download(this.action_prod + fileRow.fileUrl, 'download.txt', 'text/plain', fileRow.oldFileName);
        } else {
          this.$message.error(res.message)
        }
      }).catch(err =>{
        this.$message.error(err.message)
      })
    },
    imgPreView (index,objId, objFileName, objType) {
       let obj = {
        objId: objId,
        objName: objType,
        fileName: objFileName
      }
      let fileRow = {}
      this.axios.post('pass/baseManagement/v1/limslrcfilesaves/queryListLrcFileSave', obj, {individualType: 'json'}).then(res => {
        if (res.code == 0 || res.code == 200) {
          if (res.data.length < 1) {
            this.$message.warning('暂无图片数据')
            return
          }
          fileRow = res.data[0]
          var form = $('<form>'); // 定义一个form表单
          form.attr('id', 'downloadform');
          form.attr('style', 'display:none');
          form.attr('target', '_blank');
          form.attr('method', 'get');
          form.attr('action', this.action_prod + fileRow.fileUrl);
          var input1 = $('<input>');
          input1.attr('type', 'hidden');
          form.append(input1);
          $('body').append(form); // 将表单放置在web中
          form.submit(); // 表单提交
          $('#downloadform').remove();
        }else{
          this.$message.error(res.message)
        }
      }).catch(err =>{
        this.$message.error(err.message)
      })
    },
    tableHeaderCellStyle () {
      return 'color: rgb(255,255,255);border-bottom: 1px solid #DCDFE7;'
    }
  }
}
</script>
<style  scoped>
</style>

