<template>
  <div class="app-container">
    <el-form ref="postForm" :rules="formRules" :model="formData" label-width="150px">
      <el-form-item label="账号：" prop="username">
        <el-col :span="6">
          <el-input v-model="formData.username" placeholder="请输入账号"/>
        </el-col>
      </el-form-item>
      <el-form-item label="密码：" prop="password">
        <el-col class="flex" :span="6">
          <el-input type="password" :disabled="!passwordValue" v-model="formData.password" placeholder="请输入密码"/>
          <el-switch class="mt-10 ml-10" v-if="$props.id" v-model="passwordValue" active-color="#13ce66" inactive-color="#ff4949"></el-switch>
        </el-col>
      </el-form-item>
      <el-form-item label="企业名称：" prop="enterprise_name">
        <el-col :span="6">
          <el-input v-model="formData.enterprise_name" placeholder="请输入企业名称"/>
        </el-col>
      </el-form-item>
      <el-form-item label="企业联系人：" prop="enterprise_man">
        <el-col :span="6">
          <el-input v-model="formData.enterprise_man" placeholder="请输入企业联系人"/>
        </el-col>
      </el-form-item>
      <el-form-item label="企业联系人电话：" prop="enterprise_phone">
        <el-col :span="6">
          <el-input v-model="formData.enterprise_phone" placeholder="请输入企业联系人电话"/>
        </el-col>
      </el-form-item>
      <el-form-item align="center">
        <el-button type="primary" @click="submitForm">保存</el-button>
        <el-button @click="onCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { checkRqInput, checkPwdLength, checkTel } from '@/utils/validate'
import { detailEnterprise, createEnterprise, updateEnterprise,  } from '@/api/user/index'

export default {
  name: 'ArticleDetail',
  // 配合路由配置的props属性实现在组件中通过props获取参数
  props: {
    id: {
      type: String,
      default() {
        return this.$route.params.id;
      }
    }
  },
  data() {
    return {
      formData: {},
      // 校验规则
      formRules: {
        username: [ checkRqInput ],
        password: [ checkRqInput, checkPwdLength ],
        enterprise_name: [checkRqInput],
        enterprise_man: [checkRqInput],
        enterprise_phone: [ checkRqInput, checkTel, ]
      },
      passwordValue: this.$props.id?false:true, // 密码按钮状态
    }
  },
  created() {
    this.fetchDetail()
  },
  methods: {
    // 获取企业详情
    fetchDetail() {
      const id = this.$props.id
      if (id) {
        this.$http(detailEnterprise(id), (res) => {
          if (res.code == 0) {
            this.formData = res.data
          }
        })
      }
    },
    // 提交表单
    submitForm() {
      this.$refs['postForm'].validate((valid) => {
        if (valid) {
          // 修改
          if (this.formData.enterprise_id) {
            this.$http(updateEnterprise(this.formData), res => {
              this.$message({
                message: '成功',
                type: 'success'
              })
              this.$router.go(-1)
            })
          } else {
            // 新增
            this.$http(createEnterprise(this.formData), res => {
              this.$message({
                message: '成功',
                type: 'success'
              })
              this.$router.go(-1)
            })
          }
        } else {
          return false
        }
      })
    },
    // 取消
    onCancel() {
      this.$message({
        message: '已取消!',
        type: 'warning'
      })
      this.$router.go(-1)
    },
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
</style>
