<template>
  <div id="info-1">
    <van-row>
      <van-col span="24" class="main">
        <van-form @submit="onSubmit">
          <van-field v-model="form.id" name="id" label="数据id" style="display: none" />
          <van-field v-model="form.mech_name" name="mech_name" label="机构名称" :rules="[{ required: true, message: '机构名称' }]" />
          <van-field v-model="form.phone" name="phone" label="账号" :rules="[{ required: true, message: '账号' }]" disabled />
          <van-field v-model="form.name" name="name" label="联系电话" :rules="[{ required: true, message: '联系电话' }]" />
          <div style="margin: 16px">
            <van-button round block type="info" native-type="submit">提交</van-button>
          </div>
        </van-form>
      </van-col>
    </van-row>
  </div>
</template>

<script>
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: agent_mech } = createNamespacedHelpers('agent_mech');
export default {
  name: 'info-1',
  props: {},
  components: {},
  data: function () {
    return {
      form: {},
    };
  },
  created() {
    this.search();
  },
  methods: {
    ...agent_mech(['fetch', 'update']),
    async search() {
      if (this.user.id) {
        let res = await this.fetch(this.user.id);
        if (this.$checkRes(res)) {
          this.$set(this, `form`, res.data);
        }
      }
    },
    async onSubmit(values) {
      let res = await this.update(values);
      if (this.$checkRes(res)) {
        this.$toast({ type: `success`, message: `操作完成` });
        this.search();
      }
    },
  },
  computed: {
    ...mapState(['user']),
  },
  metaInfo() {
    return { title: this.$route.meta.title };
  },
  watch: {
    test: {
      deep: true,
      immediate: true,
      handler(val) {},
    },
  },
};
</script>

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