<template>
  <vab-query-form>
    <vab-query-form-left-panel>
      <el-form ref="queryForm" :inline="true" :model="queryForm">
        <el-form-item :label="$t('maintenance.sb')" prop="deviceId">
          <el-select v-model="queryForm.deviceId" placeholder="请选择设备">
            <el-option
              v-for="item in deviceData"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-button type="primary" @click="search">
          {{ $t('defalult.cx') }}
        </el-button>
        <el-button @click="resetForm()">{{ $t('defalult.chongz') }}</el-button>
      </el-form>
    </vab-query-form-left-panel>
    <vab-query-form-right-panel>
      <vab-query-form>
        <el-button type="primary" @click="handleAdd">
          {{ $t('trigger.tjcfq') }}
        </el-button>
      </vab-query-form>
    </vab-query-form-right-panel>
  </vab-query-form>
  <el-table
    v-loading="listLoading"
    :data="triggerData"
    stripe
    border
    style="width: 100%"
  >
    <el-table-column
      prop="id"
      :label="$t('trigger.xh')"
      width="80"
      align="center"
    ></el-table-column>
    <el-table-column
      prop="name"
      :label="$t('trigger.mc')"
      width="180"
      align="center"
    ></el-table-column>
    <el-table-column
      prop="triggerCondition"
      :label="$t('trigger.cftjlx')"
      width="280"
      align="center"
    ></el-table-column>
    <el-table-column
      prop="triggerConditionParams"
      :label="$t('trigger.cs')"
      width="180"
      align="center"
    ></el-table-column>
    <el-table-column
      fixed="right"
      :label="$t('defalult.cz')"
      width="160"
      align="center"
    >
      <template #default="{ row }">
        <el-button type="text" @click="details(row)">详情</el-button>
        <el-button type="text" @click="handleEdit(row)">
          {{ $t('defalult.bj') }}
        </el-button>
        <el-button type="text" @click="handleDelete(row)">
          {{ $t('defalult.sc') }}
        </el-button>
      </template>
    </el-table-column>
  </el-table>
  <vab-query-form>
    <vab-query-form-right-panel>
      <el-pagination
        :background="background"
        :current-page="queryForm.page"
        :layout="layout"
        :page-size="queryForm.size"
        :total="total"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
      ></el-pagination>
    </vab-query-form-right-panel>
  </vab-query-form>
  <trigger-edit ref="edit"></trigger-edit>
</template>

<script>
import {
  doDelete,
  getList,
  doEdit,
  selectDeviceByProjectId,
} from '@/api/trigger';
import triggerEdit from './triggerEdit';
export default {
  name: 'TriggerTable',
  components: {
    triggerEdit,
  },
  props: {
    projectId: {
      type: String,
      default: 'projectId',
    },
  },
  data() {
    return {
      groupData: [],
      lazy: true,
      layout: 'total, sizes, prev, pager, next, jumper',
      total: 0,
      background: true,
      listLoading: true,
      triggerData: [],
      deviceData: [],
      elementLoadingText: '正在加载...',
      queryForm: {
        page: 1,
        size: 20,
        projectId: '',
        deviceId: '',
      },
    };
  },
  computed: {
    height() {
      return 500;
    },
  },
  mounted() {
    this.queryForm.projectId = this.projectId;
    this.fetchData();
    this.getDeviceData();
  },
  methods: {
    async soreStatus(status, id) {
      const { code, msg } = await doEdit({ status: status, id: id });
      if (code == 0) {
        this.$notify({
          title: msg,
          type: 'success',
        });
      } else {
        this.$notify.error({
          title: msg,
        });
      }
      this.fetchData();
    },
    async fetchData() {
      this.listLoading = true;
      this.triggerData = [];
      const { data } = await getList(this.queryForm);
      this.triggerData = data.items;
      this.total = data.total;
      setTimeout(() => {
        this.listLoading = false;
      }, 500);
    },
    async getDeviceData() {
      const { data } = await selectDeviceByProjectId({
        projectId: this.queryForm.projectId,
      });
      this.deviceData = data;
    },
    search() {
      this.fetchData();
    },
    resetForm() {
      this.$refs.queryForm.resetFields();
    },
    handleAdd() {
      this.$refs['edit'].showEdit(this.projectId, 1);
    },
    handleEdit(row) {
      row.projectId = this.projectId;
      this.$refs['edit'].showEdit(row, 2);
    },
    details(row) {
      this.$router.push({
        path: '/triggerdetail',
        name: '触发器详情',
        query: { triggerId: row.id },
      });
    },
    handleQuery() {
      this.queryForm.page = 1;
      this.fetchData();
    },
    handleSizeChange(val) {
      this.queryForm.size = val;
      this.fetchData();
    },
    handleCurrentChange(val) {
      this.queryForm.page = val;
      this.fetchData();
    },
    handleDelete(row) {
      var that = this;
      if (row.id) {
        this.$confirm('你确定要删除当前项吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
          .then(() => {
            const { msg } = doDelete([row.id]);
            this.$message({
              type: 'success',
              message: msg == undefined ? '删除成功' : msg,
            });
            setTimeout(function () {
              that.fetchData();
            }, 1000);
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除',
            });
          });
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.el-tabs__item {
  background-color: rgba(50, 129, 253, 0.1) !important;
}
.manage-container {
  width: 100%;
  .vab-query-form {
    margin-bottom: 30px;
    .el-input {
      width: 180px;
    }
    .el-select {
      margin-right: 10px;
    }
  }
}
</style>
