<!-- 租户编辑弹窗 -->
<template>
  <a-modal
    :width="650"
    :visible="visible"
    :confirm-loading="loading"
    :title="isUpdate?'修改租户':'添加租户'"
    :body-style="{paddingBottom: '8px'}"
    @update:visible="updateVisible"
    @ok="save">
    <a-form
      ref="form"
      :model="form"
      :rules="rules"
      :label-col="{md: {span: 6}, sm: {span: 24}}"
      :wrapper-col="{md: {span: 19}, sm: {span: 24}}">
      <!-- <a-form-item
        label="租户头像:"
        name="logo"
        :label-col="{sm: {span: 3}, xs: {span: 6}}"
        :wrapper-col="{sm: {span: 21}, xs: {span: 18}}">
        <uploadImage :limit="1" :updDir="updDir" v-model:value="form.logo"/>
      </a-form-item> -->
      <a-form-item label="租户名称:" name="name">
        <a-input
          allow-clear
          :maxlength="150"
          placeholder="请输入租户名称"
          v-model:value="form.name"/>
      </a-form-item>
      <a-form-item label="租户电话:" name="phone">
        <a-input
          allow-clear
          :maxlength="50"
          placeholder="请输入租户电话"
          v-model:value="form.phone"/>
      </a-form-item>
      <!-- <a-form-item label="租户地址:" name="address">
        <a-input
          allow-clear
          :maxlength="150"
          placeholder="请输入租户地址"
          v-model:value="form.address"/>
      </a-form-item> -->
      <!-- <a-form-item label="排序号:" name="sort">
        <a-input-number
          :min="0"
          class="ele-fluid"
          placeholder="请输入排序号"
          v-model:value="form.sort"/>
      </a-form-item> -->
      <a-form-item label="用户账号:" name="username">
        <a-input
          allow-clear
          :maxlength="20"
          placeholder="请输入用户账号"
          v-model:value="form.username"/>
      </a-form-item>
      <!-- <a-form-item label="租户编码:" name="code">
        <a-input
          allow-clear
          :maxlength="30"
          placeholder="请输入租户编码"
          v-model:value="form.code"/>
      </a-form-item> -->
      <!-- <a-form-item label="租户邮箱:" name="email">
        <a-input
          allow-clear
          :maxlength="50"
          placeholder="请输入租户邮箱"
          v-model:value="form.email"/>
      </a-form-item> -->
      <a-form-item
        label="租户类型："
        name="type">
        <a-select
          v-model:value="form.type"
          placeholder="请选择租户类型"
          allow-clear>
          <a-select-option :value="1">政府</a-select-option>
          <a-select-option :value="2">企业</a-select-option>
          <a-select-option :value="3">组织</a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label="租户状态" name="status">
        <a-radio-group
          v-model:value="form.status">
          <a-radio :value="1">已启用</a-radio>
          <a-radio :value="2">未启用</a-radio>
        </a-radio-group>
      </a-form-item>
      <a-form-item
        label="登录密码:"
        name="password">
        <a-input-password
          :maxlength="20"
          placeholder="请输入登录密码"
          v-model:value="form.password"/>
      </a-form-item>
      <!-- <a-form-item
        label="备注:"
        :label-col="{sm: {span: 3}, xs: {span: 6}}"
        :wrapper-col="{sm: {span: 21}, xs: {span: 18}}">
        <a-textarea
          :rows="3"
          :maxlength="255"
          placeholder="请输入备注"
          v-model:value="form.note"/>
      </a-form-item> -->
    </a-form>
  </a-modal>
</template>

<script>
import uploadImage from '@/components/uploadImage'

export default {
  name: 'TenantEdit',
  emits: ['done', 'update:visible'],
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  components: {uploadImage},
  data() {
    return {
      // 表单数据
      form: Object.assign({}, this.data),
      // 表单验证规则
      rules: {
        name: [
          {required: true, message: '请输入租户名称', type: 'string', trigger: 'blur'}
        ],
        // code: [
        //   {required: true, message: '请输入租户编码', type: 'string', trigger: 'blur'}
        // ],
        type: [
          {required: true, message: '请选择租户类型', type: 'number', trigger: 'blur'}
        ],
        status: [
          {required: true, message: '请选择是否默认', type: 'number', trigger: 'blur'}
        ],
        // sort: [
        //   {required: true, message: '请输入排序号', type: 'number', trigger: 'blur'}
        // ],
        username: [
          {required: true, message: '请输入登录账号', type: 'string', trigger: 'blur'}
        ],
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
      // 图片上传目录
      updDir: 'tenant',
    };
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({}, this.data);
        this.isUpdate = true;
      } else {
        this.form = {status: 1};
        this.isUpdate = false;
      }
      if (this.$refs.form) {
        this.$refs.form.clearValidate();
      }
    }
  },
  methods: {
    /* 保存编辑 */
    save() {
      this.$refs.form.validate().then(() => {
        this.loading = true;
        this.$http[this.isUpdate ? 'put' : 'post'](this.isUpdate ? '/tenant/edit' : '/tenant/add', this.form).then(res => {
          this.loading = false;
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            if (!this.isUpdate) {
              this.form = {};
            }
            this.updateVisible(false);
            this.$emit('done');
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          this.loading = false;
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    }
  }
}
</script>

<style scoped>
</style>
