<template>
  <div>
    <el-card class="no-bottom">
      <el-form
        ref="searchForm"
        :model="searchForm"
        label-width="80px"
        inline
      >
        <el-form-item
          label="租户名称:"
          prop="tenantName"
        >
          <el-input v-model="searchForm.tenantName" />
        </el-form-item>
        <el-form-item
          label="状态:"
          prop="status"
        >
          <el-select v-model="searchForm.status" placeholder="请选择">
            <el-option label="全部" :value="null" />
            <el-option label="正常" :value="0" />
            <el-option label="锁定" :value="1" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="getTenantList(1)">查询</el-button>
          <el-button icon="el-icon-refresh" @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card class="margin-top-20">
      <div class="buttons-group" style="margin-bottom: 20px">
        <el-button v-permission="'MenuMgr_add'" type="primary" icon="el-icon-plus" @click="addRow">增加租户</el-button>
      </div>

      <el-table
        :data="tableData"
        border
      >
        <el-table-column
          type="index"
          label="序号"
          width="50px"
          align="center"
        />
        <el-table-column
          prop="tenantName"
          label="租户名称"
        />
        <el-table-column
          prop="tenantCode"
          label="租户代码"
        />
        <el-table-column
          prop="contact"
          label="联系人姓名"
        />
        <el-table-column
          prop="contactPhone"
          label="联系人电话"
        />
        <el-table-column
          prop="status"
          label="状态"
          :formatter="formatterStatus"
        />
        <el-table-column
          label="操作"
          width="140px"
          align="center"
        >
          <template slot-scope="props">
            <el-button v-permission="'SysMgr_update'" type="text" @click="editItem(props.row)">编辑</el-button>
            <el-popconfirm
              title="确定要删除此条记录吗？"
              @onConfirm="deleteItem(props.row.id)"
            >
              <el-button
                slot="reference"
                type="text"
                :disabled="!props.row.canEdited"
              >删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        style="margin-top: 20px;text-align: right"
        :current-page="pageForm.pageNum"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageForm.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageForm.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <el-dialog
      :title="add ? '增加租户' : '编辑租户'"
      :visible.sync="dialogVisible"
      width="700px"
      :before-close="beforeClose"
    >
      <el-form
        ref="addForm"
        label-width="100px"
        :model="addForm"
        :rules="addFormRules"
      >
        <el-form-item
          label="租户名称:"
          prop="tenantName"
        >
          <el-input v-model="addForm.tenantName" :disabled="!add" />
        </el-form-item>
        <el-form-item
          label="租户代码:"
          prop="tenantCode"
        >
          <el-input v-model="addForm.tenantCode" :disabled="!add" />
        </el-form-item>
        <el-form-item
          label="联系人姓名:"
          prop="contact"
        >
          <el-input v-model="addForm.contact" />
        </el-form-item>
        <el-form-item
          label="联系人电话:"
          prop="contactPhone"
        >
          <el-input v-model="addForm.contactPhone" />
        </el-form-item>
        <el-form-item
          label="状态:"
        >
          <el-radio-group v-model="addForm.status" :disabled="!add">
            <el-radio :label="0">正常</el-radio>
            <el-radio :label="1">锁定</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="saveSystem">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getTenantList, addTenant, updateTenant, deleteTenant } from '@/api/tenant-management/tenant-management'

export default {
  name: 'Index',
  data() {
    return {
      searchForm: {
        tenantName: null,
        status: null
      },
      add: true,
      dialogVisible: false,
      tableData: [],
      addForm: {
        tenantName: null,
        tenantCode: null,
        contact: null,
        contactPhone: null,
        status: 0
      },
      pageForm: {
        pageSize: 10,
        pageNum: 1,
        total: 0
      },
      currentId: null,
      addFormRules: {
        tenantName: [
          { required: true, message: '请输入租户名称', trigger: 'blur' },
          { min: 1, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
        ],
        tenantCode: [
          { required: true, message: '请输入租户代码', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
        ],
        contact: [
          { required: true, message: '请输入联系人姓名', trigger: 'blur' },
          { min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur' }
        ],
        contactPhone: [
          { required: true, message: '请输入联系人电话', trigger: 'blur' },
          { min: 7, max: 20, message: '长度在 7 到 20 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getTenantList(1)
  },
  methods: {
    formatterStatus(row, column, cellValue) {
      return (cellValue !== 1) ? '正常' : '锁定'
    },
    getTenantList(pageNum) {
      const params = {
        current: pageNum,
        size: this.pageForm.pageSize,
        tenantName: this.searchForm.tenantName,
        status: this.searchForm.status
      }
      getTenantList(params)
        .then(res => {
          const { data } = res
          this.tableData = data.records
          this.pageForm.total = data.total
        })
        .catch(err => {
          this.$message.error('操作失败')
          console.error(err.message)
        })
    },
    resetFields() {
      this.addForm = {
        tenantName: null,
        tenantCode: null,
        contact: null,
        contactPhone: null,
        status: 0
      }
      this.$refs.addForm.clearValidate()
    },
    beforeClose(done) {
      this.resetFields()
      this.$nextTick()
        .then(_ => {
          done()
        })
    },
    closeDialog() {
      this.resetFields()
      this.$nextTick()
        .then(_ => {
          this.dialogVisible = false
        })
    },
    handleSizeChange(val) {
      this.pageForm.pageSize = val
      this.getTenantList(this.pageForm.pageNum)
    },
    handleCurrentChange(val) {
      this.pageForm.pageNum = val
      this.getTenantList(val)
    },
    resetForm() {
      this.$refs.searchForm.resetFields()
    },
    addRow() {
      this.add = true
      this.dialogVisible = true
    },
    editItem(row) {
      const { id, tenantName, tenantCode, contact, contactPhone, status, canEdited } = row
      this.currentId = id
      this.addForm = {
        tenantName,
        tenantCode,
        contact,
        contactPhone,
        status,
        canEdited
      }
      this.add = false
      this.dialogVisible = true
    },
    deleteItem(id) {
      deleteTenant({ id })
        .then(res => {
          this.$message.success('操作成功')
          this.getTenantList(1)
        })
        .catch(err => {
          this.$message.error(err.message)
          console.error(err.message)
        })
    },
    saveSystem() {
      this.$refs.addForm.validate()
        .then(_ => {
          const params = this.addForm
          if (this.add) {
            addTenant(params)
              .then(res => {
                this.$message.success('操作成功')
                this.getTenantList(this.pageForm.pageNum)
              })
              .catch(err => {
                this.$message.error('操作失败')
                console.error(err.message)
              })
          } else {
            params.id = this.currentId
            updateTenant(params)
              .then(res => {
                this.$message.success('操作成功')
                this.getTenantList(this.pageForm.pageNum)
              })
              .catch(err => {
                this.$message.error('操作失败')
                console.error(err.message)
              })
          }
          this.closeDialog()
        })
        .catch(_ => {})
    }
  }
}
</script>

<style scoped>

</style>
