<template>
  <app-container>
    <search-area
      size="small"
      @search="fetchData"
    >
      <!--一般查询条件-->
      <search-item>
        <el-input
          v-model="listQuery.tenantName"
          size="small"
          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="makeDep(scope.row)">
              关联组织
            </el-button>
          </template>
        </el-table-column>
      </template>
    </normal-table>
    <edit-tenant ref="editTenant" @watchChild="fetchData" />
    <relate-dept ref="relateDept" @watchChild="fetchData" />
  </app-container>
</template>

<script>
import EditTenant from '@/views/system/tenant/components/editTenant'
import { getTenantList, delTenantSys } from '@/api/system/tenant'
import RelateDept from '@/views/system/tenant/components/relateDept'

export default {
  name: 'TenantList',
  components: { RelateDept, EditTenant },
  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: 'deptName',
          align: 'center'
        }
      ], // 显示列
      listLoading: false,
      deviceModelList: [], // 设备类型列表
      list: [], // 表格数据列表
      // 是否需要序号列
      options: {
        needIndex: false
      }
    }
  },
  created() {
    this.fetchData()
  },
  mounted() {
    this.$refs.normalTable.initColumnsState(false)
  },
  methods: {
    // 获取数据列表
    fetchData() {
      this.listLoading = true
      getTenantList(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()
          }
        })
      })
    },
    // 新增
    add() {
      this.$refs.editTenant.initDialog('create')
    },
    // 修改
    edit(row) {
      this.$refs.editTenant.initDialog('update', row)
    },
    // 绑定组织
    makeDep(row) {
      this.$refs.relateDept.initDialog(row)
    }
  }
}
</script>
