<template>
  <div id="index">
    <el-row>
      <el-col :span="24">
        <el-col :span="24" class="leftTop"> <span>|</span> <span>基本信息</span> </el-col>
        <el-col :span="24" class="info">
          <el-form ref="form" :model="form" :rules="rules" label-width="120px">
            <el-form-item label="用户名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入用户名称"></el-input>
            </el-form-item>
            <el-form-item label="手机号" prop="phone">
              <el-input v-model="form.phone" placeholder="请输入手机号" :minlength="11" :maxlength="11"></el-input>
            </el-form-item>
            <el-form-item label="登录密码" prop="password">
              <el-input v-model="form.password" placeholder="请输入登录密码" show-password></el-input>
            </el-form-item>
            <el-form-item label="用户类别" prop="role">
              <el-radio-group v-model="form.role">
                <el-radio label="2">个人</el-radio>
                <el-radio label="3">企业管理员</el-radio>
                <el-radio label="6">专家</el-radio>
                <el-radio label="7" disabled>临时用户</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="身份证号" prop="cardnumber" v-if="form.role == '2' || form.role == '3' || form.role == '6'">
              <el-input v-model="form.cardnumber" placeholder="请输入身份证号" :minlength="18" :maxlength="18"></el-input>
            </el-form-item>
            <el-form-item label="邮箱" prop="email" v-if="form.role == '2' || form.role == '3' || form.role == '6'">
              <el-input v-model="form.email" placeholder="请输入邮箱"></el-input>
            </el-form-item>
            <el-form-item label="地址" prop="addr" v-if="form.role == '2' || form.role == '3' || form.role == '6'">
              <el-input v-model="form.addr" placeholder="请输入地址"></el-input>
            </el-form-item>
            <el-form-item label="头像图片" prop="img_path" v-if="form.role == '2' || form.role == '3' || form.role == '6'">
              <upload :limit="1" :data="form.img_path" type="img_path" :url="'/files/imgpath/upload'" @upload="uploadSuccess"></upload>
            </el-form-item>
            <el-form-item label="身份证正面" v-if="form.role == '2' || form.role == '3'" prop="cardfile_a">
              <upload :limit="1" :data="form.cardfile_a" type="cardfile_a" :url="'/files/cardfilea/upload'" @upload="uploadSuccess"></upload>
            </el-form-item>
            <el-form-item label="身份证背面" v-if="form.role == '2' || form.role == '3'" prop="cardfile_b">
              <upload :limit="1" :data="form.cardfile_b" type="cardfile_b" :url="'/files/cardfileb/upload'" @upload="uploadSuccess"></upload>
            </el-form-item>
            <el-form-item label="组织机构图片" v-if="form.role == '3'" prop="img_qy">
              <upload :limit="1" :data="form.img_qy" type="img_qy" :url="'/files/imgqy/upload'" @upload="uploadSuccess"></upload>
            </el-form-item>
            <template v-if="form.role == '3'">
              <el-form-item label="机构类型" prop="institution_type">
                <el-input v-model="form.institution_type" placeholder="请输入机构类型"></el-input>
              </el-form-item>
              <el-form-item label="机构名称" prop="institution_name">
                <el-input v-model="form.institution_name" placeholder="请输入机构名称"></el-input>
              </el-form-item>
              <el-form-item label="机构代码" prop="institution_code">
                <el-input v-model="form.institution_code" placeholder="请输入机构代码"></el-input>
              </el-form-item>
              <el-form-item label="机构性质" prop="institution_nature">
                <el-input v-model="form.institution_nature" placeholder="请输入机构性质"></el-input>
              </el-form-item>
            </template>
            <el-form-item label="办公电话" v-if="form.role == '2' || form.role == '3'" prop="office_phone">
              <el-input v-model="form.office_phone" placeholder="请输入办公电话"></el-input>
            </el-form-item>
            <el-form-item label="所属行业" v-if="form.role == '2' || form.role == '3'" prop="profession">
              <el-input v-model="form.profession" placeholder="请输入所属行业"></el-input>
            </el-form-item>
            <el-form-item label="性别" v-if="form.role == '6'" prop="gender">
              <el-select v-model="form.gender" placeholder="请选择民性别">
                <el-option v-for="(item, index) in gender_list" :key="index" :label="item.name" :value="item.name"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="出生日期" v-if="form.role == '6'" prop="birthday">
              <el-date-picker v-model="form.birthday" type="date" placeholder="选择日期" value-format="yyyy-MM-dd"> </el-date-picker>
            </el-form-item>
            <el-form-item label="职称级别" v-if="form.role == '6'" prop="level">
              <el-input v-model="form.level" placeholder="请输入职称级别"></el-input>
            </el-form-item>
            <el-form-item label="职称" v-if="form.role == '6'" prop="levelname">
              <el-input v-model="form.levelname" placeholder="请输入职称"></el-input>
            </el-form-item>
            <el-form-item label="职务" v-if="form.role == '6'" prop="position">
              <el-input v-model="form.position" placeholder="请输入职务"></el-input>
            </el-form-item>
            <el-form-item label="院校" v-if="form.role == '6'" prop="school">
              <el-input v-model="form.school" placeholder="请输入院校"></el-input>
            </el-form-item>
            <el-form-item label="学历" v-if="form.role == '6'" prop="xl">
              <el-select v-model="form.xl" placeholder="请选择学历要求">
                <el-option v-for="(item, index) in edu_list" :key="index" :label="item.name" :value="item.name"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="学位" v-if="form.role == '6'" prop="xw">
              <el-select v-model="form.xw" placeholder="请选择学位">
                <el-option v-for="(item, index) in degree_list" :key="index" :label="item.name" :value="item.name"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="专业" v-if="form.role == '6'" prop="major">
              <el-input v-model="form.major" placeholder="请输入专业"></el-input>
            </el-form-item>
            <el-form-item label="从事专业" v-if="form.role == '6'" prop="professional">
              <el-input v-model="form.professional" placeholder="请输入从事专业"></el-input>
            </el-form-item>
            <el-form-item label="个人简历" v-if="form.role == '2' || form.role == '6'" prop="resume">
              <el-input type="textarea" v-model="form.resume" placeholder="请输入个人简介"></el-input>
            </el-form-item>
            <el-form-item label="项目" v-if="form.role == '6'" prop="project">
              <el-input v-model="form.project" placeholder="请输入项目"></el-input>
            </el-form-item>
            <el-form-item label="学术成就" v-if="form.role == '6'" prop="academic">
              <el-input v-model="form.academic" placeholder="请输入学术成就"></el-input>
            </el-form-item>
            <el-form-item label="论文" v-if="form.role == '6'" prop="paper">
              <el-input v-model="form.paper" placeholder="请输入论文"></el-input>
            </el-form-item>
            <el-form-item label="备注" v-if="form.role == '6'" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入备注"></el-input>
            </el-form-item>
            <el-form-item label="用户审核状态" prop="status" v-if="form.status == '3'">
              <el-radio-group v-model="form.status">
                <el-radio label="0">待审核</el-radio>
                <!-- <el-radio label="1" disabled>审核成功</el-radio>
                <el-radio label="2" disabled>审核失败</el-radio> -->
                <el-radio label="3">待认证</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="authSubmit" v-if="form.status == '0' || form.status == '3'">提交认证</el-button>
              <el-button type="primary" @click="onSubmit">提交修改</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { mapState, createNamespacedHelpers } from 'vuex';
import upload from '@/components/uploadone.vue';
const { mapActions: market } = createNamespacedHelpers('market');
const { mapActions: expertsuser } = createNamespacedHelpers('expertsuser');
export default {
  name: 'index',
  props: {},
  components: {
    upload,
  },
  data: function() {
    return {
      // 性别
      gender_list: [
        {
          name: '男',
        },
        {
          name: '女',
        },
      ],
      // 学历
      edu_list: [
        {
          name: '中专及以上',
        },
        {
          name: '大专及以上',
        },
        {
          name: '本科及以上',
        },
        {
          name: '研究生及以上',
        },
      ],
      // 学位
      degree_list: [
        {
          name: '学士',
        },
        {
          name: '博士',
        },
        {
          name: '其他',
        },
      ],
      rules: {
        name: [{ required: true, message: '请输入用户名称', trigger: 'blur' }],
        phone: [{ required: true, message: '请输入手机号', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        cardnumber: [{ required: true, message: '请输入身份证号', trigger: 'blur' }],
        email: [{ required: true, message: '请输入邮箱', trigger: 'blur' }],
        addr: [{ required: true, message: '请输入地址', trigger: 'blur' }],
        img_path: [{ required: true, message: '请输入头像', trigger: 'blur' }],
        role: [{ required: true, message: '请输入用户类型', trigger: 'blur' }],
        cardfile_a: [{ required: false, message: '请输入身份证正面', trigger: 'blur' }],
        cardfile_b: [{ required: false, message: '请输入身份证反面', trigger: 'blur' }],
        img_qy: [{ required: false, message: '请输入组织机构图片', trigger: 'blur' }],
        institution_type: [{ required: false, message: '请输入机构类型', trigger: 'blur' }],
        institution_name: [{ required: false, message: '请输入机构名称', trigger: 'blur' }],
        institution_code: [{ required: false, message: '请输入机构代码', trigger: 'blur' }],
        institution_nature: [{ required: false, message: '请输入机构性质', trigger: 'blur' }],
        office_phone: [{ required: false, message: '请输入办公电话', trigger: 'blur' }],
        profession: [{ required: false, message: '请输入从事专业', trigger: 'blur' }],
        gender: [{ required: false, message: '请输入性别', trigger: 'blur' }],
        birthday: [{ required: false, message: '请输入出生日期', trigger: 'blur' }],
        level: [{ required: false, message: '请输入职称级别', trigger: 'blur' }],
        levelname: [{ required: false, message: '请输入职称级别', trigger: 'blur' }],
        position: [{ required: false, message: '请输入职务', trigger: 'blur' }],
        school: [{ required: false, message: '请输入院校', trigger: 'blur' }],
        xl: [{ required: false, message: '请输入学历', trigger: 'blur' }],
        xw: [{ required: false, message: '请输入学位', trigger: 'blur' }],
        major: [{ required: false, message: '请输入专业', trigger: 'blur' }],
        professional: [{ required: false, message: '请输入从事专业', trigger: 'blur' }],
        resume: [{ required: false, message: '请输入个人简介', trigger: 'blur' }],
        project: [{ required: false, message: '请输入项目', trigger: 'blur' }],
        academic: [{ required: false, message: '请输入学术成就', trigger: 'blur' }],
        paper: [{ required: false, message: '请输入论文', trigger: 'blur' }],
        remark: [{ required: false, message: '请输入备注', trigger: 'blur' }],
      },
      form: {},
    };
  },
  created() {
    this.searchInfo();
  },
  methods: {
    ...market(['fetch', 'update']),
    ...expertsuser({ expertsuserFetch: 'fetch', expertsuserUpdate: 'update' }),
    async searchInfo() {
      if (this.user.role == '2' || this.user.role == '3' || this.user.role == '7') {
        let res = await this.fetch(this.user.userid);
        if (res.errcode === 0) {
          this.$set(this, `form`, res.data);
        }
      } else if (this.user.role == '6') {
        let res = await this.expertsuserFetch(this.user.userid);
        if (res.errcode === 0) {
          this.$set(this, `form`, res.data);
        }
      }
    },
    async onSubmit() {
      if (this.form.role == '2' || this.form.role == '3' || this.form.role == '7') {
        let res;
        let msg;
        res = await this.update(this.form);
      } else if (this.form.role == '6') {
        let res;
        let msg;
        res = await this.expertsuserUpdate(this.form);
      }
      this.$message({
        message: '修改信息成功',
        type: 'success',
      });
      this.searchInfo();
    },
    async authSubmit() {
      let res;
      let msg;
      res = await this.update(this.form);
      this.$message({
        message: '提交认证信息成功，等待管理员认证',
        type: 'success',
      });
      this.searchInfo();
    },
    uploadSuccess({ type, data }) {
      this.$set(this.form, `${type}`, data.uri);
    },
  },
  computed: {
    ...mapState(['user']),
    pageTitle() {
      return `${this.$route.meta.title}`;
    },
  },
  metaInfo() {
    return { title: this.$route.meta.title };
  },
};
</script>

<style lang="less" scoped>
.leftTop {
  font-size: 18px;
  width: 96%;
  height: 41px;
  line-height: 35px;
  border-bottom: 1px solid #e5e5e5;
  position: relative;
  bottom: 1px;
  margin: 10px;
  font-weight: 600;
  color: #22529a;
}
.info {
  padding: 0 40px 0 0;
}
</style>
