<template>
  <a-card :bordered="false" :title="titleName">
    <a-form-model
      ref="trainForm"
      :rules="rules"
      :model="form"
      :layout="layout"
      :labelCol="{ span: 2 }"
      :wrapperCol="{ span: 10 }"
    >
      <a-form-model-item label="培训名称" prop="trainName">
        <a-input v-model="form.trainName" />
      </a-form-model-item>
      <a-form-model-item label="培训时间" style="margin-bottom: 0">
        <a-form-model-item :style="{ display: 'inline-block', width: 'calc(50% - 12px)' }" prop="trainSdate">
          <a-date-picker v-model="form.trainSdate" style="width: 100%" valueFormat="YYYY-MM-DD HH:mm:ss" />
        </a-form-model-item>
        <span :style="{ display: 'inline-block', width: '24px', textAlign: 'center' }"> - </span>
        <a-form-model-item :style="{ display: 'inline-block', width: 'calc(50% - 12px)' }" prop="trainEdate">
          <a-date-picker v-model="form.trainEdate" style="width: 100%" valueFormat="YYYY-MM-DD HH:mm:ss" />
        </a-form-model-item>
      </a-form-model-item>
      <a-form-model-item label="主持人" prop="host">
        <a-input v-model="form.host" />
      </a-form-model-item>
      <a-form-model-item label="会议人数" prop="personNum">
        <a-input-number v-model="form.personNum" :min="1"/>
      </a-form-model-item>
      <a-form-model-item label="学习内容" prop="learningContent">
        <a-input v-model="form.learningContent" type="textarea" />
      </a-form-model-item>
      <a-form-model-item label="联系人" prop="contact">
        <a-input v-model="form.contact" />
      </a-form-model-item>
      <a-form-model-item label="联系方式" prop="phone">
        <a-input v-model="form.phone" />
      </a-form-model-item>
      <a-form-model-item label="备注">
        <a-input v-model="form.remark" type="textarea" />
      </a-form-model-item>
      <a-form-model-item>
        <a-button
          type="primary"
          @click="
            () => {
              this.form.status = 1
              handleSubmit()
            }
          "
          style="border-left: 200px"
        >
          保存
        </a-button>
        <a-button
          type="primary"
          @click="
            () => {
              this.form.status = 2
              handleSubmit()
            }
          "
        >
          保存并发布
        </a-button>
      </a-form-model-item>
    </a-form-model>
  </a-card>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》'
import { terminalTrainEdit, terminalTrainGet } from '@/api/project/terminalTrain'

export default {
  //import引入的组件需要注入到对象中才能使用
  components: {
    terminalTrainEdit,
    terminalTrainGet,
  },
  props: {},
  data() {
    //这里存放数据
    return {
      form: {
        id: '',
        orgId: '',
        trainName: '',
        trainSdate: '',
        trainEdate: '',
        host: '',
        personNum: 1,
        learningContent: '',
        contact: '',
        phone: '',
        remark: '',
        status: 1,
      },
      titleName: '终端培训表单',
      layout: 'horizontal',
      rules: {
        trainName: [{ required: true, message: '培训名称不能为空', trigger: 'blur' }],
        trainSdate: [{ required: true, message: '培训开始时间不能为空', trigger: 'change' }],
        trainEdate: [{ required: true, message: '培训结束时间不能为空', trigger: 'change' }],
        host: [{ required: true, message: '主持人不能为空', trigger: 'blur' }],
        personNum: [{ required: true, message: '会议人数不能为空', trigger: 'blur' }],
        learningContent: [{ required: true, message: '学习内容不能为空', trigger: 'blur' }],
        contact: [{ required: true, message: '联系人不能为空', trigger: 'blur' }],
        phone: [{ required: true, message: '联系方式不能为空', trigger: 'blur' }],
      },
    }
  },
  //计算属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    handleSubmit(e) {
      this.$refs.trainForm.validate((valid) => {
        if (valid) {
          console.log('提交的数据', this.form)
          terminalTrainEdit(this.form).then((res) => {
            if (res.code == 200) {
              this.$message.success('保存成功！')
              this.$router.push({
                //跳转list页面
                path: '/project/project/terminalTrain/list',
                query: {
                  terminalTrainName: this.$route.query.terminalTrainName,
                  terminalTrainStatus: this.$route.query.terminalTrainStatus,
                  terminalTrainStartDate: this.$route.query.terminalTrainStartDate,
                  terminalTrainEndDate: this.$route.query.terminalTrainEndDate,
                  terminalTrainPageNum: this.$route.query.terminalTrainPageNum,
                },
              })
            } else {
              this.$message.error('保存失败：' + res.msg)
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
  },
  created() {
    console.log('1---', this.$store.state.user.info.name)
    if (this.$route.query.id) {
      this.form = terminalTrainGet(this.$route.query.id).then((res) => {
        if (res.code == 200) {
          this.form = res.data
        } else {
          this.$message.error('获取数据失败：' + res.msg)
        }
      })
    }
  }, //生命周期 - 创建完成（可以访问当前this实例）
  mounted() {}, //生命周期 - 挂载完成（可以访问DOM元素）
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
}
</script>
<style scoped>
</style>