<template>
  <div class="app-container">
    <div class="filter-container">
      <sticky style="margin-bottom: 10px;">
        <div class="sub-navbar">
          <el-button type="primary" icon="el-icon-printer" @click="batchPrintRwd()">
            打印任务单
          </el-button>
          <el-button type="success" icon="el-icon-printer" @click="printRwd()">
            预览打印
          </el-button>
        </div>
      </sticky>
      <el-form>
        <el-form-item>
          <el-input v-model="listQuery.searchRwd.serialNumber" placeholder="报检单号" clearable style="width: 160px" @keyup.enter.native="handleFilter" />
          <el-select v-model="listQuery.searchRwd.teamId" placeholder="请选择检验小组" style="width: 150px;" @change="handleFilter">
            <el-option v-for="team in teams" :key="team.id" :label="team.name" :value="team.id" />
          </el-select>
          <el-input v-model="listQuery.searchRwd.shiyongdanwei" placeholder="使用单位" clearable style="width: 400px" @keyup.enter.native="handleFilter" />
          <el-button type="primary" icon="el-icon-search" @click="handleFilter">
            查询
          </el-button>
          <el-button type="info" icon="el-icon-clear" @click="clearSearch">
            清除
          </el-button>
          <br>
          <el-select v-model="listQuery.searchRwd.quhuamingcheng" style="width: 120px" placeholder="请选择所属区划" clearable value="" @change="handleFilter">
            <el-option
              v-for="item in areas"
              :key="item.value"
              :label="item.label"
              :value="item.label"
            />
          </el-select>
          <el-select v-model="listQuery.searchRwd.jianyanleibie" style="width: 120px" placeholder="请选择检验类别" clearable value="" @change="handleFilter">
            <el-option value="DJ" label="定期检验" />
            <el-option value="JJ" label="监督检验" />
          </el-select>
          <el-date-picker v-model="listQuery.searchRwd.yuejianriqiStart" type="date" placeholder="约检日期（起）" value-format="yyyy-MM-dd" clearable style="width: 160px" @keyup.enter.native="handleFilter" />
          <el-date-picker v-model="listQuery.searchRwd.yuejianriqiEnd" type="date" placeholder="约检日期（止）" value-format="yyyy-MM-dd" clearable style="width: 160px" @keyup.enter.native="handleFilter" />
          <el-date-picker v-model="listQuery.searchRwd.createTimeStart" type="date" placeholder="创建日期（起）" value-format="yyyy-MM-dd" clearable style="width: 160px" @keyup.enter.native="handleFilter" />
          <el-date-picker v-model="listQuery.searchRwd.createTimeEnd" type="date" placeholder="创建日期（止）" value-format="yyyy-MM-dd" clearable style="width: 160px" @keyup.enter.native="handleFilter" />
        </el-form-item>
      </el-form>
    </div>
    <el-table
      ref="list"
      v-loading="showLoading"
      element-loading-text="正在加载"
      element-loading-spinner="el-icon-loading"
      :data="list"
      border
      fit
      highlight-current-row
      stripe
      width="100%"
      height="600px"
      @row-click="onRowClick"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="40" />
      <el-table-column align="center" label="序号" width="50" fixed="left">
        <template slot-scope="scope">
          <span v-text="getIndex(scope.$index)" />
        </template>
      </el-table-column>
      <el-table-column align="center" label="报检单号" prop="serialNumber" width="140" />
      <el-table-column align="center" label="设备种类" prop="shebeizhongleidaima" width="80" />
      <el-table-column align="center" label="检验类别" prop="jianyanleibie" width="90">
        <template slot-scope="scope">
          <span v-if="scope.row.jianyanleibie === 'DJ'">
            定期检验
          </span>
          <span v-else-if="scope.row.jianyanleibie === 'JJ'">
            监督检验
          </span>
          <span v-else-if="scope.row.jianyanleibie === 'JJ' && scope.row.neibuleibie === 'ZZ'">
            制造检验
          </span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="使用单位" prop="shiyongdanwei" width="250" />
      <el-table-column align="center" label="台数(总/分)" prop="totalNum" width="100">
        <template slot-scope="scope">
          <el-button style="margin-left:1px" size="small" circle>
            <span style="color: #67C23A;font-weight: bold">{{ scope.row.totalNum }}</span> / <span style="color: #F56C6C;font-weight: bold">{{ scope.row.total }}</span>
          </el-button>
        </template>
      </el-table-column>
      <el-table-column align="center" label="联系电话" prop="shiyongdanweiDianhua" width="120" />
      <el-table-column align="center" label="组名称" prop="teamName" width="100" />
      <el-table-column align="center" label="区划名称" prop="quhuamingcheng" width="100" />
      <el-table-column align="center" label="组长" prop="groupLeader" :formatter="formatter.getChineseName" width="70" />
      <el-table-column align="center" label="组成员" prop="jianyanrenyuan" />
      <el-table-column align="center" label="约检日期" prop="yuejianriqi" width="100" />
      <el-table-column align="center" label="创建日期" prop="createTime" width="100" />
      <el-table-column align="center" label="查看报检" width="60">
        <template slot-scope="scope">
          <el-button type="success" size="small" circle icon="el-icon-view" title="预览报检单" @click="editBjd(scope.$index)" />
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="listQuery.pageNum"
      :page-size="listQuery.pageSize"
      :total="totalCount"
      :page-sizes="[10, 20, 40, 100]"
      background
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <iframe :src="printbutton" frameborder="0" scrolling="no" height="0px" aria-disabled="true" />
  </div>
</template>

<script>
import Sticky from '@/components/Sticky'
// import router from '../../router'
// import store from '../../store'
export default {
  name: 'RwdList',
  components: { Sticky },
  data() {
    return {
      totalCount: 0,
      list: [],
      teams: [],
      listQuery: {
        pageNum: 1, // 页码
        pageSize: 20, // 每页条数
        searchRwd: { // 任务分配人 跟 超级管理员可以查看所有 其他人只能看自己
          isLookRwdAll: this.hasRole('JDFPRW') || this.hasRole('FPRW') || this.hasRole('ADMIN')
        },
        orderBy: 'id desc'
      },
      multipleSelection: [],
      areas: [], // 区划数组
      selection: [],
      showLoading: true,
      departmentId: this.$store.getters.departmentId,
      printbutton: ''
    }
  },
  created() {
    this.getList()
    this.getTeamList(this.departmentId)
    this.getArea()
  },
  mounted() {
    const that = this
    this.common.$on('renwudan-list', function() {
      that.handleFilter()
    })
  },
  methods: {
    getList() {
      this.showLoading = true
      this.apibjd({
        url: '/renwu/listrwd',
        method: 'get',
        params: this.listQuery
      }).then(data => {
        this.list = data.list
        this.totalCount = data.totalCount
        this.showLoading = false
      })
    },
    getArea() { // 查询区划
      this.api({
        url: '/area/getShiArea',
        method: 'get',
        params: {
          shidaima: '370100'
        }
      }).then(data => {
        this.areas = data
      })
    },
    handleSizeChange(val) {
      // 改变每页数量
      this.listQuery.pageSize = val
      this.handleFilter()
    },
    handleCurrentChange(val) {
      // 改变页码
      this.listQuery.pageNum = val
      this.getList()
    },
    handleFilter() {
      // 查询事件
      this.listQuery.pageNum = 1
      this.getList()
    },
    getIndex($index) {
      // 表格序号
      return (this.listQuery.pageNum - 1) * this.listQuery.pageSize + $index + 1
    },
    // 表格单击选中行
    onRowClick(row) {
      this.$refs.list.toggleRowSelection(row)
    },
    // 获取选中行的bjdId
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    handleDialogSelectionChange(val) {
      this.selection = val
    },
    clearSearch() {
      this.listQuery.searchRwd = {
      }
      this.getList()
    },
    editBjd($index) {
      const bjd = this.list[$index]
      const type = '/finish'
      if (bjd.jianyanleibie === 'DJ') {
        this.$router.push({ path: '/bjd/bjd-dj-input/' + bjd.bjdId + '/' + bjd.shebeizhongleidaima + '/' + bjd.shebeizhongleidaima + type })
      } else {
        this.$router.push({ path: '/bjd/bjd-jj-input/' + bjd.bjdId + '/' + bjd.shebeizhongleidaima + '/' + bjd.shebeizhongleidaima + type })
      }
    },
    printRwd() {
      if (this.multipleSelection.length > 1) {
        this.$message({ message: '只能打印一份任务单', type: 'error' })
        return
      }
      this.apibjd({
        url: '/renwu/printRwd',
        method: 'get',
        params: {
          rwdIds: this.multipleSelection[0].id
        }
      }).then(data => {
        this.preview({
          url: '/print/generatePdfListing',
          method: 'post',
          data: {
            info: data.info,
            paramList: data.paramList,
            viewType: 'RWD'
          }
        }).then(data => {
          data = data.replace(/\\/g, '/')
          // router.push({ path: '/preview/1/1/urlPath?src=' + store.getters.prodName + '/static/web/viewer.html?file=' + encodeURIComponent(data) + '&v=' + Math.random() })
          window.open(data)
        })
      })
    },
    batchPrintRwd() {
      if (this.multipleSelection.length === 0) {
        this.$message({ message: '请选择至少一条数据进行打印！', type: 'error' })
        return false
      }
      this.apibjd({
        url: '/renwu/printRwdBatch',
        method: 'get',
        params: {
          rwdId: this.$refs.list.selection.map(rwd => {
            return rwd.id
          }).join(',')
        }
      }).then(data => {
        this.preview({
          url: '/print/generatePdfListingBatch',
          method: 'post',
          data: {
            list: data,
            viewType: 'RWD'
          }
        }).then(data => {
          if (data === 'success') {
            this.preview({
              url: '/print/createXmlOther',
              method: 'post',
              params: {
                ids: this.$refs.list.selection.map(rwd => {
                  return rwd.bjdId
                }).join(','),
                type: 'RWD'
              }
            }).then(data => {
              this.printbutton = 'ReportPrintApp://' + data
            })
          } else {
            this.$message({ message: '未能成功调用打印程序！', type: 'error' })
          }
        })
      })
    },
    getTeamList(departmentId) { // 获取对应组
      this.apibjd({
        url: '/team/getTeamList',
        method: 'get',
        params: { departmentId }
      }).then(data => {
        this.teams = data
      })
    }
  }
}
</script>
