<template>
  <div id="form-1">
    <el-row>
      <el-col :span="24" class="main">
        <el-col class="top-btn">
          <el-button type="primary" size="mini" @click="toBack()">返回</el-button>
        </el-col>
        <el-col :span="24" class="two">
          <data-search :fields="searchFields" v-model="searchInfo" @query="search"> </data-search>
        </el-col>
        <el-col class="top-btn">
          <el-button type="warning" size="mini" @click="toGrant()">批量发放</el-button>
        </el-col>
        <el-col :span="24" class="four">
          <data-table
            :select="true"
            :selected="selected"
            @handleSelect="handleSelect"
            :fields="fields"
            :opera="opera"
            @query="search"
            :data="list"
            :total="total"
            @grant="toGrant"
          >
          </data-table>
        </el-col>
      </el-col>
    </el-row>
  </div>
</template>

<script>
const _ = require('lodash');
const moment = require('moment');
import { mapState, mapGetters, createNamespacedHelpers } from 'vuex';
const { mapActions } = createNamespacedHelpers('users');
const { mapActions: dictData } = createNamespacedHelpers('dictData');
export default {
  name: 'form-1',
  props: { id: { type: String } },
  components: {},
  data: function () {
    return {
      // 列表
      opera: [{ label: '发放', method: 'grant', confirm: true }],
      // 多选值
      selected: [],
      fields: [
        { label: 'openid', model: 'openid' },
        { label: '用户姓名', model: 'name' },
        { label: '电话', model: 'phone' },
        { label: '邮箱', model: 'email' },
        { label: '出生年月', model: 'birth' },
        {
          label: '性别',
          model: 'gender',
          format: (i) => {
            let data = this.genderList.find((f) => f.value == i);
            if (data) return data.label;
            else return '';
          },
        },
        {
          label: '状态',
          model: 'status',
          format: (i) => {
            let data = this.useList.find((f) => f.value == i);
            if (data) return data.label;
            else return '';
          },
        },
      ],
      list: [],
      total: 0,
      // 查询
      searchInfo: {},
      searchFields: [{ label: '用户姓名', model: 'name' }],
      // 性别
      genderList: [],
      // 用户状态
      useList: [],
    };
  },
  async created() {
    await this.searchOther();
    await this.search();
  },
  methods: {
    ...dictData({ dictQuery: 'query' }),
    ...mapActions(['query', 'delete', 'fetch', 'update', 'create']),
    // 查询
    async search({ skip = 0, limit = this.$limit, ...info } = {}) {
      let condition = _.cloneDeep(this.searchForm);
      let res = await this.query({ skip, limit, ...condition, ...info });
      if (this.$checkRes(res)) {
        this.$set(this, 'list', res.data);
        this.$set(this, 'total', res.total);
      }
    },
    // 返回
    toBack() {
      this.$emit('toBack');
    },
    // 多选
    handleSelect(data) {},
    toGrant() {},
    // 重置
    toClose() {
      this.fieldform = {};
      this.search();
    },
    async searchOther() {
      let res;
      // 性别
      res = await this.dictQuery({ code: 'gender' });
      if (this.$checkRes(res)) {
        this.$set(this, `genderList`, res.data);
      }
      // 用户状态
      res = await this.dictQuery({ code: 'user_status' });
      if (this.$checkRes(res)) {
        this.$set(this, `useList`, res.data);
      }
    },
  },
  computed: {},
  metaform() {
    return { title: this.$route.meta.title };
  },
  watch: {
    test: {
      deep: true,
      immediate: true,
      handler(val) {},
    },
  },
};
</script>

<style lang="less" scoped>
.main {
  .one {
    font-size: 20px;
    margin: 10px 0 0 20%;
    padding: 5px;
  }
  .btn {
    text-align: center;
  }
  .top-btn {
    margin: 20px 0;
  }
}
</style>
