<template lang='pug'>
    el-container(style="padding:0px;" :style="{height:currentHeight-65+'px'}")
        el-header(style='height:42px;line-height:42px;border: 1px solid rgba(195, 195, 195, 1);background: #f4f4f4;')
            span.title-bold.title-left-color() 历史指令
            el-popover( placement="bottom" width="150")
              el-button( slot="reference" size='mini' type="primary" icon="el-icon-s-tools" style='float:right;transform:translateY(6px)')  筛选
              el-checkbox-group( v-model="checkList")
                el-col( :span="24")
                    el-checkbox( v-for="(item,index) in tableHeader" :label="item" :key="index" )
                      span {{item.label}}
        el-main.box_main
            el-card()
                div(style="width:100%;")
                    el-form( :inline="true" :model="dataForm" size="mini" @keyup.enter.native="getDataList()")
                        el-form-item(label="搜索:")
                            el-input( v-model="dataForm.par" placeholder="请输入指令ID/报警编号搜索" clearable)
                        el-form-item( label="执行时间:")
                            el-date-picker(type="datetime" value-format="yyyy-MM-dd HH:mm:ss" style="width:175px;" v-model="dataForm.startTime" placeholder="选择开始时间"  clearable)
                        el-form-item( label="至")
                            el-date-picker( type="datetime" value-format="yyyy-MM-dd HH:mm:ss" style="width:175px;" v-model="dataForm.endTime" placeholder="选择结束时间" clearable)
                        el-button(@click="getDataList()" size='mini' icon="el-icon-search" type="primary") 查询
                //- el-table(:data="dataList" width="100%" @sort-change='sortChange' v-loading="dataListLoading" :stripe="true" :header-cell-style="{background:'#EEF8FF',color:'#333333'}")
                    el-table-column( type='index' header-align='center', align='center', label='序号')
                      template(scope="scope")
                        span {{scope.$index+(pageIndex - 1) * pageSize + 1}}
                    el-table-column( prop="eventNumber" header-align="center" align="center" label="报警编号" sortable='custom' width=250)
                    el-table-column( prop="instruction" header-align="center" align="center" label="指令ID" sortable='custom' width=260)
                    el-table-column( prop="resourceName" header-align="center" align="center" label="资源点名称" sortable='custom'  width=200)
                    el-table-column( prop="resourceType" header-align="center" align="center" label="资源点类型" sortable='custom')
                    el-table-column( prop="resourceStatus" header-align="center" align="center" label="资源点状态" sortable='custom')
                    el-table-column( prop="actionName" header-align="center" align="center" label="资源点动作" sortable='custom')
                    el-table-column( prop="createUser" header-align="center" align="center" label="执行人" sortable='custom')
                    el-table-column( prop="createTime" header-align="center" align="center" label="执行完成时间" sortable='custom' width=160)
                    el-table-column( prop="resultName" header-align="center" align="center" label="执行结果" sortable='custom')
                        template(slot-scope="scope")
                            font(v-if="scope.row.result === 1" color='#67C23A') 成功
                            font(v-if="scope.row.result === 2" color='#F56C6C') 失败
                            font(v-if="scope.row.result === 3" color='#F56C6C') 中止
                    el-table-column( prop header-align="center" align="center" label="操作")
                        template(slot-scope="scope")
                            el-button(size='mini' type='text'  @click="queryHandle(scope.row)" v-if="isAuth('alarms:instruct:info')") 详情
                drag-table( :data="dataList" :header="checkList" :isToLine='true' :option="tableOption"  @getDataList="getDataList" v-loading="dataListLoading"  :operationNum="1"  :isShowOperate="true")
                  //- -- 插槽
                  el-table-column(slot="fixed"  fixed  prop=""  label="序号"  width='80' header-align="center" align="center")
                      template( slot-scope="scope")
                        span   {{scope.$index+(pageIndex - 1) * pageSize + 1}}

                  template(slot="result" slot-scope="scope")
                            font(v-if="scope.row.result === 1" color='#67C23A') 成功
                            font(v-if="scope.row.result === 2" color='#F56C6C') 失败
                            font(v-if="scope.row.result === 3" color='#F56C6C') 中止
                  template( slot='eventNumber' slot-scope="scope")
                        font(:title="scope.row.eventNumber") {{scope.row.eventNumber}}
                  template( slot='instruction' slot-scope="scope")
                        font(:title="scope.row.instruction") {{scope.row.instruction}}

                  //- 操作项
                  template.operation( slot-scope="scope")
                    el-button(size='mini' type='text'  @click="queryHandle(scope.row)" ) 详情
                    //- el-button(size='mini' type='text'  @click="queryHandle(scope.row)" v-if="isAuth('alarms:instruct:info')") 详情
        el-footer.box_footer
            el-pagination(@size-change="sizeChangeHandle" background @current-change="currentChangeHandle" :current-page="pageIndex" :page-sizes="[10, 20, 50, 100]" :page-size="pageSize"  :total="totalPage" layout="total, sizes, prev, pager, next, jumper")
            quert-detail(v-if="queryDetailVisible" ref="quertDetail" @refreshdatalist="getDataList")
</template>

<script>
import quertDetail from './query-detail'
import dragTable from '../../components/tab'

export default {
  name: 'sys-user',
  filters: {
    ellipsis (value) {
      if (!value) return ''
      if (value.length > 20) {
        return value.slice(0, 20) + '...'
      }
      return value
    }
  },
  computed: {
      currentHeight() {
      return this.$store.state.d2admin.currentHeight.height
      }
  },
  data () {
    return {
      data: [],
      isCollapse: true,
      dataForm: {},
      orgInfo: {},
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      queryDetailVisible: false,
      checkList: [], //筛选数据
      tableHeader: [
        { label: '报警编号', prop: 'eventNumber', slot: true, width: 260 },
        { label: '指令ID', prop: 'instruction', slot: true, width: 260 },
        { label: '资源点名称', prop: 'resourceName' },
        { label: '资源点类型', prop: 'resourceType' },
        { label: '资源点状态', prop: 'resourceStatus' },
        { label: '资源点动作', prop: 'actionName' },
        { label: '执行人', prop: 'createUser' },
        { label: '执行完成时间', prop: 'createTime' },
        { label: '执行结果', prop: 'result', slot: true }
      ],
      tableOption: {
        border: false, //是否边框
        maxHeight: 500 //高度
      }
    }
  },
  components: {
    quertDetail,
    dragTable
  },
  watch: {
    totalPage() { //注意这个函数的名字必须和你监听data中的属性的名字一样，这样才能当你data中的属性发生变化时，触发这个函数
      let pages = Math.ceil(this.totalPage / this.pageSize)//新数据总页数
      //总页数小于当前页数则重新加载列表数据
      if (pages < this.pageIndex) {
          this.pageIndex = pages || 1
          this.getDataList()//获取表格数据的方法
      }
    }
  },
  created () {
    this.checkList = [...this.tableHeader]
    this.getDataList()
  },
  methods: {
    // 获取数据列表
    getDataList (sort, order) {
      if (sort === 'resource_name') {
        sort = 'lr.name'
      }
      if (sort === 'resource_type') {
        sort = 'sd.name'
      }
      if (sort === 'resource_status') {
        sort = 'lr.status'
      }
      if (sort === 'create_time') {
        sort = 'aa.create_time'
      }
      if (sort === 'result') {
        sort = 'aa.result'
      }
      console.log(this.sort, this.order)
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrlAlarm('/alarmsLog/instructList'),
        method: 'get',
        params: {
          stationId: localStorage.getItem('stationId'),
          par: this.dataForm.par,
          startTime: this.dataForm.startTime,
          endTime: this.dataForm.endTime,
          page: this.pageIndex,
          size: this.pageSize,
          sort: sort,
          order: order
        }
      }).then(data => {
        if (data && data.code === 0) {
          // console.log(data)
          this.dataList = data.page.records
          this.totalPage = data.page.total
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    //排序
    sortChange (column) {
      if (column.order === 'descending') {
        this.order = 'desc'
      } else {
        this.order = 'asc'
      }
      if (column.column.columnKey) {
        this.sort = column.column.columnKey
      } else {
        this.sort = column.prop
      }
      this.getDataList()
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val
      this.getDataList()
    },
    indexMethod(index) {
        return (this.pageIndex - 1) * this.pageSize + index + 1
    },
    // 详情
    queryHandle (row) {
      this.queryDetailVisible = true
      this.$nextTick(() => {
        this.$refs.quertDetail.init(row)
      })
    },
    // 删除
    deleteHandle (id) {
      this.$confirm(`确认删除该指令?`, '删除指令', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        closeOnClickModal: false
      }).then(() => {
        this.$http({
          url: this.$http.adornUrlAlarm('/linkLinkageRule/delete'),
          method: 'post',
          data: this.$http.adornData(id, false)
        }).then(data => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      })
        .catch(() => {
        })
    }
  }
}
</script>
<style lang='scss' scoped>

</style>
