<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="name"
              :label="$t('dataEnquiry.zdmc')"
            >
              <el-input
                v-model="queryForm.name"
                size="small"
                :placeholder="$t('dataEnquiry.qsrzdmc')"
              ></el-input>
            </el-form-item>
            <el-form-item
              width="100"
              prop="code"
              :label="$t('dataEnquiry.xlh')"
            >
              <el-input
                v-model="queryForm.code"
                size="small"
                :placeholder="$t('dataEnquiry.qsrxlh')"
              ></el-input>
            </el-form-item>
            <el-form-item
              width="100"
              prop="community"
              :label="$t('dataEnquiry.sq')"
            >
              <el-input
                v-model="queryForm.community"
                size="small"
                :placeholder="$t('dataEnquiry.qsrsq')"
              ></el-input>
            </el-form-item>
          </vab-query-form>
        </el-form>
      </div>
      <div class="manage-input">
        <el-form ref="queryForm" :model="queryForm" label-width="100px">
          <vab-query-form>
            <el-form-item
              width="100"
              prop="commPro"
              :label="$t('dataEnquiry.txxy')"
            >
              <el-select
                v-model="queryForm.commPro"
                size="small"
                :placeholder="$t('dataEnquiry.qxztxxy')"
              >
                <el-option
                  v-for="item in dictData"
                  :key="item.dataCode"
                  :label="item.dataValue"
                  :value="item.dataCode"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item
              width="100"
              prop="townCode"
              :label="$t('dataEnquiry.jd')"
            >
              <el-select
                v-model="queryForm.townCode"
                size="small"
                :placeholder="$t('dataEnquiry.qxzjd')"
              >
                <el-option
                  v-for="item in townData"
                  :key="item.townCode"
                  :label="item.townName"
                  :value="item.townCode"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item
              width="100"
              prop="status"
              :label="$t('dataEnquiry.zt')"
            >
              <el-select
                v-model="queryForm.status"
                size="small"
                :placeholder="$t('dataEnquiry.qxzzt')"
              >
                <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>
        <el-button type="primary" size="small" @click="exportData">
          {{ $t('defalult.dc') }}
        </el-button>
      </div>
    </div>
    <el-table
      id="table"
      v-loading="listLoading"
      :data="deviceData"
      border
      stripe
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column
        prop="id"
        label="ID"
        width="60"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="name"
        :label="$t('monitorVideo.sbmc')"
        width="160"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="code"
        :label="$t('dataEnquiry.zdbm')"
        width="200"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="commPro.dataValue"
        :label="$t('dataEnquiry.txxy')"
        width="200"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="status"
        :label="$t('dataEnquiry.ljzt')"
        width="120"
        align="center"
      >
        <template #default="{ row }">
          <el-tag
            v-if="row.status == '已连接'"
            key="已连接"
            effect="dark"
            type="success"
            size="mini"
          >
            {{ $t('dataEnquiry.ylj') }}
          </el-tag>
          <el-tag
            v-if="row.status == '未连接'"
            key="未连接"
            effect="dark"
            type="danger"
            size="mini"
          >
            {{ $t('dataEnquiry.wlj') }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="enableStatus"
        :label="$t('site.qyzt')"
        width="120"
        align="center"
      >
        <template #default="{ row }">
          <el-tag
            v-if="row.enableStatus == 1"
            key="启用"
            effect="dark"
            type="success"
            size="mini"
          >
            {{ $t('site.qy') }}
          </el-tag>
          <el-tag
            v-if="row.enableStatus == 0"
            key="未启用"
            effect="dark"
            type="danger"
            size="mini"
          >
            {{ $t('site.wqy') }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="offlineDelay"
        :label="$t('dataEnquiry.lxyzm')"
        width="100"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="commPro.dataValue"
        :label="$t('dataEnquiry.txxy')"
        width="100"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="project.name"
        :label="$t('dataEnquiry.xm')"
        width="220"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="longitude"
        :label="$t('dataEnquiry.jd')"
        width="200"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="latitude"
        :label="$t('dataEnquiry.wd')"
        width="200"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="offlineDelay"
        :label="$t('dataEnquiry.lxys')"
        width="100"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="townName"
        :label="$t('dataEnquiry.xzjdmc')"
        width="150"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="community"
        :label="$t('dataEnquiry.sq')"
        width="160"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="specifications"
        :label="$t('dataEnquiry.sbgg')"
        width="100"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="specifications"
        :label="$t('dataEnquiry.sbggd')"
        width="150"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="simNum"
        :label="$t('dataEnquiry.skjjhm')"
        width="150"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="technology.dataValue"
        :label="$t('dataEnquiry.sbgy')"
        width="100"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="material.dataValue"
        :label="$t('dataEnquiry.sbcz')"
        width="100"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="outStandard.dataValue"
        :label="$t('dataEnquiry.csbz')"
        width="100"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="leader"
        :label="$t('dataEnquiry.ywry')"
        width="100"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="contact"
        :label="$t('dataEnquiry.lxfs')"
        width="150"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="type.dataValue"
        :label="$t('dataEnquiry.sblx')"
        width="100"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="userNum"
        :label="$t('dataEnquiry.fwyhs')"
        width="100"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="onlineTime"
        :label="$t('dataEnquiry.sxsj')"
        width="200"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="offlineTime"
        :label="$t('dataEnquiry.lxsj')"
        width="200"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="offlineDuration"
        :label="$t('dataEnquiry.lxsc')"
        width="150"
        align="center"
      ></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>
    <!--    <device-edit ref="edit"></device-edit>-->
    <!--    <copy-device ref="copy"></copy-device>-->
  </div>
</template>
<script>
import {
  doDelete,
  getList,
  getTownsByProject,
  getDictData,
  exportDevice,
} from '@/api/device';
import ajax from '@/api/download';
// import deviceEdit from './deviceEdit';
// import CopyDevice from './copyDevice';
export default {
  name: 'Index',
  components: {
    // deviceEdit,
    // CopyDevice,
  },
  data() {
    return {
      deviceData: [],
      lazy: true,
      activeName: 'first',
      layout: 'total, sizes, prev, pager, next, jumper',
      total: 0,
      input: '',
      textarea: '',
      list: [],
      townData: [],
      dictData: [],
      background: true,
      listLoading: true,
      ids: [],
      elementLoadingText: '正在加载...',
      statusData: [
        {
          label: 'all',
          value: 3,
        },
        {
          label: '离线',
          value: 0,
        },
        {
          label: '在线',
          value: 1,
        },
      ],
      queryForm: {
        page: 1,
        size: 20,
        name: '',
        projectId: '',
        code: '',
        community: '',
        townCode: '',
        commPro: '',
        status: 3,
      },
    };
  },
  computed: {
    height() {
      return 500;
    },
  },
  created() {
    this.queryForm.projectId = sessionStorage.getItem('projectId');
    this.fetchData();
    this.getTownData();
    this.getDictData();
  },
  methods: {
    async fetchData() {
      this.listLoading = true;
      const { data } = await getList(this.queryForm);
      this.deviceData = data.items;
      this.total = data.total;
      setTimeout(() => {
        this.listLoading = false;
      }, 500);
    },
    //乡镇街道
    async getTownData() {
      const { data } = await getTownsByProject(this.queryForm);
      this.townData = data;
    },
    //字典数据
    async getDictData() {
      const { data } = await getDictData(this.queryForm);
      this.dictData = data.communicationProtocol;
    },
    resetForm() {
      this.$refs.queryForm.resetFields();
    },
    search() {
      this.fetchData();
    },
    //导出
    async exportData() {
      const url = await exportDevice(this.queryForm);
      ajax.downloadFile(url, { fileName: '站点列表.xls' });
    },
    sensorEdit(row) {
      this.$router.push({
        name: '传感器管理',
        path: '/sensor',
        query: { deviceId: row.id },
      });
    },
    sensorData(row) {
      this.$router.push({
        name: '站点最新数据',
        path: '/sensorIotData',
        query: { deviceCode: row.code, deviceId: row.id },
      });
    },
    handleAdd() {
      this.$refs['edit'].showEdit({ projectId: this.queryForm.projectId }, 1);
    },
    handleEdit(row) {
      this.$refs['edit'].showEdit(row, 2);
    },
    copyDevice(row) {
      this.$refs['copy'].showCopy(row);
    },
    handleQuery() {
      this.queryForm.page = 1;
      this.fetchData();
    },
    handleSizeChange(val) {
      this.queryForm.size = val;
      this.fetchData();
    },
    handleCurrentChange(val) {
      this.queryForm.page = val;
      this.fetchData();
    },
    deletes() {
      const ids = this.ids;
      if (ids.length == 0) {
        this.$message({
          type: 'info',
          message: '没有选中任何项',
        });
        return false;
      }
      this.deleteData(ids);
    },
    handleSelectionChange(val) {
      const ids = [];
      val.forEach(row => {
        ids.push(row.id);
      });
      this.ids = ids;
      console.log(this.ids);
    },
    deleteData(data) {
      this.$confirm('你确定要删除当前项吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          const { msg } = doDelete(data);
          this.$message({
            type: 'success',
            message: msg == undefined ? '删除成功' : msg,
          });
          const that = this;
          setTimeout(function () {
            that.fetchData();
          }, 1000);
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除',
          });
        });
    },
    handleDelete(row) {
      if (row.id) {
        this.deleteData([row.id]);
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.el-input {
  width: 200px !important;
}
.el-select {
  width: 200px !important;
}
.manage-button {
  padding-left: 12px;
  margin-bottom: 30px;
}
</style>
