<!--
 * @Author: wally
 * @email: 18603454788@163.com
 * @Date: 2021-02-22 09:20:03
 * @LastEditors: aBin
 * @LastEditTime: 2021-05-31 09:46:52
-->
<template>
  <div class="d-flex flex-wrap pb-3">
    <!-- 添加 -->
    <a-modal :closable="false" footer title="新增病例" v-model="visible" width="700px" :mask-closable="false">
      <a-form :form="form" @submit="handleSubmit" ref="form">
        <!-- 研究编号 -->
        <a-form-item :label-col="formItemLayout.labelCol" :wrapper-col="formItemLayout.wrapperCol" label="研究序号">
          <a-input-number
            style="width: 100%"
            placeholder="研究序号"
            :min="0"
            :max="999"
            v-decorator="[
              'codeNum',
              {
                rules: [{ required: true, message: '研究序号不能为空' }],
              },
            ]"
          />
        </a-form-item>
        <!-- 住院号 -->
        <a-form-item :label-col="formItemLayout.labelCol" :wrapper-col="formItemLayout.wrapperCol" label="住院号">
          <a-input
            style="width: 100%"
            placeholder="住院号"
            v-decorator="[
              'hospitalization',
              {
                rules: [{ required: true, message: '住院号不能为空' }],
              },
            ]"
          />
        </a-form-item>
        <!-- 组别 -->
        <a-form-item :label-col="formItemLayout.labelCol" :wrapper-col="formItemLayout.wrapperCol" label="组别">
          <a-select placeholder="请选择组别" v-decorator="['inpatientId', { rules: [{ required: true, message: '组别不能为空' }] }]">
            <a-select-option :key="index" :value="group.id" v-for="(group, index) in controlGroups">{{ group.name }}</a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item class="d-flex flex-row-reverse">
          <a-button @click="$emit('closeModal')" class="mr-3">取消</a-button>
          <a-button class="white--text" html-type="submit" type="primary">保存</a-button>
        </a-form-item>
      </a-form>
    </a-modal>
  </div>
</template>

<script>
import { mapState } from 'vuex';
import { savePatientMes } from 'config/api';

const formItemLayout = {
  labelCol: { span: 6 },
  wrapperCol: { span: 16 },
};
const tailItemLayout = { wrapperCol: { span: 16, offset: 6 } };
export default {
  name: 'ActivityAdd',
  props: { visible: { type: Boolean, default: false } },
  data() {
    return {
      formItemLayout,
      tailItemLayout,
      form: this.$form.createForm(this, { name: 'r-d-add' }),
    };
  },

  computed: mapState('home', ['controlGroups']),

  methods: {
    // 提交表单
    handleSubmit(e) {
      e.preventDefault();
      this.form.validateFieldsAndScroll(async (err, values) => {
        if (!err) {
          try {
            const params = { param: values };
            const res = await savePatientMes(params);
            const { data, msg, code } = res.data;
            this.$emit('closeModal');
            if (code === 200) {
              this.$message.success('添加成功');
              this.$emit('searchMes');
              // 清空表单
              this.form.resetFields();
            } else {
              throw msg;
            }
          } catch (error) {
            // console.log('error: ', error);
            this.$message.error(error || '添加失败');
          }
        }
      });
    },
  },
};
</script>

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