<template>
  <div class="cmain">
    <el-form ref="form" :model="form" :rules="rules" :disabled="param.detail" label-width="110px">
      <div class="bos">
        <div class="lab">
          <div class="bsg"></div>
          <div class="tit">企业信息</div>
        </div>
        <el-row>
          <el-col :span="12">
            <el-form-item label="企业名称" prop="companyName">
              <el-input v-model="form.companyName" placeholder="请输入企业名称" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="邮箱地址" prop="email">
              <el-input v-model="form.email" placeholder="请输入邮箱" clearable />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="企业负责人" prop="director">
              <el-input v-model="form.director" placeholder="请输入企业方负责人" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号" prop="phone">
              <el-input v-model="form.phone" placeholder="请输入手机号" clearable />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="联系地址" prop="address">
              <el-input type="textarea" v-model="form.address" rows="4" placeholder="请输入联系地址" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="营业执照" prop="business">
              <ImageUpload v-model="form.business" :isShowTip="false" :limit="1" :read="param.detail"></ImageUpload>
            </el-form-item>
          </el-col>
        </el-row>
      </div>
      <div class="bos">
        <div class="lab">
          <div class="bsg"></div>
          <div class="tit">对公账户信息</div>
        </div>
        <el-row>
          <el-col :span="12">
            <el-form-item label="开户行" prop="bankName">
              <el-input v-model="form.bankName" placeholder="请输入开户行" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="对公账号" prop="bankAccount">
              <el-input v-model="form.bankAccount" placeholder="请输入对公账号" clearable />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="开户许可证" prop="b1">
          <ImageUpload v-model="form.b1" :isShowTip="false" :limit="1" :read="param.detail"></ImageUpload>
        </el-form-item>
      </div>
      <div class="bos" v-if="param.detail || !form.id">
        <div class="lab">
          <div class="bsg"></div>
          <div class="tit">
            <span>平台登录账号</span>
            <span class="jz">企业管理员</span>
          </div>
        </div>
        <el-row>
          <el-col :span="12">
            <el-form-item label="登录账号" prop="userName">
              <el-input v-model="form.userName" placeholder="请输入登录账号" auto-complete="user" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="登录密码" prop="password">
              <el-input v-model="form.password" placeholder="默认123456" />
            </el-form-item>
          </el-col>
        </el-row>
      </div>
    </el-form>
    <div class="mfooter" v-if="!param.detail">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="$layer.close(layerid)">取 消</el-button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: { password: 123456 },
      rules: {
        companyName: [{ required: true, message: '不能为空', trigger: 'blur' }],
        email: [{ required: true, message: '不能为空', trigger: 'blur' }],
        director: [{ required: true, message: '不能为空', trigger: 'blur' }],
        phone: [
          { required: true, message: '不能为空', trigger: 'blur' },
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: '请输入正确的手机号码',
            trigger: 'blur'
          }
        ],
        address: [{ required: true, message: '不能为空', trigger: 'blur' }],
        business: [{ required: true, message: '不能为空', trigger: 'blur' }],
        bankName: [{ required: true, message: '不能为空', trigger: 'blur' }],
        bankAccount: [{ required: true, message: '不能为空', trigger: 'blur' }],
        b1: [{ required: true, message: '不能为空', trigger: 'blur' }],
        userName: [{ required: true, message: '账号不能为空', trigger: 'blur' }],
        password: [{ required: true, message: '密码不能为空', trigger: 'blur' }]
      }
    };
  },
  props: {
    param: {
      type: Object,
      default: () => {
        return {};
      }
    },
    layerid: {
      type: String
    }
  },
  mounted() {
    if (this.param.id) {
      this.ajax({ url: '/work/company/detail/' + this.param.id }).then((response) => {
        this.form = response.data;
      });
    }
  },
  methods: {
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.id) {
            this.ajax({ method: 'post', url: '/work/company/edit', data: this.form }).then((response) => {
              this.$modal.msgSuccess('修改成功');
              this.$layer.close(this.layerid);
              this.$parent.getList();
            });
          } else {
            this.ajax({ method: 'post', url: '/work/company/add', data: this.form }).then((response) => {
              this.$modal.msgSuccess('新增成功');
              this.$layer.close(this.layerid);
              this.$parent.getList();
            });
          }
        }
      });
    }
  }
};
</script>
