<template>
  <div id="detail">
    <el-row>
      <el-col :span="24" class="debt">
        <el-col :span="24" class="top">
          <!-- <topInfo :topTitle="topTitle" :display="display"></topInfo> -->
          <detailTopInfo :topTitle="topTitle" :display="display" @goBack="goBack"></detailTopInfo>
        </el-col>
        <el-col :span="24" class="main">
          <otheruserForm :ruleForm="ruleForm" :formTitle="formTitle" :charList="charList" @submitForm="submitForm" @resetForm="resetForm"></otheruserForm>
        </el-col>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import topInfo from '@/layout/common/topInfo.vue';
import detailTopInfo from '@/layout/custom/detailTopInfo.vue';
import otheruserForm from '@/layout/otheruser/otheruserForm.vue';
import { createNamespacedHelpers, mapGetters } from 'vuex';
const { mapActions: otheruser } = createNamespacedHelpers('otheruser');
const { mapActions: character } = createNamespacedHelpers('character');
export default {
  name: 'detail',
  props: {},
  components: {
    detailTopInfo, //头部导航
    otheruserForm, //头部返回
  },
  data: () => ({
    topTitle: '其他用户管理',
    display: 'block',

    formTitle: '用户信息',
    ruleForm: {
      passwd: process.env.VUE_APP_USER_PASSWD,
      characterid: '',
    },
    charList: {
      name: '10',
    },
  }),
  created() {
    this.searchInfo();
    this.searchChar();
  },
  computed: {
    id() {
      return this.$route.query.id;
    },
  },
  methods: {
    ...otheruser(['query', 'fetch', 'update', 'create']),
    ...character({ charquery: 'query', charFetch: 'fetch' }),
    async searchInfo() {
      if (this.id) {
        const res = await this.fetch(this.id);
        this.$set(this, `ruleForm`, res.data);
      }
    },
    async searchChar() {
      const res = await this.charquery();
      if (`${res.errcode}` === '0') {
        console.log(res.data);
        this.$set(this, `charList`, res.data);
      } else {
        this.$message.error(result.errmsg ? result.errmsg : 'error');
      }
    },
    // 提交
    async submitForm({ data }) {
      let res;
      if (this.id) {
        res = await this.update(data);
        if (res.errcode === 0) {
          this.$message({
            message: '信息修改成功',
            type: 'success',
          });
        }
      } else {
        res = await this.create(data);
        if (res.errcode === 0) {
          console.log(data);
          this.$message({
            message: '信息创建成功',
            type: 'success',
          });
        }
      }
      if (this.$checkRes(res)) this.resetForm();
      console.log(res.data);
    },
    // 取消
    resetForm() {
      this.$router.push({ path: '/otheruser/index' });
    },
    // 返回
    goBack() {
      this.$router.go(-1);
    },
  },
};
</script>

<style lang="less" scoped>
.top {
  height: 50px;
  margin: 0 0 10px 0;
}
.main {
  min-height: 765px;
  background: #ffffff;
  padding: 20px;
}
</style>
