<template>
  <div id="add">
    <el-row>
      <el-col :span="24" class="main animate__animated animate__backInRight">
        <el-col :span="24" class="one">
          <c-search :is_title="true" :is_back="true" @toBack="toBack"></c-search>
        </el-col>
        <el-col :span="24" class="two">
          <data-form :fields="fields" :form="form" :rules="{}" @save="toSave" :span="24">
            <template #is_vip>
              <el-option v-for="i in isnoList" :key="i._id" :label="i.label" :value="i.value"></el-option>
            </template>
            <template #gender>
              <el-option v-for="i in genderList" :key="i._id" :label="i.label" :value="i.value"></el-option>
            </template>
            <template #logo_url="{ item }">
              <c-upload v-model="form[item.model]" url="/files/projectadmin/imgurl/upload" :limit="1"></c-upload>
            </template>
          </data-form>
        </el-col>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions } = createNamespacedHelpers('users');
const { mapActions: dictdata } = createNamespacedHelpers('dictdata');
export default {
  name: 'add',
  props: {},
  components: {},
  data: function () {
    return {
      fields: [
        { label: '账号', model: 'account', options: { readonly: true } },
        { label: '昵称', model: 'nick_name' },
        { label: '联系电话', model: 'phone' },
        { label: '性别', model: 'gender', type: 'select' },
        { label: '头像', model: 'logo_url', custom: true },
        { label: '是否为vip', model: 'is_vip', type: 'select' },
        { label: 'vip开始时间', model: 'vip_start_time', type: 'datetime' },
        { label: 'vip结束时间', model: 'vip_end_time', type: 'datetime' },
      ],
      form: {},
      // 字典
      // 性别
      genderList: [],
      // 是否
      isnoList: [],
    };
  },
  async created() {
    await this.searchOther();
    await this.search();
  },
  methods: {
    ...mapActions(['fetch', 'create', 'update']),
    ...dictdata({ dQuery: 'query' }),
    async search() {
      if (this.id) {
        let res = await this.fetch(this.id);
        if (this.$checkRes(res)) {
          this.$set(this, `form`, res.data);
        }
      }
    },
    // 保存信息
    async toSave({ data }) {
      let res;
      if (data.id) res = await this.update(data);
      else res = await this.create(data);
      if (this.$checkRes(res, `维护信息成功`, res.errmsg)) this.toBack();
    },
    // 字典
    async searchOther() {
      let res;
      // 性别
      res = await this.dQuery({ type: 'gender' });
      if (this.$checkRes(res)) {
        this.$set(this, `genderList`, res.data);
      }
      // 是否
      res = await this.dQuery({ type: 'is_no' });
      if (this.$checkRes(res)) {
        this.$set(this, `isnoList`, res.data);
      }
    },
    // 返回
    toBack() {
      window.history.go('-1');
    },
  },
  computed: {
    ...mapState(['user']),
    id() {
      return this.$route.query.id;
    },
  },
  metaInfo() {
    return { title: this.$route.meta.title };
  },
  watch: {
    test: {
      deep: true,
      immediate: true,
      handler(val) {},
    },
  },
};
</script>

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