<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">

<!--          <a-col :span="6">-->
<!--            <a-form-item label="采集时间" >-->
<!--              <j-date placeholder="请选择采集时间" v-model="queryParam.time" :trigger-change="true"-->
<!--                      style="width: 100%" :showTime=true dateFormat="YYYY-MM-DD HH:mm:ss" />-->
<!--            </a-form-item>-->
<!--          </a-col>-->
<!--          <a-col :span="6">-->
<!--            <a-form-item label="创建时间" >-->
<!--              <j-date placeholder="请选择创建时间" v-model="queryParam.createTime" :trigger-change="true" style="width: 100%"-->
<!--                      :showTime=true date-format="YYYY-MM-DD HH:mm:ss"/>-->
<!--            </a-form-item>-->
<!--          </a-col>-->
          <a-col :span="5">
            <a-form-item label="设备名称">
              <j-select-device placeholder="请选择设备名称" :multiple="false" v-model="deviceName" @change="getSelect"/>
<!--              <j-search-select-tag placeholder="请选择设备名称" v-model="queryParam.deviceId" dict="bu_device,device_name,id" @change="getSelect"/>-->
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator" >
<!--      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>-->
      <a-button @click="handleTest1" type="primary" icon="plus">振动采集启动</a-button>
      <a-button @click="handleTest2" type="primary" icon="plus">振动采集停止</a-button>
      <a-button @click="handleTest3" type="primary" icon="plus">环境采集启动</a-button>
      <a-button @click="handleTest4" type="primary" icon="plus">环境采集停止</a-button>
<!--      <a-button type="primary" icon="download" @click="handleExportXls('传感器数据表')">导出</a-button>-->
<!--      <a-upload-->
<!--        name="file"-->
<!--        :showUploadList="false"-->
<!--        :multiple="false"-->
<!--        :headers="tokenHeader"-->
<!--        :action="importExcelUrl"-->
<!--        @change="handleImportExcel">-->
<!--          <a-button type="primary" icon="import">导入</a-button>-->
<!--      </a-upload>-->
      <!-- 高级查询区域 -->
<!--      <j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query>-->
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :scroll="{x:true}"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>
        <template slot="deviceFunState" slot-scope="text">
          <a-tag v-if="text === '已执行'" color="#87d068">已执行</a-tag>
          <a-tag v-else-if="text === '未执行'" color="#f50">未执行</a-tag>
          <span v-else>{{ text }} </span>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleReset(record)">故障复位</a>
<!--          <a @click="handleEdit(record)">编辑</a>-->
<!--          <a-divider type="vertical" />-->
<!--          <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">-->
<!--            <a>删除</a>-->
<!--          </a-popconfirm>-->
        </span>

      </a-table>
    </div>

    <buSensorData-modal ref="modalForm" @ok="modalFormOk" :mainId="mainId"></buSensorData-modal>
  </a-card>
</template>

<script>

  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import BuSensorDataModal from './modules/BuSensorDataModal'
  import JSuperQuery from '@/components/jeecg/JSuperQuery.vue'
  import JDate from '@comp/jeecg/JDate'
  import JSelectDevice from '@comp/jeecgbiz/JSelectDevice'
  import { postActions, getAction, controlhttpAction } from '@api/manage'


  export default {
    name: "BuSensorDataList",
    mixins:[JeecgListMixin],
    components: {
      JSelectDevice,
      BuSensorDataModal,
      JSuperQuery,
      JDate,
    },
    props:{
      mainId:{
        type:String,
        default:'',
        required:false
      }
    },
    data () {
      return {
        description: '传感器信息表管理页面',
        // disableMixinCreated:true,
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'设备名称',
            align:"center",
            dataIndex: 'deviceName',
          },
          {
            title:'传感器启动',
            align:"center",
            dataIndex: 'hjhx_dictText',
            scopedSlots: { customRender: 'deviceFunState' },
          },
          {
            title:'传感器停止',
            align:"center",
            dataIndex: 'hjt_dictText',
            scopedSlots: { customRender: 'deviceFunState' },
          },
          {
            title:'振动监测启动',
            align:"center",
            dataIndex: 'syq_dictText',
            scopedSlots: { customRender: 'deviceFunState' },
          },
          {
            title:'振动监测停止',
            align:"center",
            dataIndex: 'syt_dictText',
            scopedSlots: { customRender: 'deviceFunState' },
          },
          // {
          //   title:'采集时间',
          //   align:"center",
          //   dataIndex: 'collectTime'
          // },
          // {
          //   title:'采集间隔',
          //   align:"center",
          //   dataIndex: 'collectInterval'
          // },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' },
          }
        ],
        url: {
          list: "/sensor/buSensor/listBuSensorDataByMainId",
          delete: "/sensor/buSensor/deleteBuSensorData",
          deleteBatch: "/sensor/buSensor/deleteBatchBuSensorData",
          exportXlsUrl: "/sensor/buSensor/exportBuSensorData",
          importUrl: "/sensor/buSensor/importBuSensorData",
          deviceReset: "/device/buDevice/getDeviceReset",
        },
        dictOptions:{
          typeId:[],
          deviceId:[],
        },
        superFieldList:[],
        deviceName:'',
      }
    },
    created() {
    },
    computed: {
      importExcelUrl(){
        return `${window._CONFIG['domianURL']}/${this.url.importUrl}/${this.mainId}`;
      }
    },
    methods: {
      // clearList(){
      //   this.dataSource=[]
      //   this.selectedRowKeys=[]
      //   this.ipagination.current = 1
      // },
      loadData(arg) {
        if(!this.url.list){
          this.$message.error("请设置url.list属性!")
          return
        }
        //加载数据 若传入参数1则加载第一页的内容
        if (arg === 1) {
          this.ipagination.current = 1;
        }
        var params = this.getQueryParams();//查询条件
        this.loading = true;
        getAction(this.url.list, params).then((res) => {
          if (res.success) {
            this.dataSource = res.result.records||res.result;
            if(res.result.total)
            {
              this.ipagination.total = res.result.total;
            }
          }
          if(res.code===510){
            this.$message.warning(res.message)
          }
          this.loading = false;
        })
      },
      handleTest2(){
        let url = '/mqtt/syqt?deviceId=' + this.deviceName;
        controlhttpAction(url,{},'get').then((res)=>{
          if(res){
            this.$message.success("操作成功!");
          }else{
            this.$message.warning(res.msg);
          }
        }).finally(() => {
        })
      },
      handleTest1(){
        let url = '/mqtt/syq?deviceId=' + this.deviceName;
        controlhttpAction(url,{},'get').then((res)=>{
          if(res){
            this.$message.success("操作成功!");
          }else{
            this.$message.warning(res.msg);
          }
        }).finally(() => {
        })
      },
      handleTest3(){
        let url = '/mqtt/hxml?deviceId=' + this.deviceName;
        controlhttpAction(url,{},'get').then((res)=>{
          if(res){
            this.$message.success("操作成功!");
          }else{
            this.$message.warning(res.msg);
          }
        }).finally(() => {
        })
      },
      handleTest4(){
        let url = '/mqtt/hxtz?deviceId=' + this.deviceName;
        controlhttpAction(url,{},'get').then((res)=>{
          if(res){
            this.$message.success("操作成功!");
          }else{
            this.$message.warning(res.msg);
          }
        }).finally(() => {
        })
      },
      getSelect(data){
        this.deviceName = data;
      },
      handleReset(record){
        let params = {name : record.deviceName};
        getAction(this.url.deviceReset, params).then((res) => {
          if(res){
            this.$message.success("操作成功!");
          }else{
            this.$message.warning(res.msg);
          }
        })
      },

    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>
