<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          size="mini"
          @click="addHazard"
          v-hasPermi="['system:alarmhistory:remove']"
        >新增危害工作票</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="ticketList" border row-key="aId"
              :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
      <el-table-column label="延期次数" align="center" prop="delayCount" :show-overflow-tooltip="true">
        <template slot-scope="{row}">
          {{ row.delayCount || '-' }}
        </template>
      </el-table-column>
      <el-table-column label="续票状态" align="center" prop="permitStatus" :show-overflow-tooltip="true"
                       :formatter="permitStatusFormat">
      </el-table-column>
      <el-table-column label="危害工作许可证票号" align="center" prop="aId" :show-overflow-tooltip="true">
        <template slot-scope="{row}">
          {{ row.aId || '-' }}
        </template>
      </el-table-column>
      <el-table-column label="动火工作许可证票号" align="center" prop="hId" :show-overflow-tooltip="true">
        <template slot-scope="{row}">
          {{ row.hId || '-' }}
        </template>
      </el-table-column>
      <el-table-column label="动火作业等级" align="center" prop="hId" :show-overflow-tooltip="true">
        <template slot-scope="{row}">
          {{ row.hId || '-' }}
        </template>
      </el-table-column>
      <el-table-column label="限制性空间进入许可证票号" align="center" prop="cId" :show-overflow-tooltip="true">
        <template slot-scope="{row}">
          {{ row.cId || '-' }}
        </template>
      </el-table-column>
      <el-table-column label="工作内容" align="center" prop="bWorkContent" :show-overflow-tooltip="true"/>
      <el-table-column label="开始时间" align="center" prop="plantCode" :show-overflow-tooltip="true"/>
      <el-table-column label="结束时间" align="center" prop="year" :show-overflow-tooltip="true"/>
      <el-table-column label="班组" align="center" prop="fileName" :show-overflow-tooltip="true"/>
      <el-table-column label="签发人" align="center" prop="filePath" :show-overflow-tooltip="true"/>
      <el-table-column label="操作" align="center" fixed="right" width="180" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >查看
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="delay(scope.row)"
          >延期
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-download"
            @click="handleDownload(scope.row)"
          >{{ $t('下载') }}</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-download"
            @click="handleSee(scope.row)"
          >{{ $t('预览') }}</el-button>
          <!--          <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-delete"
                    >废弃
                    </el-button>-->
        </template>
      </el-table-column>
    </el-table>
    <el-dialog v-dialogDrag :title="pdf.title" :visible.sync="pdf.open" width="1300px" append-to-body>
      <div style="margin-top: -60px;float: right;margin-right: 40px;">
        <el-button size="mini" type="text" @click="openPdf">{{$t('新页面打开PDF')}}</el-button></div>
      <div style="margin-top: -30px">
        <iframe :src="pdf.pdfUrl" frameborder="0" width="100%" height="700px"></iframe>
      </div>
    </el-dialog>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {
  listTicket
} from "@/api/invoicing/permitRelation"

export default {
  data() {
    return {
      // 遮罩层
      loading: false,
      // 总条数
      total: 0,
      ticketList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
      },
      pdf : {
        title: '',
        pdfUrl: '',
        numPages: null,
        open: false,
        pageNum: 1,
        pageTotalNum: 1,
        loadedRatio: 0,
      },
      permitStatusOptions: []
    };
  },
  created() {
    this.getList();
    this.getDicts("permit_type").then(response => {
      this.permitStatusOptions = response.data;
    });
  },
  methods: {
    getList() {
      this.loading = true;
      listTicket(this.queryParams).then(response => {
        this.ticketList = response.rows;
        console.log(this.ticketList);
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      if (row.aId != null) {
        this.$router.push({
          path: '/ticket/hazardwork',
          query: {
            aId: row.aId
          }
        });
      }
      if (row.cId != null) {
        alert("限制性空间进入许可证暂未开发");
      }
    },
    /** 延期操作 */
    delay(row) {
      //TODO 缺少延期次数的校验
      if (row.delayNo != null) {
        this.$router.push({
          path: '/ticket/delaypermit',
          query: {
            vId: row.delayNo
          }
        });
      }
      if (row.delayNo == null) {
        this.$router.push({
          path: '/ticket/delaypermit',
          query: {
            aId: row.aId,
            hId: row.hId,
            cId: row.cId,
          }
        });
      }
    },
    // 票据状态类型：字典翻译
    permitStatusFormat(row, column) {
      console.log(row.permitStatus);
      return this.selectDictLabel(this.permitStatusOptions, row.permitStatus);
    },
    // 文件下载处理
    handleDownload(row) {
      var name = row.permitType + row.aId + '.docx';
      var url = row.wordUrl;
      var suffix = url.substring(url.lastIndexOf("."), url.length);
      console.log(url)
      const a = document.createElement('a')
      a.setAttribute('download', name)
      a.setAttribute('target', '_blank')
      a.setAttribute('href', process.env.VUE_APP_BASE_API + url)
      a.click()
    },
    //pdf预览
    handleSee (row){
      console.log(row.pdfUrl)
      // window.open(process.env.VUE_APP_BASE_API +'/pdf/web/viewer.html?file=' + process.env.VUE_APP_BASE_API + row.fileUrl);//path是文件的全路径地址
      this.pdf.open =true
      this.pdf.title = row.fileName
      this.pdf.pdfUrl = process.env.VUE_APP_BASE_API +'/pdf/web/viewer.html?file=' + process.env.VUE_APP_BASE_API + row.pdfUrl
    },
    openPdf(){
      window.open(this.pdf.pdfUrl);//path是文件的全路径地址
    },
    addHazard(){
      let routeData = this.$router.resolve({
        path: "/hazard",
      });
      window.open(routeData.href, '_blank');
    }
  },
}
</script>

<style scoped>

</style>
