<template>
  <div class="div_main">
    <div class="div_search">
      <el-form :inline="true"
               :model="formData"
               ref="ref_searchform"
               :label-width="'100px'">
        <div>
          <el-form-item label="内容摘要"
                        prop="content">
            <el-input v-model="formData.content"
                      size="small"
                      class="item_width_1"
                      clearable
                      placeholder="请输入内容摘要">
            </el-input>

          </el-form-item>

          <el-form-item label="预计处理时间"
                        prop="scheduledStartTime">
            <el-date-picker v-model="scheduledTimeRange"
                            size="small"
                            type="daterange"
                            value-format="yyyy-MM-dd"
                            @change="handleScheduledTimeChange"
                            range-separator="至"
                            start-placeholder="开始时间"
                            end-placeholder="结束时间">
            </el-date-picker>

          </el-form-item>
          <el-form-item label="创建时间"
                        prop="startTime">
            <el-date-picker v-model="timeRange"
                            size="small"
                            type="daterange"
                            value-format="yyyy-MM-dd"
                            @change="handleTimeChange"
                            range-separator="至"
                            start-placeholder="开始时间"
                            end-placeholder="结束时间">
            </el-date-picker>

          </el-form-item>
          <el-button style="margin-left:10px"
                     class="diy-button--search"
                     size="small"
                     @click="handleSearch">查询</el-button>
          <el-button style="margin-left:10px"
                     class="diy-button--reset"
                     size="small"
                     @click="resetSearch">重置</el-button>
        </div>
      </el-form>
    </div>
    <div class="div_table">
      <div class="div_btn">
        <el-button style=""
                   class="diy-button--add"
                   size="small"
                   @click="handleAdd">新增</el-button>

      </div>

      <el-table class="table"
                :data="tableData"
                border
                :height="tableHeight"
                v-loading="tableLoading"
                :header-cell-style="{background:'#2195FE',color:'#FFFFFF'}"
                style="width: 100%">

        <el-table-column label="序号"
                         header-align="center"
                         align="center"
                         type="index"
                         width="50"></el-table-column>

        <el-table-column prop="content"
                         header-align="center"
                         align="center"
                         label="内容"
                         min-width="180">
          <template slot-scope="scope">
            <div class="twoline">{{scope.row.content}}</div>

          </template>
        </el-table-column>
        <el-table-column prop="resolveWay"
                         header-align="center"
                         align="center"
                         label="解决方式"
                         min-width="120">
        </el-table-column>

        <el-table-column prop="scheduledTime"
                         header-align="center"
                         align="center"
                         label="预计处理时间"
                         width="170">
        </el-table-column>
        <el-table-column prop="remindTime"
                         header-align="center"
                         align="center"
                         label="提醒时间"
                         width="170">
        </el-table-column>
        <el-table-column prop="remark"
                         header-align="center"
                         align="center"
                         label="备注"
                         width="180">
        </el-table-column>
        <el-table-column prop="createdTime"
                         header-align="center"
                         align="center"
                         label="创建时间"
                         width="170">
        </el-table-column>

        <el-table-column label="操作"
                         fixed="right"
                         width="170"
                         header-align="center"
                         align="center"
                         class="operate">
          <template slot-scope="scope">

            <el-button type="text"
                       class="div-table-button--detail"
                       size="small"
                       @click="handleDetail(scope.row)">查看</el-button>
            <el-button v-if="!scope.row.actId"
                       type="text"
                       class="div-table-button--edit"
                       size="small"
                       @click="handleEdit(scope.row)">修改</el-button>
            <el-button v-if="!scope.row.actId"
                       type="text"
                       class="div-table-button--detail"
                       size="small"
                       @click="handleExport(scope.row)">导出</el-button>

            <el-button type="text"
                       class="div-table-button--delete"
                       size="small"
                       @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div>
        <el-pagination @size-change="handleSizeChange"
                       @current-change="handleCurrentChange"
                       :current-page.sync="pageNo"
                       :page-sizes="[10, 20, 50]"
                       :page-size="pageSize"
                       layout="sizes, prev, pager, next, total"
                       :total="total">
        </el-pagination>
      </div>
    </div>

    <!-- 修改弹出框 -->
    <el-dialog :visible.sync="formShow"
               :close-on-click-modal="false"
               :close-on-press-escape="false"
               :title="formTitle"
               width="950px"
               top="5vh"
               class="dialog-h"
               @closed="diaClose">
      <difficulty-form ref="ref_form"
                       @dialogCancle="addFormCancle"
                       @dialogOk="addFormOk"></difficulty-form>
    </el-dialog>

    <el-dialog :visible.sync="detailShow"
               :close-on-click-modal="false"
               :close-on-press-escape="false"
               :title="'详情'"
               width="950px"
               top="5vh"
               class="dialog-h"
               @closed="diaClose">
      <difficulty-detail ref="ref_detail"
                         @diaClose="diaClose"></difficulty-detail>
    </el-dialog>

  </div>
</template>

<script>

import difficultyForm from './difficultyForm'
import difficultyDetail from './difficultyDetail'
import util from '@js/util.js';
import { requestPost } from "@/js/dai/request";
import { mapGetters } from 'vuex'
import { Loading } from 'element-ui' // 引入Loading服务

let loading // 加载动画
export default {
  data () {
    return {
      loading: false,
      total: 0,
      pageSize: 10,
      pageNo: 0,
      tableLoading: false,

      scheduledTimeRange: [],
      timeRange: [],
      formData: {
        content: '',//内容摘要		
        startTime: '',//开始时间yyyy-mm-dd	
        endTime: '',//结束时间yyyy-mm-dd	
        scheduledStartTime: '',//预计处理开始时间yyyy-mm-dd	
        scheduledEndTime: '',//预计处理结束时间	yyyy-mm-dd
      },

      tableData: [],

      //form相关
      formShow: false,
      formTitle: '新增',
      detailShow: false,


    }
  },
  components: {
    difficultyForm, difficultyDetail
  },
  async created () {

  },
  async mounted () {

    //获取网格下拉框数据
    await this.loadTable()
  },

  methods: {
    handleSearch () {
      this.loadTable()
    },

    async loadTable () {
      this.tableLoading = true

      const url = "/gov/project/memoDifficulty/page"
      // const url = "http://yapi.elinkservice.cn/mock/245/gov/project/memoDifficulty/page"
      let params = {
        pageSize: this.pageSize,
        pageNo: this.pageNo,
        ...this.formData
      }

      const { data, code, msg } = await requestPost(url, params)

      if (code === 0) {
        this.total = data.total
        this.tableData = data.list
      } else {
        this.$message.error(msg)
      }
      this.tableLoading = false
    },



    diaClose () {
      if (this.formShow) {
        this.$refs.ref_form.resetData()
        this.formShow = false
      } else {
        this.detailShow = false
      }

    },


    handleDetail (row) {
      this.detailShow = true
      this.formTitle = '详情'
      this.$nextTick(() => {
        this.$refs.ref_detail.initForm(row)

      })
    },

    handleAdd () {
      this.formTitle = '新增'
      this.formShow = true
      this.$nextTick(() => {
        this.$refs.ref_form.initForm('add', null)
      })
    },

    handleEdit (row) {
      this.formTitle = '修改'
      this.formShow = true
      this.$nextTick(() => {
        this.$refs.ref_form.initForm('edit', row.id)
      })
    },

    addFormCancle () {
      this.formShow = false
    },
    addFormOk () {
      this.formShow = false
      this.loadTable()

    },

    async handleDelete (row) {

      this.$confirm("确认删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.deleteDifficulty(row)
        })
        .catch(err => {
          if (err == "cancel") {

          }

        });
    },

    async deleteDifficulty (row) {
      const url = "/gov/project/memoDifficulty/delete"
      // const url = "http://yapi.elinkservice.cn/mock/245/gov/project/memoDifficulty/delete"
      let ids = [row.id]
      let params = {
        ids: ids
      }

      const { data, code, msg } = await requestPost(url, ids)

      if (code === 0) {
        this.$message({
          type: "success",
          message: "删除成功"
        });

        this.loadTable()
      } else {
        this.$message.error(msg)
      }
    },

    //重置搜索条件
    resetSearch () {
      this.scheduledTimeRange = []
      this.timeRange = []
      this.formData = {
        content: '',//内容摘要		
        startTime: '',//开始时间yyyy-mm-dd	
        endTime: '',//结束时间yyyy-mm-dd	
        scheduledStartTime: '',//预计处理开始时间yyyy-mm-dd	
        scheduledEndTime: '',//预计处理结束时间	yyyy-mm-dd
      }
      this.pageSize = 10
      this.pageNo = 0
      this.loadTable()
    },

    //导出表格
    async handleExport (row) {
      let title = '难点堵点详情'
      let url = `/gov/project/memoDifficulty/${row.id}/exportWord`

      let params = {}

      app.ajax.exportFilePost(
        url,
        params,
        (data, rspMsg) => {

          this.download(data, title + '.doc')
        },
        (rspMsg, data) => {
          this.$message.error(rspMsg);
        }
      );

    },

    // 下载文件
    download (data, fileName) {
      if (!data) {
        return
      }

      var csvData = new Blob([data])

      if (window.navigator && window.navigator.msSaveOrOpenBlob) {
        window.navigator.msSaveOrOpenBlob(csvData, fileName);
      }
      // for Non-IE (chrome, firefox etc.)
      else {
        var a = document.createElement('a');
        document.body.appendChild(a);
        a.style = 'display: none';
        var url = window.URL.createObjectURL(csvData);
        a.href = url;
        a.download = fileName;
        a.click();
        a.remove();
        window.URL.revokeObjectURL(url);
      }

    },

    handleScheduledTimeChange (time) {
      if (time) {
        this.formData.scheduledStartTime = time[0]
        this.formData.scheduledEndTime = time[1]

      } else {
        this.formData.scheduledStartTime = ''
        this.formData.scheduledEndTime = ''

      }

    },
    handleTimeChange (time) {
      if (time) {
        this.formData.startTime = time[0]
        this.formData.endTime = time[1]

      } else {
        this.formData.startTime = ''
        this.formData.endTime = ''

      }

    },

    handleSizeChange (val) {
      this.pageSize = val
      this.pageNo = 1
      this.loadTable()
    },
    handleCurrentChange (val) {
      this.pageNo = val
      this.loadTable()
    },

    // 开启加载动画
    startLoading () {
      loading = Loading.service({
        lock: true, // 是否锁定
        text: '正在加载……', // 加载中需要显示的文字
        background: 'rgba(0,0,0,.7)' // 背景颜色
      })
    },
    // 结束加载动画
    endLoading () {
      // clearTimeout(timer);
      if (loading) {
        loading.close()
      }
    }
  },
  computed: {
    tableHeight () {

      return (this.clientHeight - 380)

    },

    ...mapGetters(['clientHeight'])
  },
  watch: {

  },
  props: {

  }
}
</script>
<style lang="scss" scoped >
.div_main {
  width: 100%;
}

.div_search {
  background: #ffffff;
  border-radius: 4px;
  padding: 30px 20px 5px;
  box-shadow: 0px 2px 12px 0px rgba(0, 0, 0, 0.1);
}

.item_width_1 {
  width: 260px;
}

.div_table {
  background: #ffffff;
  box-shadow: 0px 2px 12px 0px rgba(0, 0, 0, 0.1);
  border-radius: 4px;
  margin-top: 15px;
  padding: 23px 30px 10px;

  .table {
    margin-top: 20px;
  }
}

.el-row {
  /* margin-bottom: 20px; */
  display: flex;
  flex-wrap: wrap;
  margin-top: 10px;
  margin-right: 50px;
}

.twoline {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
}
</style>
