<template>
  <!-- 违章详情 -->
  <div class="app-container">
    <el-form v-show="showSearch" :model="queryParams" ref="queryForm" size="small" inline>
      <el-form-item label="关键字" prop="keyWord">
        <el-input
          v-model="queryParams.keyWord"
          placeholder="请输入关键字"
          clearable v-no-whitespace
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="违章编号" prop="violationCode">
        <el-input
          v-model="queryParams.violationCode"
          placeholder="请输入违章编号"
          clearable v-no-whitespace
          @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="违章类型" prop="violationType">
        <el-select v-model="queryParams.violationType" placeholder="请选择违章类型" clearable filterable
                   :popper-append-to-body="false" class="custom-select"
                   @change="queryParams"
        >
          <el-option v-for="item in violationTypeOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="违章日期范围" prop="timeRange">
        <el-date-picker
          v-model="queryParams.timeRange"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          clearable
          value-format="yyyy-MM-dd"
          format="yyyy-MM-dd"
        />
      </el-form-item>
      <el-form-item label="" prop="isTicket">
        <el-select v-model="queryParams.isTicket" placeholder="请选择是否下发处罚单" clearable>
          <el-option value="0" label="否" >否</el-option>
          <el-option value="1" label="是"  >是</el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" @click="handleReset">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport">导出数据</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table :data="tableList" fit highlight-current-row style="width: 100%">
      <!-- 多选 -->
      <el-table-column type="selection" width="55" align="center" @selection-change="selectionChange" />
      <el-table-column
        type="index"
        width="55"
        label="序号"
        align="center"
        :index="indexContinuous(queryParams.pageNum, queryParams.pageSize)"
      />
      <el-table-column
        v-for="(column, index) in tableColumns"
        show-overflow-tooltip
        :key="column.prop"
        :label="column.label"
        :prop="column.prop"
        align="center"
      >
        <!-- 插槽 -->
        <template v-slot="scope" v-if="column.prop == 'photoNum'">
          <span v-if="Number(scope.row.photoNum) === 1" class="see" @click="handleSeeFill(scope.row)"
          >{{ scope.row.photoNum }}</span>
          <span v-else-if="Number(scope.row.photoNum) > 1" class="see" @click="handleSeeFills(scope.row)">
            {{ scope.row.photoNum }}
          </span>
          <span v-else>-</span>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 文件查看 -->
    <el-dialog title="文件查看" :visible.sync="previewOpen" width="90%" append-to-body>
      <div style="width: 100%; height: 720px">
        <bns-kkFile-preview :items="filePreview"></bns-kkFile-preview>
      </div>
    </el-dialog>

    <el-dialog title="文件查看" :visible.sync="document" width="80%" top="5vh" append-to-body>
      <div style="width: 100%; height: 720px">
        <bns-file-list-tabs :items="fileList" />
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  exportViolationInfo,
  getTypeOfViolationSelect,
  getViolationList,
  getViolationPhoto
} from '@/api/violation/violation'
import { indexContinuous, parseTime } from '@/utils/bonus'
import bnsFileListTabs from '@/components/pro-tabs/bns-file-list-tabs.vue'
import bnsKkFilePreview from '@/components/pro-tabs/bns-kkFile-preview.vue'

export default {
  components: { bnsKkFilePreview, bnsFileListTabs },
  data() {
    return {
      showSearch: true,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        keyWord: '',
        violationCode: '',
        violationType: '',
        isTicket: '',
        timeRange: [],
      },
      violationTypeOptions: [], // 违章类型
      total: 0, // 总条数
      // 表头
      tableColumns: [
        { label: '违章工程', prop: 'proName' },
        { label: '违章编号', prop: 'violationCode' },
        { label: '违章类型', prop: 'violationTypeName' },
        { label: '责任承包商', prop: 'consName' },
        { label: '责任承包商负责人', prop: 'personName' },
        { label: '责任人', prop: 'directorName' },
        { label: '违章依据', prop: 'violationContent' },
        { label: '违章时间', prop: 'violationTime' },
        { label: '违章采集来源', prop: 'violationSource' },
        { label: '违章照片', prop: 'photoNum' },
        { label: '是否下发处罚单', prop: 'isTicket' },
        { label: '处罚完成状态', prop: 'ticketStatus' },
      ],
      // 表格数据
      tableList: [],
      dialogVisible: false,
      rows: {},
      previewOpen: false,
      document: false,
      filePreview: {
        filePreviewUrl: '',
        fileName: ''
      },
      fileList: []
    }
  },
  created() {
    this.rows = this.$route.query
    this.getTypeOfViolationSelect()
    this.getList()
  },
  methods: {
    indexContinuous,
    getTypeOfViolationSelect() {
      getTypeOfViolationSelect({ type:2 }).then(response => {
        this.violationTypeOptions = response.data
      })
    },
    // 查询
    handleQuery() {
      console.log('查询')
      this.getList()
    },
    // 重置
    handleReset() {
      console.log('重置')
      this.queryParams.timeRange = []
      this.queryParams.startTime = ''
      this.queryParams.endTime = ''
      this.$refs.queryForm.resetFields()
      this.getList()
    },
    // 获取列表
    async getList() {
      try {
        if (this.rows.type === '工程违章详情'){
          this.queryParams.proId = this.rows.proId
        }else{
          this.queryParams.violationType = this.rows.violationType
        }
        if (this.queryParams.timeRange && this.queryParams.timeRange.length > 0) {
          this.queryParams.startTime = parseTime(this.queryParams.timeRange[0])
          this.queryParams.endTime = parseTime(this.queryParams.timeRange[1])
        }else{
          this.queryParams.startTime = ''
          this.queryParams.endTime = ''
        }
        getViolationList(this.queryParams).then(response => {
          this.tableList = response.rows
          this.total = response.total
        })
      } catch (error) {
        console.log('🚀 ~ 获取列表 ~ error:', error)
      }
    },
    // 多选
    selectionChange(val) {
      console.log('selectionChange', val)
    },
    // 导出数据
    handleExport() {
      try {
        let fileName = `违章信息记录_${new Date().getTime()}.xLsx`
        let url = exportViolationInfo()
        let startTime = ''
        let endTime = ''
        if (this.queryParams.timeRange && this.queryParams.timeRange.length > 0) {
          startTime = parseTime(this.queryParams.timeRange[0])
          endTime = parseTime(this.queryParams.timeRange[1])
        } else {
          startTime = ''
          endTime = ''
        }
        const params = { ...this.queryParams, startTime, endTime }
        delete params.timeRange
        console.log('🚀 ~ 导出 ~ params:', params)
        this.derive(url, params, fileName)
      } catch (error) {
        console.log('导出数据失败', error)
      }
    },
    // 查看
    handleSeeFill(row) {
      console.log('查看', row)
      getViolationPhoto({
        violationId: row.violationId,
        bigType: '3',
        fileType: '8'
      }).then(response => {
        console.log('查看', response)
        this.filePreview = {
          filePreviewUrl: response.data[0].url,
          fileName: response.data[0].url
        }
        this.previewOpen = true
      })
    },
    handleSeeFills(row) {
      console.log('查看', row)
      getViolationPhoto({
        violationId: row.violationId,
        bigType: '3',
        fileType: '8'
      }).then(response => {
        console.log('查看', response)
        this.fileList = response.data
        this.document = true
      })
    }
  },
}
</script>

<style lang="scss" scoped>

.see {
  color: #409eff;
  cursor: pointer;
}
</style>
