<template>
  <div class="index">
    <el-form ref="ruleFormRef" :model="form" :rules="rules" label-width="auto" class="form" label-position="left">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="名称" prop="name">
            <el-input size="large" clearable v-model="form.name" placeholder="请输入名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="负责人姓名" prop="person">
            <el-input size="large" clearable v-model="form.person" placeholder="请输入负责人姓名" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="负责人电话" prop="person_phone">
            <el-input size="large" clearable v-model="form.person_phone" placeholder="请输入负责人电话" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否公开" prop="is_show">
            <el-radio-group size="large" v-model="form.is_show">
              <el-radio v-for="i in isUseList" :key="i._id" :label="i.value">{{ i.label }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-col :span="24">
        <el-form-item label="地址" prop="address">
          <el-input size="large" v-model="form.address" :autosize="{ minRows: 2, maxRows: 4 }" type="textarea" placeholder="请输入地址" />
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-form-item label="简介" prop="brief">
          <el-input size="large" v-model="form.brief" :autosize="{ minRows: 2, maxRows: 4 }" type="textarea" placeholder="请输入简介" />
        </el-form-item>
      </el-col>
      <el-col :span="24" class="button">
        <el-button type="primary" @click="submitForm(ruleFormRef)">保存</el-button>
      </el-col>
    </el-form>
  </div>
</template>
<script setup>
// 基础
const $checkRes = inject('$checkRes')
const cloneDeep = inject('cloneDeep')
// 用户信息
import { UserStore } from '@/store/user'
const userStore = UserStore()
const user = computed(() => userStore.user)
// 表单
const ruleFormRef = inject('ruleFormRef')
// 字典表
const isUseList = inject('isUseList')
// 接口
import { UnitStore } from '@/store/api/user/unit'
const unitStore = UnitStore()
const form = ref({})
const rules = reactive({
  // nick_name: [{ required: true, message: '请输入昵称', trigger: 'blur' }],
  // gender: [{ required: true, message: '请选择性别', trigger: 'blur' }],
  // phone: [{ required: true, validator: validatePhoneNumber, trigger: 'blur' }],
  // account: [{ required: true, message: '请输入账号', trigger: 'blur' }],
  // password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
  // refpassword: [{ required: true, validator: validatePassword, trigger: 'blur' }]
})
// 注册
const submitForm = async (formEl) => {
  if (!formEl) return
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      const data = cloneDeep(form.value)
      data.status = '0'
      let res
      if (data.id) res = await unitStore.update(data)
      else res = await unitStore.create({ ...data, user: user.value.id })
      if ($checkRes(res, true)) {
        search()
      } else ElMessage.error(res.message)
    } else {
      console.log('error submit!', fields)
    }
  })
}
// 请求
onMounted(async () => {
  await search()
})
const search = async () => {
  if (user.value.id) {
    let res = await unitStore.query({ user: user.value.id })
    if (res.errcode == '0') form.value = res.data[0]
  }
}
</script>
<style scoped lang="scss">
.index {
  .button {
    text-align: center;
  }
}
</style>
