<template>
  <a-modal
    :title="title"
    :width="600"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">

        <a-form-model-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          prop="title"
          required
          label="模板名称">
          <a-input placeholder="请输入模板名称" v-model="model.title" style="width: 174px"/>
        </a-form-model-item>

        <!-- <a-form-model-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          prop="yType"
          required
          label="曲线类型">
           <a-radio-group v-decorator="['radio-group']" style="width: 174px" v-model="model.yType">
                <a-radio value="time"> 时间 </a-radio>
                <a-radio value="deep"> 深度 </a-radio>
              </a-radio-group>
        </a-form-model-item> -->
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
  import { addTemplate } from '@/api/api'

  export default {
    name: 'TemplateModal',
    data() {
      return {
        value: 1,
        title: '操作',
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        confirmLoading: false,
        validatorRules: {
          title: [{ required: true, message: '不能为空!' }],
          yType: [{ required: true, message: '不能为空!' }]
          }
      }
    },
    created() {
    },
    methods: {
      add() {
       this.visible = true
        this.model = Object.assign({}, '')
      },
      // 确定
      handleOk() {
        const that = this
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true
            this.model.title = (this.model.title || '').trim()
            this.model.yType = 'time'
            this.model.isDefault='Y'
            let obj
            obj = addTemplate(this.model)
            obj.then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok',res.result.id)
              } else {
                that.$message.warning(res.message)
              }
            }).finally(() => {
              that.confirmLoading = false
              that.close()
            })
          }else{
            return false;
          }
        })
      },
      // 关闭
      handleCancel() {
        this.close()
      },
      close() {
        this.$emit('close')
        this.visible = false
        this.$refs.form.resetFields();
      }
    }
  }
</script>