<template>
  <div id="assign-1">
    <van-row>
      <van-col span="24" class="main">
        <van-col span="24" class="one">
          <van-form @submit="onSubmit">
            <van-field v-model="form.answer_id" name="answer_id" label="答题人id" style="display: none" />
            <van-field
              readonly
              clickable
              name="answer_name"
              :value="form.answer_name"
              label="答题人"
              placeholder="点击选择"
              @click="showPicker = true"
              :rules="[{ required: true, message: '答题人' }]"
            />
            <van-popup v-model="showPicker" position="bottom">
              <van-picker show-toolbar :columns="answernameList" value-key="name" @confirm="answerCon" @cancel="showPicker = false" />
            </van-popup>
            <div style="margin: 16px">
              <van-button round block type="info" native-type="submit">提交</van-button>
            </div>
          </van-form>
        </van-col>
      </van-col>
    </van-row>
  </div>
</template>

<script>
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: answer_tea } = createNamespacedHelpers('answer_tea');

export default {
  name: 'assign-1',
  props: {
    form: { type: Object },
  },
  components: {},
  data: function () {
    return {
      answernameList: [],
      showPicker: false,
    };
  },
  created() {
    this.search();
  },
  methods: {
    ...answer_tea(['query']),
    async search() {
      let res = await this.query();
      if (this.$checkRes(res)) {
        this.$set(this, `answernameList`, res.data);
      }
    },
    answerCon(values) {
      this.$set(this.form, `answer_id`, values.id);
      this.$set(this.form, `answer_name`, values.name);
      this.showPicker = false;
    },
    // 提交
    onSubmit(values) {
      if (values) this.$emit('onSubmit', { data: this.form });
    },
  },
  computed: {
    ...mapState(['user']),
  },
  metaInfo() {
    return { title: this.$route.meta.title };
  },
  watch: {
    test: {
      deep: true,
      immediate: true,
      handler(val) {},
    },
  },
};
</script>

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