<template>
  <div class="manage-container">
    <div class="manage-wrap">
      <div class="manage-input">
        <el-form ref="queryForm" :model="queryForm" label-width="100px">
          <vab-query-form>
            <el-form-item
              width="100"
              prop="sonserName"
              :label="$t('maintenance.sbmc')"
            >
              <el-input
                v-model="queryForm.sonserName"
                size="small"
                placeholder="请输入设备名称"
              ></el-input>
            </el-form-item>
            <el-form-item
              width="100"
              prop="deviceSerial"
              :label="$t('dataEnquiry.xlh')"
            >
              <el-input
                v-model="queryForm.deviceSerial"
                size="small"
                :placeholder="$t('dataEnquiry.qsrxlh')"
              ></el-input>
            </el-form-item>
            <el-form-item
              width="100"
              prop="status"
              :label="$t('dataEnquiry.zt')"
            >
              <el-select
                v-model="queryForm.status"
                size="small"
                placeholder="请选择状态"
              >
                <el-option
                  v-for="item in statusData"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </vab-query-form>
        </el-form>
      </div>
      <div class="manage-button">
        <el-button size="small" @click="resetForm()">
          {{ $t('defalult.chongz') }}
        </el-button>
        <el-button type="primary" size="small" @click="search">
          {{ $t('defalult.cx') }}
        </el-button>
      </div>
    </div>
    <el-table :data="data" border stripe style="width: 100%">
      <el-table-column
        prop="id"
        label="设备id"
        width="400"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="account"
        label="账户名"
        width="150"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="deviceSerial"
        label="设备序列号"
        width="150"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="deviceName"
        label="设备名字"
        width="200"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="iotDevice.name"
        label="所属站点"
        width="150"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="status"
        label="设备状态"
        width="100"
        align="center"
      >
        <template #default="{ row }">
          <el-tag
            v-if="row.status == 1"
            key="在线"
            effect="dark"
            type="success"
            size="mini"
          >
            已连接
          </el-tag>
          <el-tag
            v-if="row.status == 0"
            key="离线"
            effect="dark"
            type="danger"
            size="mini"
          >
            未连接
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="addTime"
        label="添加时间"
        width="180"
        align="center"
      ></el-table-column>
      <el-table-column
        fixed="right"
        :label="$t('defalult.cz')"
        width="180"
        align="center"
      >
        <template #default="{ row }">
          <el-button type="text" @click="bindDevice(row)">绑定站点</el-button>
          <el-button type="text" @click="videoData(row)">播放</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :background="background"
      :current-page="queryForm.page"
      :layout="layout"
      :page-size="queryForm.size"
      :total="total"
      style="text-align: right"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    ></el-pagination>
    <bind-device ref="bind"></bind-device>
  </div>
</template>
<script>
import { getDevices } from '@/api/fluorite';
import bindDevice from './bindDevice';
export default {
  name: 'VideoData',
  components: {
    bindDevice,
  },
  data() {
    return {
      deviceData: [],
      data: [],
      account: '',
      projectId: '',
      lazy: true,
      layout: 'total, sizes, prev, pager, next, jumper',
      total: 0,
      background: true,
      listLoading: true,
      elementLoadingText: '正在加载...',
      statusData: [
        {
          value: 3,
          label: 'all',
        },
        {
          value: 1,
          label: '在线',
        },
        {
          value: 0,
          label: '离线',
        },
      ],
      queryForm: {
        page: 1,
        size: 20,
        account: '',
        sonserName: '',
        status: 3,
        deviceSerial: '',
      },
    };
  },
  computed: {
    height() {
      return 500;
    },
  },
  created() {
    this.queryForm.account = this.$route.query.account;
    this.fetchData();
  },
  methods: {
    async fetchData() {
      this.listLoading = true;
      const { data } = await getDevices(this.queryForm);
      this.data = data.items;
      this.total = data.total;
      setTimeout(() => {
        this.listLoading = false;
      }, 500);
    },
    resetForm() {
      this.$refs.queryForm.resetFields();
    },
    search() {
      this.fetchData();
    },
    videoData(row) {
      this.$router.push({
        path: '/fluoriteSensorData',
        name: '监控视频',
        query: { account: row.account, deviceSerial: row.deviceSerial },
      });
    },
    bindDevice(row) {
      this.$refs['bind'].bindDevice({
        deviceSerial: row.deviceSerial,
        account: row.account,
      });
    },
    handleEdit(row) {
      row.account = this.account;
      this.$refs['edit'].showEdit(row, 1);
    },
    handleQuery() {
      this.queryForm.page = 1;
      this.fetchData();
    },
    handleSizeChange(val) {
      this.queryForm.size = val;
      this.fetchData();
    },
    handleCurrentChange(val) {
      this.queryForm.page = val;
      this.fetchData();
    },
  },
};
</script>

<style lang="scss" scoped>
.manage-wrap {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  .el-input {
    width: 200px !important;
  }
  .el-select {
    width: 200px !important;
  }
  .manage-button {
    padding-left: 12px;
    margin-bottom: 30px;
  }
}
</style>

<style lang="scss">
.el-dialog {
  .el-dialog__header {
    background-color: #1890ff !important;
    padding: 15px 20px;
    text-align: left !important;
    .el-dialog__title {
      color: #e8f4ff !important;
    }
    .el-dialog__headerbtn .el-dialog__close {
      color: #e8f4ff !important;
    }
  }
  .el-dialog__body {
    padding: 20px !important;
    .el-form-item {
      margin-bottom: 10px !important;
      .el-input {
        width: 100% !important;
      }
      .el-select {
        width: 100% !important;
      }
    }
    .el-form-item:last-child {
      margin-bottom: 20px !important;
    }
  }
}
</style>
