<template>
  <div class="report-container">
    <el-form :inline="true" :model="searchParam" class="demo-form-inline">
      <el-form-item label="任务单号">
        <el-input clearable v-model="searchParam.inspectNo" size="small" placeholder="请输入任务单号" style="width: 200px"/>
      </el-form-item>
      <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="区属">
        <driverAreaSelect v-model="searchParam.driverArea"></driverAreaSelect>
      </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 label="状态">
        <el-select style="width: 150px" filterable clearable v-model="searchParam.status" size="small"
                   placeholder="请选择状态">
          <el-option v-for="(value,key,index) in statusList" :key="index" :label="value" :value="key"/>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="河道类型" prop="driverExam">
        <el-select clearable style="width: 150px" v-model="searchParam.driverExam" size="small"
                   placeholder="请选择河道类型">
          <el-option key="1" label="国、省考河道" value="1"/>
          <el-option key="2" label="市、区级河道" value="2"/>
        </el-select>
      </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 min-width="120" show-overflow-tooltip prop="reportNo" label="报告单号" align="center"/>
      <el-table-column min-width="120" show-overflow-tooltip prop="inspectNo" label="任务单号" align="center"/>
      <el-table-column min-width="70" show-overflow-tooltip prop="num" 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="巡检时间" min-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 min-width="80" show-overflow-tooltip prop="statusText" label="状态" align="center"/>
      <el-table-column label="操作" v-if="isAdmin||btnRule.some(btn=>btn=='generate')"
                       align="left" width="80">
        <template slot-scope="{row}">
          <el-button v-if="row.status==4&&(isAdmin||btnRule.some(btn=>btn=='generate'))" @click="generate(row,'edit')"
                     :loading="generateLoading" type="primary" size="mini"
                     icon="el-icon-edit">编辑
          </el-button>
          <el-button v-if="row.status==1&&(isAdmin||btnRule.some(btn=>btn=='generate'))" @click="generate(row)"
                     :loading="generateLoading" type="primary" size="mini"
                     icon="el-icon-document-checked">生成
          </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="generateVisible"
      :direction="direction"
      :size="drawerWidth"
      :before-close="handleClose">
      <report ref="generateTd" v-if="generateVisible" typeName="index" :ruleForm="ruleForm"></report>
      <div style="width: 100%;text-align: center;margin-top: 20px;margin-bottom: 20px">
        <el-button :loading="addLoading" @click="generateMain()" type="primary" size="medium"
                   icon="el-icon-document-add">提交
        </el-button>
        <el-button size="medium" icon="el-icon-back"
                   @click="generateVisible = false">返回
        </el-button>
      </div>
    </el-drawer>
  </div>
</template>

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

  export default {
    name: "report-container",
    data() {
      return {
        drawerWidth: (document.body.clientWidth - 190) + 'px',
        direction: 'rtl',
        generateLoading: false,
        addLoading: false,
        generateVisible: false,
        statusList: [],
        dataList: [],
        currentPage: 1,
        count: 0,
        pageSize: parseInt((document.body.clientHeight - 300 - 43) / 44.5),
        tableHeight: document.body.clientHeight - 300,
        tableLoading: false,
        searchParam: {
          reportNo: '',
          inspectNo: '',
          driverName: '',
          driverArea: '',
          inspectTime: '',
          status: '',
          // driverExam:''
        },
        ruleForm: {
          videoUrl: '',
          imageUrl: '',
          examineScore: [],
          manyiExamineScore: '',
          manyiCheckScore: '',
          totalExamineScore: '',
          totalCheckScore: '',
          desc: '',
          descriptionList: [],
          filterQuestionList: []
        },
      }
    },
    components: {report, driverAreaSelect},
    created() {
    },
    computed: {
      ...mapGetters(["btnRule", "isAdmin"])
    },
    mounted() {
      this.tableLoading = true
      this.getBaseData()
      this.getAllList()
    },
    methods: {
      indexMethod(index) {
        return (index + 1) + ((this.currentPage - 1) * this.pageSize);
      },
      handleClose(hide) {
        if (hide) {
          hide()
        }
        this.generateVisible = false
      },
      generateMain() {
        this.addLoading = true
        let ruleForm = JSON.parse(JSON.stringify(this.$refs.generateTd.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
        }
        reportApi.generateSub(param).then(res => {
          this.$message({
            type: 'success',
            message: '报告生成成功！'
          })
          this.generateVisible = false
          this.addLoading = false
          this.tableLoading = true
          this.getAllList()
        }).catch(e => {
          this.addLoading = false
        })
      },
      initRuleForm() {
        this.ruleForm = {
          videoUrl: '',
          imageUrl: '',
          examineScore: [],
          manyiExamineScore: '',
          manyiCheckScore: '',
          totalExamineScore: '',
          totalCheckScore: '',
          desc: '',
          descriptionList: [],
          filterQuestionList: [],
        }
      },
      generate(data, type) {
        let method = 'generate'
        if (type) {
          method = 'getDetail'
        }
        this.generateLoading = true
        reportApi[method]({id: data.id}).then(res => {
          this.generateLoading = 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.generateVisible = true
        }).catch(e => {
          this.generateLoading = false
        })
      },
      initSearchParam() {
        this.searchParam = {
          reportNo: '',
          inspectNo: '',
          driverName: '',
          driverArea: '',
          inspectTime: '',
          status: '',
          // driverExam:''
        }
      },
      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.getGenerateList(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">
  .report-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;

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