<template>
  <div class="index">
    <el-form
      ref="ruleFormRef"
      :model="form"
      :rules="rules"
      label-width="110px"
      class="form"
      label-position="left"
    >
      <el-row>
        <el-form-item label="企业Logo" prop="logo">
          <custom-upload
            model="logo"
            :list="form.logo"
            :limit="1"
            url="/files/web/template_company/upload"
            @change="onUpload"
            listType="picture-card"
          ></custom-upload>
        </el-form-item>
      </el-row>
      <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="pattern">
            <el-select clearable v-model="form.pattern" placeholder="请选择企业类型">
              <el-option
                v-for="(item, index) in patternList"
                :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="scale">
            <el-select clearable v-model="form.scale" placeholder="请选择企业规模">
              <el-option
                v-for="item in scaleList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="统一信用代码" prop="code">
            <el-input clearable v-model="form.code" placeholder="请输入统一信用代码" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <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 IndustryList"
                :key="index"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="所在地区" prop="region">
            <el-cascader
              v-model="form.area"
              :props="{ value: 'label', label: 'label' }"
              :options="cityList"
              clearable
              placeholder="请选择所在地区"
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="法定代表人" prop="representative">
            <el-input clearable v-model="form.representative" placeholder="请输入法定代表人名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="电子邮箱" prop="email">
            <el-input clearable v-model="form.email" type="email" placeholder="请输入电子邮箱" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="员工人数" prop="person">
            <el-input clearable v-model="form.person" placeholder="请输入员工人数" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="注册资本" prop="register">
            <el-input clearable v-model="form.register" placeholder="请输入注册资本(万元)" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="成立时间" prop="create_time">
            <el-date-picker
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
              v-model="form.create_time"
              type="date"
              placeholder="请选择成立时间"
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="企业地址" prop="address">
            <el-input clearable v-model="form.address" placeholder="请输入企业地址" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-col :span="24">
        <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-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 patternList = inject('patternList')
const scaleList = inject('scaleList')
const IndustryList = inject('IndustryList')
const cityList = inject('cityList')
const isUseList = inject('isUseList')
// 接口
import { CompanyStore } from '@/store/api/user/company'
const companyStore = CompanyStore()
const form = ref({})
const rules = reactive({
  logo: [{ required: true, message: '请输入企业Logo', trigger: 'blur' }],
  name: [{ required: true, message: '请输入企业名称', trigger: 'blur' }],
  pattern: [{ required: true, message: '请输入企业类型', trigger: 'blur' }],
  scale: [{ required: true, message: '请输入企业规模', trigger: 'blur' }],
  type: [{ required: true, message: '请输入所属行业', trigger: 'blur' }],
  representative: [{ required: true, message: '请输入法定代表人', trigger: 'blur' }],
  companyStatus: [{ required: true, message: '请输入公司状态', trigger: 'blur' }],
  create_time: [{ required: true, message: '请输入成立时间', trigger: 'blur' }],
  code: [{ required: true, message: '请输入统一社会信用代码', trigger: 'blur' }],
  email: [{ required: true, message: '请输入电子邮箱', trigger: 'blur' }],
  person: [{ required: true, message: '请输入员工人数', trigger: 'blur' }],
  region: [{ required: true, message: '请选择地区', trigger: 'blur' }],
  register: [{ required: true, message: '请输入注册资金', trigger: 'blur' }],
  is_show: [{ required: true, message: '请选择是否公开', trigger: 'blur' }],
  brief: [{ required: true, message: '请输入简介', trigger: 'blur' }],
  address: [{ required: true, message: '请输入地址', trigger: 'blur' }]
})
// 上传图片
const onUpload = (e) => {
  const { model, value } = e
  form.value[model] = value
}
// 注册
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 companyStore.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 companyStore.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>
