<template>

  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    switchFullscreen
    @ok="handleOk"
    :okButtonProps="{ class:{'jee-hidden': disableSubmit} }"
    @cancel="handleCancel"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <j-form-container :disabled="disableSubmit">
        <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
          <a-row>
            <a-col :span="24">
              <a-form-model-item label="任务名称" prop="taskName" v-bind="labelCol1">
                <a-input v-model="model.taskName" placeholder="请输入任务名称"  ></a-input>
              </a-form-model-item>
            </a-col>

            <a-col :span="12">
              <a-form-model-item label="开始时间" prop="taskStartTime" v-bind="labelCol2">
                <j-date placeholder="请选择任务开始时间"  v-model="model.taskStartTime" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="结束时间" prop="taskEndTime" v-bind="labelCol2">
                <j-date placeholder="请选择任务结束时间"  v-model="model.taskEndTime" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item label="描述" prop="description" v-bind="labelCol1">
                <j-editor v-model="model.description"/>
              </a-form-model-item>
            </a-col>

          </a-row>
        </a-form-model>
      </j-form-container>
    </a-spin>

  </j-modal>

</template>

<script>

import { httpAction } from '@api/manage'

export default {
  name: 'TaskAdd',
  data () {
    return {
      description: '添加页面',
      title: '',
      width: 960,
      visible: false,
      disableSubmit: false,
      model:{
      },
      // 1列
      labelCol1 : {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 2 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 21 },
        }
      },
      // 2列
      labelCol2 : {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 4 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 18 },
        }
      },
      confirmLoading: false,
      validatorRules: {
        taskName: [
          { required: true, message: '请输入任务名称!'},
        ],
        taskStartTime: [
          { required: true, message: '请输入任务开始时间!'},
        ],
        taskEndTime: [
          { required: true, message: '请输入任务结束时间!'},
        ],
        fileUrl: [
          { required: true, message: '请输入文件路径!'},
        ],
      },
      url: {
        add: "/smsCheck/task/add",
        edit: "/smsCheck/task/edit",
        queryById: "/smsCheck/task/queryById"
      }
    }
  },
  created () {
    // 备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
  },
  methods: {
    handleOk () {
      this.submitForm();
    },
    handleCancel () {
      this.visible = false;
    },
    add () {
      this.edit(this.modelDefault);
    },
    edit (record) {
      this.model = Object.assign({}, record);
      this.visible = true;
    },
    submitForm () {
      const that = this;
      // 触发表单验证
      this.$refs.form.validate(valid => {
        // 验证通过
        if (valid) {
          that.confirmLoading = true;
          let httpUrl = '';
          let method = '';
          if (!this.model.id) {
            httpUrl += this.url.add;
            method = 'post';
          } else {
            httpUrl += this.url.edit;
            method = 'put';
          }
          // 发送请求
          httpAction(httpUrl, this.model, method)
            .then((res) => {
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
                that.visible = false;
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
        }
      })
    },
  }
}
</script>

<style scoped>

</style>