<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增租户</el-button>
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="list">
      <el-table-column label="租户ID" prop="tenantId" width="80" />
      <el-table-column label="租户名称" prop="tenantName" />
      <el-table-column label="联系人" prop="contact" width="100" />
      <el-table-column label="手机" prop="phone" width="130" />
      <el-table-column label="邮箱" prop="email" />
      <el-table-column label="状态" width="80">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status==='0' ? 'success' : 'danger'">{{ scope.row.status==='0' ? '正常' : '停用' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" prop="createTime" width="160" />
      <el-table-column label="操作" class-name="col-ops" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="租户名称" prop="tenantName"><el-input v-model="form.tenantName" /></el-form-item>
        <el-row>
          <el-col :span="12"><el-form-item label="联系人" prop="contact"><el-input v-model="form.contact" /></el-form-item></el-col>
          <el-col :span="12"><el-form-item label="手机" prop="phone"><el-input v-model="form.phone" /></el-form-item></el-col>
        </el-row>
        <el-form-item label="邮箱" prop="email"><el-input v-model="form.email" /></el-form-item>
        <el-form-item label="备注" prop="remark"><el-input v-model="form.remark" type="textarea" rows="2" /></el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="open=false">取消</el-button>
        <el-button type="primary" @click="submitForm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { listTenant, getTenant, addTenant, updateTenant, delTenant } from "@/api/bid/tenant";
export default {
  name: "Tenant",
  data() {
    return {
      loading: false, total: 0, list: [], open: false, title: "",
      queryParams: { pageNum: 1, pageSize: 10 },
      form: {},
      rules: { tenantName: [{ required: true, message: "租户名称不能为空", trigger: "blur" }] }
    };
  },
  created() { this.getList(); },
  methods: {
    getList() {
      this.loading = true;
      listTenant(this.queryParams).then(r => { this.list = r.rows; this.total = r.total; this.loading = false; });
    },
    handleAdd() { this.form = { status: "0" }; this.open = true; this.title = "新增租户"; },
    handleUpdate(row) { getTenant(row.tenantId).then(r => { this.form = r.data; this.open = true; this.title = "修改租户"; }); },
    handleDelete(row) {
      this.$modal.confirm("确认删除？").then(() => delTenant(row.tenantId)).then(() => { this.getList(); this.$modal.msgSuccess("删除成功"); });
    },
    submitForm() {
      this.$refs["form"].validate(v => {
        if (!v) return;
        const action = this.form.tenantId ? updateTenant : addTenant;
        action(this.form).then(() => { this.$modal.msgSuccess("操作成功"); this.open = false; this.getList(); });
      });
    }
  }
};
</script>
