<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 label="显示名称:" prop="title" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input style="width:170px" v-model="model.title"> </a-input>
            </a-form-model-item>
            <a-form-model-item label="宽度（像素）:" prop="width" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number style="width: 174px" v-model="model.width"></a-input-number>
            </a-form-model-item>
            <a-form-model-item label="宽度是否固定:" prop="fix" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-radio-group v-decorator="['radio-group']" v-model="model.fix">
                <a-radio value="1"> 是 </a-radio>
                <a-radio value="0"> 否 </a-radio>
              </a-radio-group>
            </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

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

  export default {
    name: 'TrackModal',
    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: '不能为空!' }],
          width: [{ required: true, message: '不能为空!' }],
          fix: [{ required: true, message: '不能为空!' }]
          }
      }
    },
    created() {
    },
    methods: {
      add(templateId) {
        this.visible = true
        this.model = Object.assign({}, '')
        this.model.templateId=templateId
      },
      // 确定
      handleOk() {
        const that = this
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true
            this.model.title = (this.model.title || '').trim()
            let obj
            obj = addTrack(this.model)
            obj.then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok', res.result.templateId+'-'+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>