<template>
  <div class="check-container">
    <el-form :inline="true" :model="searchParam" class="demo-form-inline">
      <el-form-item label="报告编号">
        <el-input clearable v-model="searchParam.reportNo" size="small" placeholder="请输入报告编号" style="width: 200px"/>
      </el-form-item>
      <el-form-item label="河道名称">
        <el-input clearable v-model="searchParam.driverName" size="small" placeholder="请输入河道名称" style="width: 200px"/>
      </el-form-item>
      <el-form-item label="区属">
        <driver-area-select v-model="searchParam.driverArea"></driver-area-select>
      </el-form-item>
      <el-form-item label="巡检时间">
        <el-date-picker
          style="width: 150px"
          placeholder="巡检时间"
          clearable
          size="small"
          value-format="yyyy-MM-dd"
          v-model="searchParam.inspectTime">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button :loading="tableLoading" icon="el-icon-search" @click="search" size="small" type="primary">搜索
        </el-button>
        <el-button icon="el-icon-refresh-right" @click="initSearchParam();search()"
                   type="primary" size="small"
                   style="background-color: #fff; border: 1px solid #ccc ; color: #666">清空
        </el-button>
      </el-form-item>
    </el-form>
    <el-table stripe ref="table"
              :height="tableHeight" v-loading="tableLoading" :data="dataList" border size="medium"
              row-key="id">
      <el-table-column :index="indexMethod" label="序号" type="index" width="80" align="center"/>
      <el-table-column width="120" show-overflow-tooltip prop="reportNo" label="报告单号" align="center"/>
      <el-table-column show-overflow-tooltip prop="driverName" label="河道名称" align="center"/>
      <el-table-column show-overflow-tooltip prop="driverAreaText" label="区划" align="center"/>
      <el-table-column show-overflow-tooltip prop="formatInspectTime" label="巡检时间" width="160" align="center">
      </el-table-column>
      <el-table-column show-overflow-tooltip prop="driverStart" label="起点" align="center"/>
      <el-table-column show-overflow-tooltip prop="driverEnd" label="终点" align="center"/>
      <el-table-column show-overflow-tooltip prop="inspectLength" label="长度" align="center"/>
      <el-table-column width="160" prop="createTime"  show-overflow-tooltip label="创建时间" align="center">
      </el-table-column>
      <el-table-column label="操作" v-if="isAdmin||btnRule.some(btn=>btn=='checkSub')"
                       align="left" width="80">
        <template slot-scope="{row}">
          <el-button v-if="isAdmin||btnRule.some(btn=>btn=='checkSub')" @click="check(row)" :loading="checkLoading" type="primary" size="mini" icon="el-icon-s-check">审核</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div  class="page-current">
      <el-pagination
        :current-page="currentPage"
        :page-size="pageSize"
        prev-text="上一页"
        next-text="下一页"
        background
        layout="total, prev, pager, next, jumper"
        style="display: inline-block;"
        :total="count"
        @current-change="handleCurrentChange"
      />
    </div>
    <el-drawer
      title="南京市河道无人机巡检报告"
      :visible.sync="checkVisible"
      :direction="direction"
      :size="drawerWidth"
      :before-close="handleClose">
      <report ref="checkTd" typeName="check" v-if="checkVisible" :ruleForm="ruleForm"></report>
      <div style="width: 100%;text-align: center;margin-top: 20px;margin-bottom: 20px">
        <el-button :loading="addLoading" @click="checkMain()" type="primary" size="medium" icon="el-icon-document-add">审核通过</el-button>
        <el-button :loading="rejectLoading" @click="reject()" type="warning" size="medium" icon="el-icon-takeaway-box">驳回</el-button>
        <el-button size="medium" icon="el-icon-back" @click="checkVisible = false">返回</el-button>
      </div>
    </el-drawer>
  </div>
</template>

<script>
  import reportApi from '@/api/report/report'
  import report from './report'
  import driverAreaSelect from '@/components/driverAreaSelect'
  import {mapGetters} from "vuex";

  export default {
    name: "check",
    data() {
      return {
        drawerWidth: (document.body.clientWidth - 190) + 'px',
        direction: 'rtl',
        rejectLoading: false,
        checkLoading: false,
        addLoading: false,
        checkVisible: false,
        statusList: [],
        dataList: [],
        currentPage: 1,
        count: 0,
        pageSize: parseInt((document.body.clientHeight - 250- 43) / 44.5),
        tableHeight: document.body.clientHeight - 250,
        tableLoading: false,
        searchParam: {
          inspectNo: '',
          driverName: '',
          driverArea: '',
          inspectTime: '',
        },
        ruleForm: {
          videoUrl: '',
          imageUrl: '',
          examineScore: [],
          manyiExamineScore: '',
          manyiCheckScore: '',
          totalExamineScore: '',
          totalCheckScore: '',
          desc: '',
          descriptionList: []
        },
      }
    },
    components: {report,driverAreaSelect},
    computed: {
      ...mapGetters(["btnRule", "isAdmin"])
    },
    created() {
    },
    mounted() {
      this.tableLoading = true
      this.getBaseData()
      this.getAllList()
    },
    methods: {
      handleClose(hide) {
        if (hide) {
          hide()
        }
        this.checkVisible = false
      },
      indexMethod(index) {
        return (index + 1) + ((this.currentPage - 1) * this.pageSize);
      },
      submit(type) {
        if (type == 1) {
          this.addLoading = true
        } else {
          this.rejectLoading = true
        }
        let ruleForm=JSON.parse(JSON.stringify(this.$refs.checkTd.ruleForm))
        let descriptionList = []
        if (ruleForm.questionFileList && ruleForm.questionFileList.length > 0) {
          ruleForm.questionFileList.map(item => {
            let child = {
              id: item.id + '',
              description: item.questionContent ? item.questionContent : ''
            }
            descriptionList.push(child)
          })
        }
        let examineScore = []
        if (ruleForm.filterQuestionList && ruleForm.filterQuestionList.length > 0) {
          ruleForm.filterQuestionList.map(item => {
            let child = {
              questionId: item.id + '',
              examineScore: item.examineScore,
              checkScore: item.checkScore,
              remark: item.remark
            }
            examineScore.push(child)
          })
        }
        let param = {
          id: ruleForm.id,
          imageUrl: ruleForm.imageUrl,
          videoUrl: ruleForm.videoUrl,
          examineScore: examineScore,
          manyiExamineScore: ruleForm.manyiExamineScore,
          manyiCheckScore: ruleForm.manyiCheckScore,
          awareExamineScore: ruleForm.awareExamineScore,
          awareCheckScore: ruleForm.awareCheckScore,
          totalExamineScore: ruleForm.totalExamineScore,
          totalCheckScore: ruleForm.totalCheckScore,
          desc: ruleForm.desc,
          descriptionList: descriptionList,
          remark: ruleForm.remark,
          status: type == 1 ? 3 : 4
        }
        reportApi.checkSub(param).then(res => {
          this.$message({
            type: 'success',
            message: type == 1 ? '审核通过成功！' : '驳回报告成功！'
          })
          this.checkVisible = false
          if (type == 1) {
            this.addLoading = false
          } else {
            this.rejectLoading = false
          }
          this.tableLoading = true
          this.getAllList()
        }).catch(e => {
          this.addLoading = false
          this.rejectLoading = false
        })
      },
      reject() {
        this.submit(2)
      },
      checkMain() {
        this.submit(1)
      },
      initRuleForm() {
        this.ruleForm = {
          videoUrl: '',
          imageUrl: '',
          examineScore: [],
          manyiExamineScore: '',
          manyiCheckScore: '',
          totalExamineScore: '',
          totalCheckScore: '',
          desc: '',
          descriptionList: []
        }
      },
      check(data) {
        this.checkLoading = true
        reportApi.getDetail({id: data.id}).then(res => {
          this.checkLoading = false
          this.initRuleForm()
          let ruleForm = JSON.parse(JSON.stringify(res.data))
          let questionList = ruleForm.questionList
          let filterQuestionList = []
          let categoryList = []
          questionList.map(item => {
            if (!categoryList.some(category => category == item.category)) {
              categoryList.push(item.category)
            }
          })
          categoryList.sort(function (a, b) {
            return a - b
          })
          categoryList.map(category => {
            let list = questionList.filter(item => item.category == category)
            let typeList = []
            list.map(item => {
              if (!typeList.some(type => type == item.type)) {
                typeList.push(item.type)
              }
            })
            typeList.sort(function (a, b) {
              return a - b
            })
            let filter = []
            typeList.map(type => {
              let filterType = list.filter(item => item.type == type)
              filterType[0].typeRow = filterType.length
              filter = filter.concat(filterType)
            })
            filter[0].categoryRow = list.length
            filterQuestionList = filterQuestionList.concat(filter)
          })
          ruleForm.filterQuestionList = filterQuestionList
          this.ruleForm = Object.assign({}, ruleForm)
          this.checkVisible = true
        }).catch(e => {
          this.checkVisible = false
        })
      },
      initSearchParam() {
        this.searchParam = {
          inspectNo: '',
          driverName: '',
          driverArea: '',
          inspectTime: ''
        }
      },
      search() {
        this.currentPage = 1
        this.tableLoading = true
        this.$refs.table.bodyWrapper.scrollTop = 0;
        this.getAllList()
      },
      handleCurrentChange(val) {
        this.tableLoading = true
        this.currentPage = val
        this.$refs.table.bodyWrapper.scrollTop = 0;
        this.getAllList()
      },
      getAllList() {
        let searchParam = JSON.parse(JSON.stringify(this.searchParam))
        if (searchParam.inspectTime) {
          searchParam.inspectTime = new Date(searchParam.inspectTime).getTime() / 1000
        }
        searchParam.page = this.currentPage
        searchParam.pageSize = this.pageSize
        reportApi.getCheckList(searchParam).then(res => {
          this.dataList = res.data.records ? res.data.records : []
          this.count = res.data.total
          this.tableLoading = false
        }).catch(e => {
          this.dataList = []
          this.tableLoading = false
        })
      },
      getBaseData() {
        reportApi.getBaseData().then(res => {
          const {areaList, statusList} = res.data
          this.statusList = statusList ? statusList : {}
        }).catch(e => {
          this.statusList = []
        })
      },
    }
  }
</script>

<style lang="less">
  .check-container {
    height: fit-content;
    background-color: #fff;
    box-shadow: 0 2px 4px 0 rgba(0, 0, 0, .12), 0 0 6px 0 rgba(0, 0, 0, .04);
    padding: 15px;

    .check-dialog {
      .el-dialog {
        width: 80% !important;
      }
    }
  }
</style>
