<template>
  <div id="detail">
    <el-row>
      <el-col :span="24" class="index">
        <el-col :span="24" class="top">
          <topInfo :topTitle="pageTitle"></topInfo>
        </el-col>
        <el-col :span="24" class="main">
          <data-form :fields="fields" :data="form" :rules="rules" @save="drawerSave" :isNew="drawerIsNew">
            <template #radios="{item}">
              <template v-if="item.model === 'gender'">
                <el-radio label="男" value="男"></el-radio>
                <el-radio label="女" value="女"></el-radio>
              </template>
              <template v-else>
                <el-radio label="3">主播</el-radio>
                <el-radio label="4">房间用户</el-radio>
              </template>
            </template>
          </data-form>
        </el-col>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import topInfo from '@/layout/public/top.vue';
import dataForm from '@/components/form.vue';
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: roomuser } = createNamespacedHelpers('roomuser');
export default {
  name: 'detail',
  props: {},
  components: {
    topInfo,
    dataForm,
  },
  data: function() {
    return {
      drawerIsNew: false,
      form: {},
      fields: [
        { label: '姓名', prop: 'name', model: 'name' },
        { label: '电话', prop: 'phone', model: 'phone' },
        { label: '密码', prop: 'passwd', model: 'passwd', type: 'paswword' },
        { label: '机构名称', prop: 'deptname', model: 'deptname' },
        { label: '职务', prop: 'level', model: 'level' },
        { label: '个人简介', prop: 'title', model: 'title', type: 'textarea' },
        { label: '备注', prop: 'remark', model: 'remark', type: 'textarea' },
        { label: '用戶类型', required: true, model: 'role', type: 'radio' },
      ],
      rules: {
        name: [{ required: true, message: '请输入姓名' }],
        passwd: [{ required: false, message: '请输入密码' }],
        phone: [{ required: true, message: '请输入电话' }],
        deptname: [{ required: true, message: '请输入机构名称' }],
        role: [{ required: true, message: '请选择用户类型' }],
      },
    };
  },
  created() {
    this.search();
  },
  methods: {
    ...roomuser(['query', 'delete', 'update', 'create', 'fetch']),
    async search() {
      if (this.id) {
        let res = await this.fetch(this.id);
        this.$set(this, `form`, res.data);
      }
    },
    // 创建&修改
    async drawerSave({ data, isNew }) {
      let res;
      let msg;
      if (this.isNew) {
        res = await this.update(data);
        msg = `${this.keyWord}修改成功`;
      } else {
        res = await this.create(data);
        msg = `${this.keyWord}添加成功`;
      }
      if (this.$checkRes(res, msg)) this.$router.push({ path: './index' });
    },
  },
  computed: {
    id() {
      return this.$route.query.id;
    },
    isNew() {
      return this.$route.query.id ? true : false;
    },
    pageTitle() {
      return `${this.$route.meta.title}`;
    },
    mainTitle() {
      let meta = this.$route.meta;
      let main = meta.title || '';
      let sub = meta.sub || '';
      return `${main}${sub}`;
    },
    keyWord() {
      let meta = this.$route.meta;
      let main = meta.title || '';
      return main;
    },
  },

  metaInfo() {
    return { title: this.$route.meta.title };
  },
  watch: {
    isNew: {
      handler(val) {
        if (!val) {
          this.search();
        }
      },
      immediate: true,
    },
  },
};
</script>

<style lang="less" scoped></style>
