<template>
  <div class="index">
    <el-form
      ref="ruleFormRef"
      :model="form"
      :rules="rules"
      label-width="90px"
      class="form"
      label-position="left"
    >
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="名称" prop="name">
            <el-input clearable v-model="form.name" placeholder="请输入名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="负责人姓名" prop="person">
            <el-input 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 clearable v-model="form.person_phone" placeholder="请输入负责人电话" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="部门类型" prop="type">
            <el-select clearable v-model="form.type" placeholder="请选择部门类型">
              <el-option
                v-for="(item, index) in typeList"
                :key="index"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="是否公开" prop="is_show">
            <el-radio-group 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
            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
            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 { StateStore } from '@/store/api/user/state'
const stateStore = StateStore()
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'
      const res = await stateStore.update(data)
      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 stateStore.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>
