<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 :model="form" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="用户名" prop="adminuser">
              <el-input v-model="form.adminuser" placeholder="请输入用户名"></el-input>
            </el-form-item>
            <el-form-item label="手机号" prop="phone">
              <el-input v-model="form.phone" placeholder="请输入用户名" disabled></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submitForm('form')">保存</el-button>
              <el-button @click="resetForm('form')">重置</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: dock } = createNamespacedHelpers('dock');
const { mapActions: authUser } = createNamespacedHelpers('authUser');
export default {
  name: 'index',
  props: {},
  components: {},
  data: function() {
    return {
      form: {},
      rules: {
        adminuser: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        phone: [{ required: true, message: '请输入手机号', trigger: 'blur' }],
      },
    };
  },
  created() {
    this.searchInfo();
  },
  methods: {
    ...dock({ dockQuery: 'query', dockFetch: 'fetch', dockUpdate: 'update' }),
    ...authUser({ authUserFetch: 'fetch', authUserUpdate: 'update' }),
    async searchInfo() {
      if (this.user.uid) {
        let res = await this.dockQuery(this.user.uid);
        if (this.$checkRes(res)) {
          for (const val of res.data) {
            this.$set(this, `form`, val);
          }
        }
      }
    },
    async submitForm() {
      let res = await this.dockUpdate(this.form);
      if (this.$checkRes(res)) {
        let data = {};
        data.id = this.user.uid;
        data.name = this.form.adminuser;
        let arr = await this.authUserUpdate(data);
        if (this.$checkRes(arr)) {
          this.$message({
            message: '修改信息成功',
            type: 'success',
          });
        } else {
          this.$message({
            message: '修改信息失败',
            type: 'error',
          });
        }
      }
      this.searchInfo();
    },
    resetForm() {
      this.form.adminuser = '';
    },
  },
  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>
