<template>
  <div style="padding: 0 20px;">
    <lx-header
      icon="md-document"
      style="margin-bottom: 10px; border: 0px; margin-top: 15px"
      :text="(id ? '编辑' : '新增') + '出差申请'"
    >
    </lx-header>
    <el-card>
      <el-form :model="form" :rules="rules" ref="form" label-width="120px">
        <el-form-item label="标题" prop="title">
          <el-input clearable placeholder="请输入标题" v-model="form.title" style="max-width: 300px;width: 100%;"></el-input>
        </el-form-item>
        <el-form-item label="是否用车" prop="use_car">
          <el-radio-group v-model="form.use_car">
            <el-radio :label="1">是</el-radio>
            <el-radio :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="开始时间" prop="start_date">
          <el-date-picker value-format="yyyy-MM-dd" clearable placeholder="请选择开始时间" style="max-width: 300px;width: 100%;" v-model="form.start_date"></el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="end_date">
          <el-date-picker value-format="yyyy-MM-dd"
                          clearable
                          placeholder="请选择结束时间"
                          style="max-width: 300px;width: 100%;"
                          v-model="form.end_date"></el-date-picker>
        </el-form-item>
        <el-form-item label="是否室内补助" prop="is_subsidize">
          <el-radio-group v-model="form.is_subsidize">
            <el-radio :label="1">是</el-radio>
            <el-radio :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item align="center">
          <el-button type="warning" plain @click="reset">重置</el-button>
          <el-button :loading="loading" type="primary" @click="submit">提交</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { deepCopy } from "@/utils";
import { save, show } from "@/api/away"
export default {
  data() {
    return {
      id: "",
      loading: false,
      form: {
        title: '',
        use_car: 0,
        start_date: '',
        end_date: '',
        is_subsidize: 0,
      },
      copyForm: {

      },
      rules: {
        title: [
          { required: true, message: '请输入标题', trigger: 'blur' },
        ],
        start_date: [
          { required: true, message: '请选择开始时间', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              if (this.form.end_date && this.form.start_date) {
                if (this.$moment(this.form.end_date).isAfter(this.form.start_date)) {
                  callback()
                } else {
                  callback(new Error('结束时间不能小于开始时间'))
                }
              } else {
                callback()
              }
            }
          }
        ],
        end_date: [
          { required: true, message: '请选择结束时间', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              if (this.form.end_date && this.form.start_date) {
                if (this.$moment(this.form.end_date).isAfter(this.form.start_date)) {
                  callback()
                } else {
                  callback(new Error('结束时间不能小于开始时间'))
                }
              } else {
                callback()
              }
            }
          }
        ],
      }
    }
  },
  methods: {
    init () {
      this.copyForm = deepCopy(this.form)
    },

    reset () {
      this.form = deepCopy(this.copyForm)
      this.$nextTick(() => this.$refs.form.clearValidate())
    },

    submit () {
      this.loading = true
      this.$refs.form.validate((valid) => {
        if (valid) {
          save(this.form,true).then(res => {
            this.loading = false
            this.$message({
              message: '提交成功',
              type:'success'
            })
            setTimeout(() => {
              this.$router.push("/contract/away")
            },500)
          }).catch(err => {
            this.loading = false
          })
        } else {
          return false
        }
      })
    }
  },
  computed: {},
  created() {
    this.init();
  }
}
</script>

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