<template>
  <div class="app-container">
    <div class="search">
      <el-input v-model="table.getdataListParm.parammaps.rolename" placeholder="标题" style="width: 180px;" class="filter-item" clearable />
      <el-input v-model="table.getdataListParm.parammaps.rolename"  :placeholder="$t('role.cjr')" style="width: 180px;" class="filter-item" clearable />
      <el-date-picker ref="inputDatetime" v-model="table.getdataListParm.parammaps.inputDatetime" class="inputDatetime filter-item" type="daterange" :range-separator="$t('common.to')"  :start-placeholder="$t('common.startTime')" :end-placeholder="$t('common.endTime')" style="width: 250px;" />
      <el-button class="successBorder" @click="form_search('table')">{{$t('common.query')}}</el-button>
      <el-button class="successBorder" @click="handleRefresh('table')">{{$t('common.reset')}}</el-button>
    </div>
    <div class="operation">
      <el-button class="success" icon="el-icon-plus" @click="handleCreate">{{$t('formulationEvaluation.add')}}</el-button>
      <el-button class="danger" icon="el-icon-delete" @click="form_delete">{{$t('formulationEvaluation.del')}}</el-button>
    </div>
    <div class="table">
      <el-table
        :key="table.tableKey"
        v-loading="table.listLoading"
        :element-loading-text="$t('common.tableMsg')"
        :data="table.list"
        border
        fit
        highlight-current-row
        style="width: 100%;"
        :row-style="rowStyle"
        :cell-style="cellStyle"
        class="elTable table-fixed"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" align="center" width="50" />
        <el-table-column :label="$t('formulationEvaluation.xh')" align="center" type="index" width="50px">
          <template slot-scope="scope">
            <span>{{ scope.$index + (table.pageNum-1) * table.pageSize + 1 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="标题" min-width="100px" align="center" prop="rolename" />
        <el-table-column label="时间范围" min-width="100px" align="center" prop="rolename" />
        <el-table-column
:label="$t('supplier.cjsj')" min-width="100px" align="center" prop="createdtime" />
        <el-table-column  :label="$t('role.cjr')" min-width="100px" align="center" prop="createmp" />
        <el-table-column  :label="$t('errorAnalysis.cz')" align="center" width="180" class-name="small-padding fixed-width" fixed="right">
          <template slot-scope="{row}">
            <el-button class="miniSuccess" icon="el-icon-search" @click="form_see(row)" />
            <span class="centerSpan">|</span>
            <el-button class="miniSuccess" icon="el-icon-edit-outline" @click="handleUpdate(row)" />
            <span class="centerSpan">|</span>
            <el-button class="miniDanger" icon="el-icon-delete" @click="handleRowDelete(row)" />
            <span class="centerSpan">|</span>
            <a class="correcting" @click="handleStatisticsRead(row)">已读统计</a>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="table.total>=0" :total="table.total" :page.sync="table.getdataListParm.offset" :limit.sync="table.getdataListParm.pagecount" @pagination="getList" />
    </div>

    <el-dialog :title="textMap[statisticsRead.dialogStatus]" :visible.sync="statisticsRead.dialogFormVisible" :close-on-click-modal="false" width="70%">
      <div class="search">
        <el-select v-model="statisticsRead.getdataListParm.parammaps.enable"
:placeholder="$t('supplier.mc')" class="filter-item" style="width: 120px;" clearable>
          <el-option v-for="item in pastureList" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
        <el-input v-model="statisticsRead.getdataListParm.parammaps.rolename" placeholder="用户" style="width: 180px;" class="filter-item" clearable />
        <el-date-picker ref="inputDatetime" v-model="statisticsRead.getdataListParm.parammaps.inputDatetime" class="inputDatetime filter-item" type="daterange" :range-separator="$t('common.to')"  :start-placeholder="$t('common.startTime')" :end-placeholder="$t('common.endTime')" style="width: 250px;" />
        <el-button class="successBorder" @click="form_search('statisticsRead')">{{$t('common.query')}}</el-button>
        <el-button class="successBorder" @click="handleRefresh('statisticsRead')">{{$t('common.reset')}}</el-button>
      </div>
      <div class="table">
        <el-table
          :key="statisticsRead.tableKey"
          v-loading="statisticsRead.listLoading"
          :element-loading-text="$t('common.tableMsg')"
          :data="statisticsRead.list"
          border
          fit
          highlight-current-row
          style="width: 100%;"
          :row-style="rowStyle"
          :cell-style="cellStyle"
          class="elTable table-fixed"
        >
          <el-table-column :label="$t('formulationEvaluation.xh')" align="center" type="index" width="50px">
            <template slot-scope="scope">
              <span>{{ scope.$index + (statisticsRead.pageNum-1) * statisticsRead.pageSize + 1 }}</span>
            </template>
          </el-table-column>
          <el-table-column :label="$t('feedingEfficiency.mc')"min-width="100px" align="center" prop="rolename" />
          <el-table-column label="用户" min-width="100px" align="center" prop="rolename" />
          <el-table-column label="已读时间" min-width="100px" align="center" prop="createdtime" />
        </el-table>
        <pagination v-show="statisticsRead.total>=0" :total="statisticsRead.total" :page.sync="statisticsRead.getdataListParm.offset" :limit.sync="statisticsRead.getdataListParm.pagecount" @pagination="getStatisticsReadList" />
        <div slot="footer" class="dialog-footer">
          <el-button class="cancelClose cancelClose1" @click="statisticsRead.dialogFormVisible = false;getList(); ">{{$t('common.closed')}}</el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { GetDataByName, GetDataByNames, ExecDataByConfig } from '@/api/common'
import Cookies from 'js-cookie'
import { parseTime } from '@/utils/index.js'
import { MessageBox } from 'element-ui'
import Pagination from '@/components/Pagination'
export default {
  name: 'NotificationManagement',
  components: { Pagination },
  data() {
    return {
      requestParams: [
        { name: 'getPastureListJT', offset: 0, pagecount: 0, parammaps: { pastureid: Cookies.get('pastureid'), pasturename: '' }}
      ],
      pastureList: [], // 牧场
      table: {
        getdataListParm: {
          name: 'getRoleList',
          page: 1,
          offset: 1,
          pagecount: parseInt(Cookies.get('pageCount')),
          returntype: 'Map',
          parammaps: {
            pastureid: Cookies.get('pastureid'),
            rolename: '',
            createmp: '',
            enable: '',
            startTime: '',
            stopTime: '',
            inputDatetime: ''
          }
        },
        tableKey: 0,
        list: [],
        total: 0,
        listLoading: true,
        temp: {}
      },
      statisticsRead: {
        getdataListParm: {
          name: 'getRoleList',
          page: 1,
          offset: 1,
          pagecount: 10,
          returntype: 'Map',
          parammaps: {
            pastureid: Cookies.get('pastureid'),
            rolename: '',
            createmp: '',
            enable: '',
            startTime: '',
            stopTime: '',
            inputDatetime: ''
          }
        },
        tableKey: 0,
        list: [],
        total: 0,
        listLoading: true,
        temp: {},
        dialogFormVisible: false
      },
      dialogStatus: '',
      isokDisable: false,
      textMap: {
        statisticsRead: '已读统计'
      },
      selectList: {},
      requestParam: {},
      rowStyle: { maxHeight: 30 + 'px', height: 30 + 'px' },
      cellStyle: { padding: 0 + 'px' }
    }
  },

  created() {
    this.getDownList()
    this.getList()
  },

  methods: {
    getDownList() {
      GetDataByNames(this.requestParams).then(response => {
        this.pastureList = response.data.getPastureListJT.list
      })
    },
    getList() {
      this.table.listLoading = true
      GetDataByName(this.table.getdataListParm).then(response => {
        console.log('table数据', response.data.list)
        if (response.data.list !== null) {
          this.table.list = response.data.list
          this.table.pageNum = response.data.pageNum
          this.table.pageSize = response.data.pageSize
          this.table.total = response.data.total
        } else {
          this.table.list = []
        }
        setTimeout(() => {
          this.table.listLoading = false
        }, 100)
      })
    },
    form_search(item) {
      if (item == 'table') {
        if (this.table.getdataListParm.parammaps.inputDatetime !== '' && this.table.getdataListParm.parammaps.inputDatetime !== null) {
          this.table.getdataListParm.parammaps.startTime = parseTime(this.table.getdataListParm.parammaps.inputDatetime[0], '{y}-{m}-{d}')
          this.table.getdataListParm.parammaps.stopTime = parseTime(this.table.getdataListParm.parammaps.inputDatetime[1], '{y}-{m}-{d}')
        } else {
          this.table.getdataListParm.parammaps.inputDatetime = ''
          this.table.getdataListParm.parammaps.startTime = ''
          this.table.getdataListParm.parammaps.stopTime = ''
        }
        this.table.getdataListParm.offset = 1
        this.getList()
      } else if (item == 'statisticsRead') {
        if (this.statisticsRead.getdataListParm.parammaps.inputDatetime !== '' && this.statisticsRead.getdataListParm.parammaps.inputDatetime !== null) {
          this.statisticsRead.getdataListParm.parammaps.startTime = parseTime(this.statisticsRead.getdataListParm.parammaps.inputDatetime[0], '{y}-{m}-{d}')
          this.statisticsRead.getdataListParm.parammaps.stopTime = parseTime(this.statisticsRead.getdataListParm.parammaps.inputDatetime[1], '{y}-{m}-{d}')
        } else {
          this.statisticsRead.getdataListParm.parammaps.inputDatetime = ''
          this.statisticsRead.getdataListParm.parammaps.startTime = ''
          this.statisticsRead.getdataListParm.parammaps.stopTime = ''
        }
        this.statisticsRead.getdataListParm.offset = 1
        this.getList()
      }
    },
    handleRefresh(item) {
      console.log('点击了重置')
      if (item == 'table') {
        // table
      } else if (item == 'statisticsRead') {
        // 已读统计
      }
    },
    handleCreate() {},

    form_see(row) {},
    handleUpdate(row) {},
    handleRowDelete(row) {},
    handleStatisticsRead(row) {
      this.statisticsRead.temp = Object.assign({}, row)
      this.statisticsRead.dialogStatus = 'statisticsRead'
      this.statisticsRead.dialogFormVisible = true
      this.getStatisticsReadList()
    },
    getStatisticsReadList() {
      this.statisticsRead.listLoading = true
      GetDataByName(this.statisticsRead.getdataListParm).then(response => {
        if (response.data.list !== null) {
          this.statisticsRead.list = response.data.list
          this.statisticsRead.pageNum = response.data.pageNum
          this.statisticsRead.pageSize = response.data.pageSize
          this.statisticsRead.total = response.data.total
        } else {
          this.statisticsRead.list = []
        }
        setTimeout(() => {
          this.statisticsRead.listLoading = false
        }, 100)
      })
    },
    handleSelectionChange(val) {
      console.log('勾选数据', val)
      this.selectList = val
    },
    form_delete() {
      console.log('点击了删除')
      if (this.selectList.length == 0) {
        this.$message({ type: 'error', message: this.$t('message.msg166'), duration: 2000 })
      } else {
        MessageBox.confirm( this.$t('common.delMsg'), {
          confirmButtonText: this.$t('common.confirm'), cancelButtonText: this.$t('common.cancel'), type: 'warning'
        }).then(() => {
          console.log(this.selectList)
          this.requestParam.common = { 'returnmap': '0' }
          this.requestParam.data = []
          for (let i = 0; i < this.selectList.length; i++) {
            this.requestParam.data[i] = { 'name': 'checkDeleteRole', 'type': 'v', 'parammaps': {
              'pastureid': this.selectList[i].pastureid,
              'id': this.selectList[i].id
            }}
          }
          this.requestParam.data[this.selectList.length] = { 'name': 'insertSpotList', 'resultmaps': { 'list': this.selectList }}
          this.requestParam.data[this.selectList.length].children = []
          this.requestParam.data[this.selectList.length].children[0] = { 'name': 'deleteRole', 'type': 'e', 'parammaps': {
            'pastureid': '@insertSpotList.pastureid',
            'id': '@insertSpotList.id'
          }}
          ExecDataByConfig(this.requestParam).then(response => {
            console.log('删除保存发送参数', this.requestParam)
            if (response.msg === 'fail') {
              this.$notify({ title: this.$t('common.delfail'), message: response.data, type: 'warning', duration: 2000 })
            } else {
              this.$notify({ title: '',message: this.$t('common.delSuccess'), type: 'success', duration: 2000 })
              this.getList()
            }
          })
        })
      }
    }
  }
}
</script>
<style lang="scss" scoped>
  .search{margin-top:10px;}

 </style>
