<template>
  <!-- 保温箱追溯 -->
  <div style="overflow-y: auto;">
    <actionBar :operateList="operateList" :formList="formList" :ruleForm="searchRuleForm" @openModel="openModel"
      @searchProtocol="searchProtocol" @handleScroll="handleScroll" @remoteMethod="remoteMethod">
    </actionBar>
    <div class="card_content">
      <!-- 表单 -->
      <tables :tableList="tableList" :tableData="tableData"></tables>
      <!-- 分页 -->
      <div v-if="Total">
        <pagination :total="Total" :currentPage="Pagination.PageIndex" @changeSize="changeSize"
          @changeCurrent="changeCurrent">
        </pagination>
      </div>
    </div>
  </div>
</template>

<script>
  import {
    getcoolerboxall,
    getCoolerBox,
    coolerExportExecl
  } from '@/api/incubator'
  import actionBar from '@/components/actionBar'
  import tables from '@/components/tables'
  import pagination from '@/components/pagination'
  import forms from '@/components/forms'
  import {
    employee
  } from "./retrospect.js";
  export default {
    name: 'IncubatorManagement',
    components: {
      actionBar,
      tables,
      pagination,
      forms,
    },
    data() {
      return {
        operateList: [{
          type: 'export',
          colour: 'success',
          title: '导出Excel',
        }],
        formList: [{
          type: 'select',
          label: '保温箱',
          field: 'coolerBoxId',
          placeholder: '请选择保温箱',
          filterable: true,
          options: []
        }, {
          type: 'picker',
          label: '使用日期',
          field: 'dateService',
          placeholder: '使用日期',
        }],
        searchRuleForm: {
          coolerBoxId: null,
          dateService: [],
        },
        searchValue: {
          coolerBoxId: null,
          dateService: [],
        },
        Pagination: {
          PageIndex: 1,
          PageSize: 10,
        },
        Total: 0,
        tableData: [],
        tableList: [],
        page: 1,
        staffName: '',
        limitNoil: true,
        // 是否预冷
        iceColdFlag: false,
        userList: {},
      }
    },
    mounted() {
      const arr = localStorage.getItem('userList')
      this.userList = JSON.parse(arr)
      if (this.userList.dept) {
        this.iceColdFlag = this.userList.dept.isCoolerReleaseCold
      }
      if (!this.iceColdFlag) {
        var tableList = employee()
        this.tableList = removePointById(tableList, ['coolerBoxStarTime', 'cold_temperatures', 'cooler-cold-address',
          'cooler-cold-users'
        ])
      } else {
        this.tableList = employee()
      }

      function removePointById(arr1, arr2) {
        for (let i = 0; i < arr2.length; i++) {
          for (let j = 0; j < arr1.length; j++) {
            if (arr2[i] == arr1[j].field) {
              let indexs = arr1.indexOf(arr1[j]);
              arr1.splice(indexs, 1);
            }
          }
        }
        return arr1
      }
      this.getList()
      this.getIncubator()
    },
    methods: {
      // 搜索
      searchProtocol(value) {
        this.searchValue = value
        this.getList()
      },
      // 获取保温箱使用列表
      getList() {
        var params = {
          page: this.Pagination.PageIndex,
          pageSize: this.Pagination.PageSize,
          ...this.searchValue
        }
        params.startTime = params.dateService[0]
        params.endTime = params.dateService[1]
        delete params.dateService
        getcoolerboxall(params).then(res => {
          if (res.code == 200) {
            this.tableData = res.data.list
            this.Total = res.data.count
          }
        })
      },
      // 触底事件
      handleScroll(type) {
        if (this.limitNoil) {
          this.getIncubator()
        }
      },
      // 远程搜索
      remoteMethod(event) {
        this.staffName = event
        this.page = 1
        this.limitNoil = true
        this.formList.forEach(item => {
          if (item.field == 'coolerBoxId') {
            item.options = []
          }
        })
        this.getIncubator()
      },
      // 获取保温箱列表
      getIncubator() {
        let params = {
          page: this.page,
          pageSize: 10,
          status: '2',
          name: this.staffName,
        }
        getCoolerBox(params).then(res => {
          if (res.code == 200) {
            let arr = res.data.list
            let arrList = []
            arr.forEach(item1 => {
              var arrData = {
                label: null,
                value: null,
              }
              arrData.label = item1.name
              arrData.value = item1.id
              arrList.push(arrData)
            })
            if (this.limitNoil == true) {
              this.formList.forEach(item => {
                if (item.field == 'coolerBoxId') {
                  item.options = item.options.concat(arrList)
                }
              })
            }
            if (arrList.length >= 10) {
              this.page = ++this.page;
            } else {
              // 已经没数据了 不需要增加数据
              this.limitNoil = false;
            }
          }
        })
      },
      openModel(type) {
        if (type == 'export') {
          this.$confirm('确定导出Excel, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            var params = {
              ...this.searchValue
            }
            params.startTime = params.dateService[0]
            params.endTime = params.dateService[1]
            delete params.dateService
            coolerExportExecl(params).then(response => {
              if (response.fileName) {
                let fileName = decodeURI(response.fileName);
                if (window.navigator.msSaveOrOpenBlob) {
                  navigator.msSaveBlob(new Blob([response.data]), fileName);
                } else {
                  let url = window.URL.createObjectURL(new Blob([response.data]));
                  let link = document.createElement('a');
                  link.style.display = 'none';
                  link.href = url;
                  link.setAttribute('download', fileName);
                  document.body.appendChild(link);
                  link.click();
                }
              } else {
                this.$message.error('未获取到文件名,导出失败');
              }
            })
          }).catch(() => {
            console.log('已取消')
          });
        }
      },
      changeSize(val) {
        this.Pagination.PageSize = val
        this.getList()
      },
      changeCurrent(val) {
        this.Pagination.PageIndex = val
        this.getList()
      },
    }
  }
</script>

<style lang="scss" scoped></style>
