<template>
  <el-dialog title="厂别信息" v-model="visible" width="600px" append-to-body draggable>
    <div class="form-container">
      <el-form ref="tenantRef" class="master-container" :model="form" :rules="rules" label-width="140px">
        <el-form-item label="logo：" prop="logo">
          <image-upload :action="webHost + '/common/upload'" :limit="1" :isShowTip="false" v-model="form.logo" />

        </el-form-item>
        <el-form-item label="企业全称：" prop="orgName">
          <el-input v-model.trim="form.orgName" placeholder="请输入企业全称" />
        </el-form-item>
        <el-form-item label="厂别编码：" prop="code">
          <el-input v-model.trim="form.code" placeholder="请输入厂别编码" />
        </el-form-item>
        <!-- <el-form-item label="统一社会信用编码：" prop="orgCrtCode">
          <el-input v-model.trim="form.orgCrtCode" placeholder="请输入统一社会信用编码" />
        </el-form-item>
        <el-form-item label="详细地址：" prop="orgAddr">
          <el-input v-model.trim="form.orgAddr" placeholder="选择省市区" />
        </el-form-item>
        <el-form-item label="联系人：" prop="contactName">
          <el-input v-model.trim="form.contactName" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="联系电话：" prop="contactPhone">
          <el-input v-model.trim="form.contactPhone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="电子邮箱：" prop="contactEmail">
          <el-input v-model.trim="form.contactEmail" placeholder="请输入电子邮箱" />
        </el-form-item> -->
      </el-form>
    </div>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" icon="Check" @click="handleSave">确认</el-button>
        <el-button icon="Close" @click="handleCancel">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script setup>
import { getTenant, saveTenant } from '@/api/business/tenant'
const { proxy } = getCurrentInstance()
const emit = defineEmits(['handleSaveSuccess'])
/** 表单抽屉 页变量 */
const loading = ref(false)
const visible = ref(false)
const webHost = import.meta.env.VITE_APP_PRODUCTION_API

const data = reactive({
  form: {},
  rules: {
    orgName: [{ required: true, message: '企业全称不能为空', trigger: 'blur' }]
  }
})
const { form, rules } = toRefs(data)
/***********************  方法区  ****************************/
/** 打开抽屉 */
function open(id) {
  reset()
  visible.value = true
  if (id) {
    loading.value = true
    getTenant(id).then((res) => {
      if (res.data) {
        form.value = res.data
      }
      loading.value = false
    })
  }
}

/** 取消按钮 */
function handleCancel() {
  visible.value = false
  reset()
}

/** 表单重置 */
function reset() {
  form.value = {}
  proxy.resetForm('tenantRef')
}

/** 提交按钮 */
function handleSave() {
  proxy.$refs['tenantRef'].validate((valid) => {
    if (valid) {
      saveTenant(form.value).then((res) => {
        if (res.code == 200) {
          proxy.$modal.msgSuccess('操作成功')
          emit('handleSaveSuccess')
        }
        visible.value = false
      })
    }
  })
}

/** 暴露给父组件的方法 */
defineExpose({
  open
})
</script>
