<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="租户编号" prop="tradeNo">
        <el-input
            v-model="queryParams.tradeNo"
            placeholder="请输入订单编号"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系人" prop="tradeNo">
        <el-input
            v-model="queryParams.tradeNo"
            placeholder="请输入联系人"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="tradeNo">
        <el-input
            v-model="queryParams.tradeNo"
            placeholder="请输入联系电话"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="企业名称" prop="tradeNo">
        <el-input
            v-model="queryParams.tradeNo"
            placeholder="请输入企业名称"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['xq:banner:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['xq:banner:remove']"
        >删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="tenantList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" v-if="false" />
      <el-table-column label="平台编号" align="center" prop="tenantId" />
      <el-table-column label="用户前缀" align="center" prop="prefix" />
      <el-table-column label="联系人" align="center" prop="contactUserName" />
      <el-table-column label="联系电话" align="center" prop="contactPhone" />
      <el-table-column label="企业名称" align="center" prop="companyName" />
<!--      <el-table-column label="社会信用代码" align="center" prop="licenseNumber" />-->
<!--      <el-table-column label="过期时间" align="center" prop="expireTime" width="180" />-->
      <el-table-column label="租户状态" align="center" prop="status">
        <template v-slot="scope">
          <el-switch v-model="scope.row.status" active-value="0" inactive-value="1" @change="handleStatusChange(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" min-width="100">
        <template v-slot="scope">
          <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['xq:banner:edit']"
          >修改
          </el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-refresh"
              @click="handleRefresh(scope.row)"
              v-hasPermi="['xq:banner:edit']"
          >同步
          </el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['xq:banner:remove']"
          >删除
          </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"
    />

    <add-or-update-tenant v-if="addOrUpdateTenantVisible" ref="addOrUpdateTenant" @refreshDataList="getList"/>
  </div>
</template>
<script>
import { changeTenantStatus, delTenant, listTenant, syncTenantPackage } from '@/api/system/tenant'
import AddOrUpdateTenant from '@/views/system/tenant/add-or-update-tenant.vue'

export default {
  name: 'index',
  components: {AddOrUpdateTenant},
  data() {
    return {
      addOrUpdateTenantVisible: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 借款状态表格数据
      tenantList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        usedRemit: undefined,
        borrowName: undefined,
        borrowRemark: undefined,
      },
    }
  },
  created() {
    this.getList();
  },
  methods: {
    handleStatusChange(row){
      let text = row.status === 0 ? '启用' : '停用'
      this.$confirm('确认要' + text + '[' + row.companyName + ']租户吗?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return changeTenantStatus(row.id, row.tenantId, row.status);
      }).then(() => {
        this.$modal.msgSuccess(text + '成功')
      }).catch(function() {
        row.openVideoStatus = row.openVideoStatus === 1 ? 0 : 1
      })
    },
    getList() {
      this.loading = true;
      listTenant(this.queryParams).then(response => {
        this.tenantList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    handleUpdate(row){
      this.addOrUpdateTenantVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdateTenant.init(row.id)
      })
    },
    handleAdd(){
      this.addOrUpdateTenantVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdateTenant.init()
      })
    },
    handleRefresh(row){
      this.$modal.confirm('是否确定同步【"' + row.tenantId + '"】的租户套餐项？').then(() => {
        this.loading = true;
        return syncTenantPackage(row.tenantId, row.packageId);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("同步成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    handleDelete(row) {
      this.$modal.confirm('是否确认删除平台【"' + row.tenantId + '"】的数据项？').then(() => {
        this.loading = true;
        return delTenant(row.id);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
  }
}
</script>
<style scoped lang="scss">

</style>

