<template>
  <div class="problem-container">
    <el-form :inline="true" :model="searchParam" class="demo-form-inline">
      <el-form-item label="任务单号">
        <el-input
          clearable
          v-model="searchParam.questionNo"
          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="日期">
        <el-date-picker
          style="width: 200px"
          placeholder="日期"
          clearable
          size="small"
          value-format="yyyy-MM-dd"
          v-model="searchParam.time"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="区划">
        <driverAreaSelect v-model="searchParam.driverArea"></driverAreaSelect>
      </el-form-item>
      <el-form-item label="状态">
        <el-select
          clearable
          v-model="searchParam.status"
          size="small"
          placeholder="-请选择-"
          style="width: 150px"
        >
          <el-option v-for="(value,key,index) in typeList" :key="key" :label="value" :value="key" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          :loading="tableLoading"
          @click="search"
          size="small"
          icon="el-icon-search"
          type="primary"
        >搜索</el-button>
        <el-button
          @click="initSearchParam();search()"
          icon="el-icon-refresh-right"
          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"
      size="medium"
      border
    >
      <el-table-column :index="indexMethod" label="序号" type="index" width="80" align="center" />
      <el-table-column
        show-overflow-tooltip
        prop="name"
        label="问题单号"
        align="center"
        min-width="120"
      >
        <template slot-scope="scoped">
          <a
            v-if="isAdmin||btnRule.some(btn=>btn=='detail')"
            href="javascript:void(0);"
            @click="handleEdit(scoped.row,1)"
          >{{scoped.row.questionNo}}</a>
          <span v-else>{{scoped.row.questionNo}}</span>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="问题图片" align="center" min-width="80">
        <template slot-scope="{row}">
          <img style="width:20px;height:20px" :src="row.imgUrl" />
        </template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="areaName"
        label="区划"
        align="center"
        min-width="80"
      />
      <el-table-column
        show-overflow-tooltip
        prop="driverName"
        label="河湖名称"
        align="center"
        min-width="100"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="inspectTime"
        label="巡检时间"
        min-width="120"
        align="center"
      />
      <el-table-column show-overflow-tooltip prop="questionType" label="问题类型" align="center" />
      <el-table-column show-overflow-tooltip prop="statusText" label="问题状态" align="center" />
    </el-table>
    <!-- 分页 -->
    <div class="page-current">
      <el-pagination
        background
        :current-page="currentPage"
        :page-size="pageSize"
        prev-text="上一页"
        next-text="下一页"
        layout="total, prev, pager, next, jumper"
        style="display: inline-block;"
        @current-change="handleCurrentChange"
        :total="count"
      />
    </div>
    <el-drawer
      class="detail-diaog"
      title="问题详情"
      :visible.sync="visible"
      :direction="direction"
      :size="drawerWidth"
      :before-close="beforeClose"
    >
      <detailDialog @handleClose="visible=false" :visible="visible" v-if="visible" :id="id"></detailDialog>
      <div style="width: 100%;text-align: center;margin-top: 20px;margin-bottom: 20px">
        <el-button size="medium" icon="el-icon-back" @click="visible=false">返回</el-button>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import api from "@/api/question/list";
import driverAreaSelect from "@/components/driverAreaSelect";
import detailDialog from "./detailDialog";
import { mapGetters } from "vuex";

export default {
  name: "problem-container",
  data() {
    return {
      drawerWidth: document.body.clientWidth - 190 + "px",
      direction: "rtl",
      id: "",
      visible: false,
      typeList: {
        1: "待指派",
        2: "处理中",
        3: "已完成"
      },
      searchParam: {
        questionNo: "",
        driverName: "",
        time: "",
        driverArea: "",
        status: ""
      },
      dataList: [],
      currentPage: 1,
      pageSize: parseInt((document.body.clientHeight - 290 - 43) / 47),
      count: 0,
      tableLoading: false,
      tableHeight: document.body.clientHeight - 290
    };
  },
  components: { driverAreaSelect, detailDialog },
  computed: {
    ...mapGetters(["btnRule", "isAdmin"])
  },
  created() {},
  mounted() {
    this.tableLoading = true;
    let me = this;
    me.getAllList();
  },
  watch: {},
  methods: {
    beforeClose(){
      this.visible=false
    },
    handleEdit(row) {
      this.id = row.id;
      this.visible = true;
    },
    indexMethod(index) {
      return index + 1 + (this.currentPage - 1) * this.pageSize;
    },
    getAllList() {
      let searchParam = JSON.parse(JSON.stringify(this.searchParam));
      searchParam.page = this.currentPage;
      searchParam.pageSize = this.pageSize;
      if (searchParam.time && searchParam.time.length > 0) {
        searchParam.startTime = searchParam.time[0];
        searchParam.endTime = searchParam.time[1];
        delete searchParam.time;
      }
      api
        .getList(searchParam)
        .then(res => {
          this.count = res.data.total;
          this.dataList = res.data.records;
          this.tableLoading = false;
        })
        .catch(err => {
          this.dataList = [];
          this.tableLoading = false;
        });
    },
    search() {
      this.currentPage = 1;
      this.tableLoading = true;
      this.$refs.table.bodyWrapper.scrollTop = 0;
      this.getAllList();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.tableLoading = true;
      this.$refs.table.bodyWrapper.scrollTop = 0;
      this.getAllList();
    },
    initSearchParam() {
      this.searchParam = {
        questionNo: "",
        driverName: "",
        time: "",
        driverArea: "",
        status: ""
      };
    }
  }
};
</script>

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

  .el-input.is-disabled .el-input__inner {
    background-color: #fff !important;
    color: #333 !important;
  }
}
</style>
