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

<script>
import detailTopInfo from '@/layout/custom/detailTopInfo.vue';
import characterForm from '@/layout/character/characterForm.vue';
import { createNamespacedHelpers, mapGetters } from 'vuex';
const { mapActions: character } = createNamespacedHelpers('character');
const { mapActions: menurole } = createNamespacedHelpers('menurole');
export default {
  name: 'detail',
  props: {},
  components: {
    characterForm, //添加其他用户
    detailTopInfo,
  },
  data: () => ({
    formTitle: '角色信息',
    display: 'block',
    topTitle: '角色信息管理',
    ruleForm: {
      passwd: '123456',
      roles: [],
    },
    rolesList: [
      {
        name: '10',
      },
    ],
  }),
  created() {
    this.searchInfo();
    this.searchRole();
  },
  computed: {
    id() {
      return this.$route.query.id;
    },
  },
  methods: {
    ...character(['query', 'fetch', 'update', 'create']),
    ...menurole({ rolequery: 'query', rolesFetch: 'fetch' }),
    async searchInfo() {
      if (this.id) {
        const res = await this.fetch(this.id);
        if (`${res.errcode}` === '0') {
          this.$set(this, `ruleForm`, res.data);
        } else {
          this.$message.error(result.errmsg ? result.errmsg : 'error');
        }
      }
    },
    async searchRole() {
      const res = await this.rolequery();
      if (`${res.errcode}` === '0') {
        this.$set(this, `rolesList`, 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) {
          this.$message({
            message: '信息创建成功',
            type: 'success',
          });
        }
      }
      if (this.$checkRes(res)) this.resetForm();

      console.log(res.data);
    },
    // 取消
    resetForm() {
      this.$router.push({ path: '/character/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>
