<template>
  <!-- 增加任务 -->
  <div class="examination">
    <el-dialog :close-on-click-modal='false' @close="closeTest" width='80%' :visible.sync="testShow">
      <div slot="title" class="titleBox">
        <i class='el-icon-document' style="font-size: 20px"></i>
        <span style="margin-left:10px;font-size:18px">增加任务</span>
      </div>
      <el-row>
        <el-col :span='12'>
          <div class="common-title-name">
            <img style="width: 25px; height: 25px" src='../../../../assets/img/imgScreen/logo.png' />
            任务名称
            <el-input v-model="testItemName2" size="small" placeholder="请输入查询内容" @keyup.enter.native="getDataLists2()" style="width: 120px"></el-input>
            执行人
            <el-select v-model="creater" value-key="id" clearable filterable size="small" style="width:120px;" placeholder="默认为当前执行人">
              <el-option v-for="(item, index) in lrcEmp" :key="index + item.id" :label="item.name" :value="item">
              </el-option>
            </el-select>
          </div>
          <el-table v-loading="dataLoading" ref="dataTable" border @row-click='clickResetData' :height="heightTop" highlight-current-row :data="table.listTests"
                    :header-cell-style="tableHeaderCellStyle" :icore-filter-flag="icoreFilterFlag">
            <el-table-column type="index" label="NO" align="center"></el-table-column>
            <el-table-column min-width="120px" :show-overflow-tooltip="true" sortable prop="testItemName" label="测试名称" align="center"></el-table-column>
            <el-table-column min-width="50px" :show-overflow-tooltip="true" sortable prop="itemTypeName" label="项目类型" align="center"></el-table-column>
          </el-table>
          <div style="margin-top: 20px; margin-left: 10px">
            <el-pagination style="float:right" @size-change="SizeTest" @current-change="pageChangeTest"
                           :current-page="pageTests.pageNum" :page-sizes="[10, 20, 50, 100, 200]" :page-size="pageTests.pageSize" layout="total, sizes, prev, pager, next"
                           :total="pageTests.totalPage" background>
            </el-pagination>
          </div>
        </el-col>
        <el-col :span='12'>
          <div class="common-title-name">
            <img style="width: 25px; height: 25px; margin-left: 20px" src='../../../../assets/img/imgScreen/logo.png' />
            选中的数据
            <el-button type="goon" size="mini" @click="remove">置空</el-button>
          </div>
          <el-table v-loading="dataLoading2" ref="dataTable2" :row-class-name="tableRowClassName" border :height="heightTop" @row-dblclick="dbClick"  highlight-current-row :data="table.listTests2"
                    :header-cell-style="tableHeaderCellStyle" :icore-filter-flag="icoreFilterFlag" :row-style="styleRow">
            <el-table-column width="100px" show-overflow-tooltip label="操作" align="center">
              <template slot-scope="scope">
                <el-button type="text" style="padding:0" @click="deleteData(scope.row, scope.$index)">删除</el-button>
              </template>
            </el-table-column>
            <el-table-column min-width="80px" :show-overflow-tooltip="true" sortable prop="carNo" label="委托编号" align="center"></el-table-column>
            <el-table-column min-width="80px" :show-overflow-tooltip="true" sortable prop="batchNo" label="批次号" align="center"></el-table-column>
            <el-table-column min-width="100px" :show-overflow-tooltip="true" sortable prop="sampleNo" label="样品编号" align="center">
              <template slot-scope="scope">
                <el-input
                  v-if="tableClickIndex === scope.$index && '480405' === scope.row.itemTypeCode"
                  v-model="scope.row.sampleNo"
                  size="small"
                  style="width: 100%"
                  :id="scope.$index"
                >
                </el-input>
                <span v-else>{{scope.row.sampleNo}}</span>
              </template>
            </el-table-column>
            <el-table-column min-width="80px" :show-overflow-tooltip="true" sortable prop="addTestItem" label="增加的任务" align="center"></el-table-column>
            <el-table-column min-width="50px" :show-overflow-tooltip="true" sortable prop="addCount" label="增加个数" align="center">
              <template slot-scope=" { row }">
                <el-input v-if="tableClickIndex === row.index" type=“number” size="mini" v-model="row.addCount"></el-input>
                <span v-else>{{ row.addCount }}</span>
              </template>
            </el-table-column>
          </el-table>
          <div style="margin-top: 20px; margin-left: 10px">
            <el-pagination style="float:right" @size-change="SizeTest2" @current-change="pageChangeTest2"
                           :current-page="pageTests.pageNum2" :page-sizes="[10, 20, 50, 100, 200]" :page-size="pageTests.pageSize2" layout="total, sizes, prev, pager, next"
                           :total="table.listTests2.length" background>
            </el-pagination>
          </div>
        </el-col>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button type="goon" size="mini" :loading='addRetestLoading' icon="el-icon-check" @click="addtestData()">确定</el-button>
        <el-button size="mini" type="goon" icon="el-icon-remove-outline" @click="testCancel">取消</el-button>
      </span>
    </el-dialog>
  </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 {
  props: ["showFlag", "Params", "itemTypeCode"],
  data() {
    return {
      testShow: false,
      taskDataLoading:false,
      icoreFilterFlag: true,
      dataLoading:false,
      addRetestLoading:false,
      testItemName2:'',
      ResetData:{},
      clickData:[],
      heightTop:0,
      id:[],
      pageTests: {
        pageSize: 50,
        pageNum: 1,
        totalPage: 0,
        pageSize2: 50,
        pageNum2: 1,
        totalPage2: 0
      },
      table:{
        addTest:[],
        listTests:[],
        listTests2:[]
      },
      addList:[],
      lrcEmp: [], // 实验室人员信息
      creater: '', // 执行人
      tableClickIndex: '',
      dataLoading2: false
    };
  },
  created(){
    this.heightTop = window.innerHeight/2 - 130 < 249 ? 249 : window.innerHeight/2 - 130
  },
  mounted() {
  },
  methods: {
    //关闭弹窗
    testCancel(){
      this.table.addTest = []
      this.addList = []
      this.testShow = false
      this.addRetestLoading = false
      this.ResetData = {}
      this.creater = {}
      this.clickData = []
      this.tableClickIndex = -1
      for (let i = 0; i < this.table.listTests2.length; i++) {
        this.table.listTests2[i].addTestItem = ''
        this.table.listTests2[i].testItemNo = ''
        this.table.listTests2[i].addCount = 1
      }
      this.$emit('refresh')
    },
    closeTest(){
      this.table.addTest = []
      this.addList = []
      this.testShow = false
      this.addRetestLoading = false
      this.ResetData = {}
      this.creater = {}
      this.clickData = []
      this.tableClickIndex = -1
      for (let i = 0; i < this.table.listTests2.length; i++) {
        this.table.listTests2[i].addTestItem = ''
        this.table.listTests2[i].testItemNo = ''
        this.table.listTests2[i].addCount = 1
      }
      this.$emit('refresh')
    },
    dbClick (row, index) {
      this.tableClickIndex = row.index
    },
    tableRowClassName ({row, rowIndex}) {
      row.addCount = (row.addCount ? row.addCount : 1)
      row.index = rowIndex
    },
    clickResetData(val){
      let count = 14;
      if (this.clickData.length > count) {
        return this.$message.warning('单次添加的任务数量不能超过' + (count + 1) +'条')
      }
      this.ResetData = val
      this.clickData = [...this.table.listTests2]
      if (this.itemTypeCode && this.itemTypeCode === '480405') {
        // 增加标样任务
        if (this.table.listTests2[0].addTestItem) {
          // 复制
          let item = Object.assign({}, this.table.listTests2[0]);
          item.addTestItem = val.testItemName
          item.testItemNo = val.testItemNo
          item.itemTypeCode = val.itemTypeCode
          this.table.listTests2.push(item);
          this.clickData.push(item)
        } else {
          this.table.listTests2[0].addTestItem = val.testItemName
          this.table.listTests2[0].testItemNo = val.testItemNo
          this.table.listTests2[0].itemTypeCode = val.itemTypeCode
        }
      } else if (this.itemTypeCode && this.itemTypeCode === '480403') {
        // 增加检验任务
        for (let i = 0; i < this.clickData.length; i++) {
          if (!this.table.listTests2[i].addTestItem){
            this.table.listTests2[i].addTestItem = val.testItemName
            this.table.listTests2[i].testItemNo = val.testItemNo
          }
        }
      }
    },
    styleRow ({row, rowIndex}) {
      if (this.clickData && this.clickData.length > rowIndex) {
        return 'background-color: rgba(64, 158, 255, 0.3) !important;' // rgb(202,249,130,0.6)
      } else {
        return ''
      }
    },
    // 增加任务-确定
    addtestData(){
      if (!this.table.listTests2.length || this.table.listTests2.length < 1) {
        this.$message.warning("请至少击选中一条数据进行重测");
        return
      }
      this.addRetestLoading = true
      let addData = {
        userno: (this.creater && this.creater.id) || '',
        username: (this.creater && this.creater.name) || '',
        list:this.table.listTests2
      }
      this.axios.post('pass/testManagement/v1/limssampletests/addTest', addData, {individualType: 'json'}).then(res =>{
        if (res.code == 0 || res.code == 200) {
          this.$message.success(res.message)
          this.testCancel()
          this.$emit('refresh')
        }else{
          this.$message.error(res.message)
        }
        this.addRetestLoading = false
      }).catch(err =>{
        this.addRetestLoading = false
        this.$message.error(err.message)
      })
    },
    //查询重测数据
    getDataLists2() {
      this.pageTests.pageNum = 1
      this.getDataListTest2()
    },
    remove(){
      this.dataLoading2 = true
      if (!this.clickData || this.clickData.length < 1){
        this.dataLoading2 = false
        return
      }
      this.table.listTests2 = []
      this.tableClickIndex = -1
      for (let i = 0; i < this.clickData.length; i++) {
        this.clickData[i].addTestItem = ''
        this.clickData[i].testItemNo = ''
        this.clickData[i].addCount = 1
        this.axios.get(`pass/testManagement/v1/limssampletests/${this.clickData[i].stId}`).then(res => {
          if (res.code == 0 || res.code == 200) {
            this.clickData[i].sampleNo = res.data.sampleNo
            this.clickData[i].itemTypeCode = res.data.itemTypeCode
          } else {
          }
        }).catch(e => {
        })
      }
      this.$nextTick(() => {
        this.table.listTests2 = [...this.clickData]
        this.dataLoading2 = false
        this.$forceUpdate()
      })
    },
    deleteData(row, index){
      if (index === 0){
        return this.$message.warning('首条数据是用于生成额外任务的模板，无法移除!')
      }
      row.testItemNo = ''
      row.addTestItem = ''
      row.addCount = 1
      this.table.listTests2.splice(index,1)
      this.pageTests.totalPage2  = this.pageTests.totalPage2 - 1
    },
    //重测数据接口
    getDataListTest2 () {
      let obj = {
        object:{
          validFlag:1,
          testItemName:this.testItemName2,
          itemTypeCode: this.itemTypeCode
        },
        pageIndex:this.pageTests.pageNum,
        pageSize:this.pageTests.pageSize
      }
      this.dataLoading = true;
      this.axios.post('pass/baseManagement/v1/limsstdtestitems/queryLimsStdTestItemPage',obj,{individualType: 'json'}).then(res => {
        this.dataLoading = false;
        this.table.listTests = res.data.list;
        this.pageTests.totalPage = res.data.total;
      }).catch(e => {
        this.dataLoading = false;
      });
    },
    SizeTest (size) {
      this.pageTests.pageSize = size;
      this.getDataListTest2();
    },
    pageChangeTest (index) {
      this.pageTests.pageNum = index;
      this.getDataListTest2();
    },
    SizeTest2 (size) {
      this.pageTests.pageSize2 = size;
      this.getDataListTest2();
    },
    pageChangeTest2 (index) {
      this.pageTests.pageNum2 = index;
      this.getDataListTest2();
    },
    tableHeaderCellStyle () {
      return 'background-color: rgb(249,249,249) !important;color: rgb(73, 72, 72);border-bottom: 1px solid #DCDFE7;border-top:3px solid #3ac1ea;'
    },
    // 实验室人员信息
    getLrcEmp () {
      let that = this,
        params = {
          validFlag: '1',
          pageNum: 1,
          pageSize: 9999
        };
      let url = 'pass/baseManagement/v1/limslrcemps/';
      that.axios.get(url, {params: params}).then(function (res) {
        if (res.code === '0') {
          let arr = [];
          for (let item of res.data.list) {
            arr.push({
              id: item.empNo,
              name: item.empName
            })
          }
          that.lrcEmp = arr
        } else {
          that.$message.error(res.message);
        }
      });
    }
  },
  watch: {
    showFlag(arg) {
      this.getLrcEmp();
      this.getDataLists2()
      this.testShow = true;
      this.id = [];
      for(let i=0;i<this.Params.length;i++){
        this.id.push(this.Params[i].id)
      }
      //console.log('this.id', this.id)
      for (let i = 0; i < this.Params.length; i++) {
        this.Params[i].addTestItem = ''
        this.Params[i].addCount = 1
        this.Params[i].testItemNo = ''
      }
      this.table.listTests2 = [...this.Params]
      this.pageTests.totalPage2 = this.Params.length
    }
  },
};
</script>
<style lang="less">
.customWidth_test {
  width: 1000px !important;
}
</style>
