<template>
  <div class="app-container">
    <div class="filter-container">
      <sticky style="margin-bottom: 10px;">
        <div class="sub-navbar">
          <el-button v-show="hasPerm('print:SFD')" type="success" icon="el-icon-printer" @click="printSfd">
            收费通知单
          </el-button>
          <el-button type="primary" icon="el-icon-printer" @click="batchToPrint('YSJL')">
            原始记录
          </el-button>
          <el-button type="primary" icon="el-icon-printer" @click="batchToPrint('JYBG')">
            检验报告
          </el-button>
          <el-button type="primary" icon="el-icon-printer" @click="batchToPrint('HGZ')">
            合格证
          </el-button>
          <el-button type="primary" @click="openGrant()">
            <svg-icon icon-class="form" />
            报告发放
          </el-button>
          <el-dropdown @command="printOnline">
            <el-button type="success" icon="el-icon-view">
              预览打印<i class="el-icon-arrow-down el-icon--right" />
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="YSJL">
                原始记录
              </el-dropdown-item>
              <el-dropdown-item command="JYBG">
                检验报告
              </el-dropdown-item>
              <el-dropdown-item command="HGZ">
                合格证
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
          <!--          <el-button type="success" icon="el-icon-edit" @click="editPrintState">-->
          <!--            标记为已打印-->
          <!--          </el-button>-->
        </div>
      </sticky>
      <el-form>
        <el-form-item>
          <el-input v-model="listQuery.serialNumber" placeholder="（全部）报检单编号" clearable style="width: 200px" @keyup.enter.native="handleFilter" />
          <el-input v-model="listQuery.sfdSerialNumber" placeholder="（全部）收费单编号" clearable style="width: 200px" @keyup.enter.native="handleFilter" />
          <el-input v-model="listQuery.baogaobianhao" placeholder="报告编号" clearable style="width: 200px" @keyup.enter.native="handleFilter" />
          <el-input v-model="listQuery.zhucedaima" placeholder="注册代码" clearable style="width: 200px" @keyup.enter.native="handleFilter" />
          <el-input v-model="listQuery.shiyongdanwei" placeholder="使用单位" clearable style="width: 350px" @keyup.enter.native="handleFilter" />
          <el-button type="primary" size="small" icon="el-icon-search" @click="handleFilter">
            查询
          </el-button>
          <el-button type="info" size="small" icon="el-icon-close" @click="clearQuery">
            清空
          </el-button>
          <br>
          <el-input v-model="listQuery.shiyongdengjibianhao" placeholder="请输入使用登记证编号" clearable style="width: 200px" @keyup.enter.native="handleFilter" />
          <el-input v-model="listQuery.chanpinbianhao" placeholder="请输入产品编号" clearable style="width: 185px" @keyup.enter.native="handleFilter" />
          <el-select v-model="listQuery.jianyanleibie" placeholder="请选择检验类别" style="width: 165px" filterable clearable @change="handleFilter">
            <el-option v-for="item in jylbList" v-show="item.parentId" :key="item.code" :label="item.name" :value="item.code" />
          </el-select>
          <el-date-picker v-model="listQuery.xiacijianyanriqi" type="date" value-format="yyyy-MM-dd" placeholder="请选择下次检验日期" style="width:145px" />
          <span class="radio-group-label">发放状态：</span>
          <el-radio-group v-model="listQuery.fafangState" @change="handleFilter">
            <el-radio :label="0">
              未发放
            </el-radio>
            <el-radio :label="1">
              已发放
            </el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
    </div>
    <el-table
      ref="list"
      v-adaptive="{bottomOffset: 50}"
      height="0"
      :data="list"
      border
      fit
      highlight-current-row
      stripe
      width="100%"
      size="small"
      @row-click="onRowClick"
      @selection-change="handleSelectionChange"
      @sort-change="sortChange"
    >
      <el-table-column type="selection" width="40" />
      <el-table-column fixed="left" align="center" label="序号" width="60">
        <template slot-scope="scope">
          <span v-text="getIndex(scope.$index)" />
        </template>
      </el-table-column>
      <el-table-column align="center" label="报告编号" prop="baogaobianhao" width="180" sortable="custom" />
      <el-table-column align="center" label="检验类别" prop="jianyanleibie" width="80">
        <template slot-scope="scope">
          <span v-if="scope.row.jianyanleibie === 'DJ'">定期检验</span>
          <span v-else-if="scope.row.jianyanleibie === 'JJ'">监督检验</span>
        </template>
      </el-table-column>
      <el-table-column align="center" :formatter="formatter.formatterCategory" label="检验项目" prop="neibuleibie" width="80" />
      <el-table-column align="center" label="使用登记证号" prop="shiyongdengjibianhao" width="180" sortable="custom" />
      <el-table-column align="center" label="注册代码" prop="zhucedaima" width="180" sortable="custom" />
      <el-table-column align="center" label="使用单位" prop="shiyongdanwei" width="240" />
      <el-table-column align="center" label="制造单位" prop="zhizaodanwei" width="240" />
      <el-table-column align="center" label="产品编号/出厂编号" prop="chanpinbianhao" width="220" />
      <el-table-column align="center" label="单位内编号" prop="danweineibubianhao" width="150" sortable="custom" />
      <el-table-column :formatter="formatter.getChineseName" align="center" label="检验人员" prop="jianyanrenyuan" width="140" />
      <el-table-column align="center" label="检验日期" prop="jianyankaishiriqi" width="110" sortable="custom" />
      <el-table-column align="center" label="下次检验日期" prop="xiacijianyanriqi" width="130" sortable="custom" />
      <el-table-column align="center" label="检验结论" prop="jianyanjielun" width="80" />
      <el-table-column align="center" label="打印状态" prop="printState" width="90" fixed="right">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.printState" type="success">
            已打印
          </el-tag>
          <el-tag v-else type="danger">
            未打印
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="交费状态" prop="jfState" width="90" fixed="right">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.jfState" type="success">
            已交费
          </el-tag>
          <el-tag v-else type="danger">
            未交费
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column fixed="right" align="center" label="操作" prop="" width="60">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" icon="el-icon-view" title="预览" circle @click="viewBaogao(scope.row.id, scope.row.reportVersion)" />
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page-num.sync="listQuery.pageNum" :page-row.sync="listQuery.pageRow" @pagination="getList" />
    <sign-info v-if="fafangVisible" :fafanglisttable="multipleSelection" @closeFafang="fafangVisible = false" @refulshList="handleFilter" />
    <!-- 高级查询窗口 -->
    <iframe :src="printbutton" frameborder="0" scrolling="no" height="0px" aria-disabled="true" />
  </div>
</template>

<script>
import Sticky from '@/components/Sticky'
import Pagination from '@/components/Pagination'
import SignInfo from '../sign_info'
export default {
  name: 'PrintReportGlWdy',
  components: { Sticky, Pagination, SignInfo },
  data() {
    return {
      total: 0,
      list: [],
      listQuery: {
        pageNum: 1, // 页码
        pageRow: 20, // 每页条数
        orderBy: 'id desc', // 排序字段
        hasChild: false,
        flowstatus: undefined,
        shebeizhongleidaima: '1000',
        fafangState: 0
      },
      flowUserList: [],
      dialogFormVisible: false,
      multipleSelection: [],
      multipleSelectionLink: [],
      jylbList: [],
      userList: [],
      printbutton: '',
      fafangVisible: false
    }
  },
  created() {
    this.getList()
    this.jylbList = this.$store.getters.allCategory
    this.userList = this.$store.getters.allUser
  },
  mounted() {
    const that = this
    this.common.$on('bggl-print-list', function() {
      that.handleFilter()
    })
  },
  methods: {
    async validator(rule, value, callback) {
      if (!value) {
        callback(new Error(rule.message))
      } else {
        callback()
      }
    },
    getList() {
      this.api({
        url: '/ysjl/getListPrint',
        method: 'get',
        params: this.listQuery
      }).then(data => {
        this.list = data.list
        this.total = data.total
      })
    },
    sortChange(column) {
      this.listQuery.orderBy = column.prop + ' ' + column.order.replace('ending', '')
      this.getList()
    },
    getIndex($index) {
      // 表格序号
      return (this.listQuery.pageNum - 1) * this.listQuery.pageRow + $index + 1
    },
    handleFilter() {
      // 查询事件
      this.listQuery.pageNum = 1
      this.getList()
    },
    onRowClick(row) {
      this.$refs.list.toggleRowSelection(row)
    },
    handleSelectionChange: function(val) {
      this.multipleSelection = val
    },
    printOnline(type, updateDyState) {
      if (this.multipleSelection.length !== 1) {
        this.$message('请选择单条进行打印。')
        return false
      }
      // if (!this.multipleSelection.jfState) {
      //   this.$message.error('存在未收费的报告，请联系收费核实后再进行打印。')
      //   return false
      // }
      // 查找同单位同年的其他报告
      const id = this.multipleSelection[0].id
      this.preview({
        url: '/print/generatePdf',
        method: 'post',
        data: {
          ysjlId: id,
          viewType: type,
          generateType: false
        }
      }).then(data => {
        data = data.replace(/\\/g, '/')
        window.open(data)
        this.api({ // 记录打印详细，不做打印状态改变。
          url: '/ysjl/batchRecordPrintState',
          method: 'get',
          params: {
            id: id,
            type: type
          }
        })
      })
    },
    batchToPrint(type) {
      if (this.multipleSelection.length === 0) {
        this.$message('请选择单条或者多条设备进行打印。')
        return false
      }
      let ids = ''
      for (let i = 0; i < this.multipleSelection.length; i++) {
        ids += this.multipleSelection[i].id + ','
        if (i + 1 === this.multipleSelection.length) {
          ids = ids.substring(0, ids.length - 1)
        }
        if (type === 'HGZ' && this.multipleSelection[i].jianyanjielun === '不合格') {
          this.$message('请选择合格的报告进行打印。')
          return false
        }
        if (!this.multipleSelection[i].jfState) {
          this.$message.error('存在未收费的报告，请联系收费核实后再进行打印。')
          return false
        }
      }
      this.preview({
        url: '/print/createXml',
        method: 'post',
        params: {
          ids: ids,
          type: type
        }
      }).then(data => {
        this.printbutton = 'ReportPrintApp://' + data
        this.api({ // 记录打印详细，不做打印状态改变。
          url: '/ysjl/batchRecordPrintState',
          method: 'get',
          params: {
            ids: ids,
            type: type
          }
        })
        if (type === 'JYBG') {
          this.api({ // 标记为打印状态。
            url: '/ysjl/batchUpdPrintState',
            method: 'get',
            params: {
              ids: ids,
              type: type
            }
          })
        }
      })
    },
    clearQuery() {
      this.$set(this, 'listQuery', {
        pageNum: 1, // 页码
        pageRow: 20, // 每页条数
        orderBy: 'id desc', // 排序字段
        hasChild: false,
        flowstatus: undefined,
        shebeizhongleidaima: '1000',
        fafangState: 0
      })
      this.getList()
    },
    printSfd() {
      if (this.multipleSelection.length > 1) {
        this.$message({
          type: 'info',
          message: '只能打印一份收费通知单！'
        })
        return false
      }
      if (this.multipleSelection[0].sfdId === null) {
        this.$message({
          type: 'warning',
          message: '该台设备没有收费通知单！！'
        })
        return false
      }
      if (this.multipleSelection[0].shebeizhongleidaima !== '1000') {
        this.$message({
          type: 'info',
          message: '只能锅炉打印收费通知单！'
        })
        return false
      }
      // 组织数据
      this.apibjd({
        url: '/sfd/getSfdPrintData',
        method: 'get',
        params: {
          ysjlId: this.multipleSelection[0].id
        }
      }).then(data => {
        this.preview({
          url: '/print/generatePdfListing',
          method: 'post',
          data: {
            info: data.info,
            paramList: data.paramList,
            viewType: 'SFD'
          }
        }).then(data => {
          data = data.replace(/\\/g, '/')
          window.open(data)
        })
      })
    },
    viewBaogao(id, viewType, generateType = false) {
      this.preview({
        url: '/print/generatePdf',
        method: 'post',
        data: {
          ysjlId: id,
          viewType: 'JYBG',
          generateType: generateType
        }
      }).then(data => {
        data = data.replace(/\\/g, '/')
        this.$router.push({ path: '/preview/' + id + '/JYBG/urlPath?src=' + this.$store.getters.prodName + '/static/web/viewer.html?file=' + encodeURIComponent(data) + '&v=' + Math.random() })
      })
    },
    openGrant() {
      if (this.multipleSelection.length <= 0) {
        this.$message({
          type: 'info',
          message: '请先选择需要发放的报告！'
        })
        return false
      }
      this.fafangVisible = true
    },
    handleClose() {
      // a
    }
  }
}
</script>

<style scoped>
  .app-container {
    padding-bottom: 0px;
  }
</style>
