<template>
  <app-container>
    <search-area
      class="searchBottom"
      :need-clear="false"
      :need-search-more="false"
      type="seperate"
      size="small"
      search-more-type="default"
      @search="fetchData"
    >
      <!--一般查询条件-->
      <search-item>
        <el-input
          v-model="listQuery.tenantName"
          placeholder="项目名称"
          clearable
        />
      </search-item>
    </search-area>
    <normal-table
      ref="normalTable"
      :data="list"
      :query="listQuery"
      :columns="columns"
      :list-loading="listLoading"
      :options="options"
      size="small"
    >
      <template slot="btns">
        <el-button size="small" @click="add">
          新增
        </el-button>
      </template>
      <template slot="columns">
        <el-table-column label="操作" width="140" align="center">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="edit(scope.row)">
              修改
            </el-button>
            <el-button type="text" size="small" @click="del(scope.row)">
              删除
            </el-button>
            <el-button type="text" size="small" @click="detail(scope.row)">
              详情
            </el-button>
            <el-button type="text" size="small" @click="makeDep(scope.row)">
              组织
            </el-button>
          </template>
        </el-table-column>
      </template>
    </normal-table>
    <edit-alarm-content ref="editAlarmContent" @watchChild="fetchData" />
  </app-container>
</template>

<script>
import editAlarmContent from '@/views/system/tenant/components/editTenant'
import { tenantSysList, delTenantSys } from '@/api/system/tenant'

export default {
  name: 'TenantList',
  components: { editAlarmContent },
  data() {
    return {
      // 查询条件
      listQuery: {
        tenantName: '',
        offset: 1,
        sort: 'id',
        order: 'asc'
      },
      columns: [
        {
          text: '项目编码',
          value: 'tenantCode',
          align: 'center'
        },
        {
          text: '项目名称',
          value: 'tenantName',
          align: 'center'
        },
        {
          text: '项目联系人',
          value: 'linkPerson',
          align: 'center'
        },
        {
          text: '联系人电话',
          value: 'tel',
          align: 'center'
        },
        {
          text: '历史数据保存天数',
          value: 'clearDay',
          align: 'center'
        },
        {
          text: '关联组织',
          value: 'deptId',
          align: 'center'
        },
        {
          text: '关联组织名称',
          value: 'deptName',
          align: 'center'
        }
      ], // 显示列
      listLoading: false,
      deviceModelList: [], // 设备类型列表
      list: [], // 表格数据列表
      // 是否需要序号列
      options: {
        needIndex: false
      }
    }
  },
  created() {
    this.fetchData()
  },
  mounted() {
    this.$refs.normalTable.initColumnsState(false)
  },
  methods: {
    // 获取数据列表
    fetchData() {
      this.listLoading = true
      tenantSysList(this.listQuery).then(response => {
        this.list = response.data
        this.listLoading = false
      })
    },
    del(row) {
      this.$confirm(
        '确定要删除所选设备吗？',
        '确认操作',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        delTenantSys(row.id).then(response => {
          if (response.code === 200) {
            this.$message.success('删除成功')
            this.fetchData()
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 新增
    add() {
      this.$refs.editAlarmContent.initDialog('create')
    },
    // 修改
    edit(row) {
      this.$refs.editAlarmContent.initDialog('update', row)
    },
    // 详情
    detail(row) {
      this.$refs.editAlarmContent.initDialog('detail', row)
    },
    // 绑定组织
    makeDep(row) {
      this.$refs.editAlarmContent.initDialog('makeDep', row)
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .searchBottom{
    border-bottom: 12px solid #ebebeb;
  }
</style>
