<template>
  <div>
    <Modal v-model="showEditModal" v-drag :mask-closable="true" width="1000px">
      <p slot="header">
        {{ modalTitle }}
      </p>
      <div>
        <Form
          id="edit-form-train-record-edit"
          ref="formObj"
          :model="formObj"
          :rules="ruleValidate"
          :label-width="80"
          inline
        >
          <Form-item label="培训主题" prop="trainTopic" class="width-32">
            <Input v-model="formObj.trainTopic" placeholder="请输入培训主题" clearable /></Input>
          </Form-item>
          <Form-item label="培训内容" prop="trainContent" class="width-32">
            <Input v-model="formObj.trainContent" name="trainContent" placeholder="请输入培训内容" clearable /></Input>
          </Form-item>
          <Form-item label="培训对象" prop="trainObjects" class="width-32">
            <Input v-model="formObj.trainObjects" placeholder="请输入培训对象" /></Input>
          </Form-item>
          <Form-item label="人数" prop="personQuantity" class="width-32">
            <InputNumber
              v-model="formObj.personQuantity"
              :min="0"
              :step="1"
              name="personQuantity"
              style="width:100%"
              placeholder="请输入人数"
            />
          </Form-item>
          <Form-item label="学时" prop="learnHour" class="width-32">
            <InputNumber
              v-model="formObj.learnHour"
              :min="0"
              :step="1"
              name="learnHour"
              style="width:100%"
              placeholder="请输入学时"
            />
          </Form-item>
          <Form-item label="授课机构或人员" prop="teachParty" class="width-32">
            <Input v-model="formObj.teachParty" name="teachParty" placeholder="请输入授课机构或人员" /></Input>
          </Form-item>
          <Form-item label="实施开始时间" prop="stime" class="width-32">
            <Date-picker
              v-model="formObj.stime"
              type="datetime"
              placeholder="实施开始时间"
              format="yyyy-MM-dd HH:mm"
              style="width: 100%;"
              @on-change="_stimeChange"
            />
            <input name="stime" type="hidden">
          </Form-item>
          <Form-item label="实施结束时间" prop="etime" class="width-32">
            <Date-picker
              v-model="formObj.etime"
              type="datetime"
              placeholder="实施结束时间"
              format="yyyy-MM-dd HH:mm"
              style="width: 100%;"
              @on-change="_etimeChange"
            />
            <input name="etime" type="hidden">
          </Form-item>
          <Form-item label="培训形式" prop="trainForm" class="width-32">
            <Input v-model="formObj.trainForm" name="trainForm" placeholder="请输入培训形式" /></Input>
          </Form-item>
          <Form-item label="所属培训计划" prop="trainId" class="width-32">
            <Input
              v-model="formObj.trainName"
              placeholder="请选择所属培训计划"
              icon="plus-circled"
              readonly
              @click.native="_selectPlan"
            /></Input>
            <input v-model="formObj.trainId" type="hidden" name="trainId">
          </Form-item>
          <Form-item label="评价方式" prop="evaluateForm" class="width-32">
            <Input v-model="formObj.evaluateForm" name="evaluateForm" placeholder="请输入评价方式" /></Input>
          </Form-item>
          <Form-item label="备注" prop="remark" class="width-32">
            <Input v-model="formObj.remark" placeholder="请输入备注" /></Input>
          </Form-item>
          <Form-item label="培训目标/目的" prop="contentAim" class="width-32">
            <Input v-model="formObj.contentAim" placeholder="请输入培训目标/目的" /></Input>
          </Form-item>
          <Form-item label="培训地点" prop="trainArea" class="width-32">
            <Input v-model="formObj.trainArea" placeholder="请输入培训地点" /></Input>
          </Form-item>
        </Form>
      </div>
      <div slot="footer">
        <modal-footer ref="footerModal" :footer="footerList" @on-result-change="_footerResult" />
      </div>
    </Modal>

    <TrainPlan ref="trainPlanModal" is-change @on-result-change="_backTrainPlanData" />
  </div>
</template>
<script>
/**
 * 添加编辑
 */
import { sampleType, staffTrainRecord } from '../../../api'
import TrainPlan from './TrainPlan'
export default {
  components: { TrainPlan },
  data() {
    return {
      id: '',
      modalTitle: '添加',
      formObj: {
        trainTopic: '', // 培训主题
        trainContent: '',
        trainObjects: '',
        personQuantity: 0,
        learnHour: 0,
        teachParty: '',
        stime: '',
        etime: '',
        trainForm: '',
        trainName: '',
        trainId: '',
        evaluateForm: '',
        remark: '',
        contentAim: '',
        trainArea: '',
        lmsStaffFiles: []
      },
      ruleValidate: {
        trainTopic: [
          { required: true, message: '培训主题不能为空', trigger: 'blur' }
        ],
        trainId: [
          { required: true, message: '所属培训计划不能为空', trigger: 'change' }
        ]
      },
      showEditModal: false,
      footerList: [
        { id: '', name: '取消', type: '' },
        { id: '', name: '草稿', type: 'primary' },
        { id: '', name: '提交', type: 'primary' }
      ]
    }
  },
  methods: {
    // modal
    _footerResult(name) {
      switch (name) {
        case '取消':
          this._cancel()
          break
        case '草稿':
          this._draft()
          break
        case '提交':
          this._ok()
          break
      }
    },
    _hideLoading() {
      this.$refs.footerModal._hideLoading()
    },
    _resultChange(result, msg) {
      if (result) {
        this.showEditModal = false
        this.$Message.success(msg)
        this.$emit('on-result-change')
      }
      this._hideLoading()
    },
    _selectPlan() {
      this.$refs.trainPlanModal._open()
    },

    _backTrainPlanData(data) {
      this.formObj.trainPlan = data.name
      this.formObj.trainId = data.id
    },
    _ok() {
      this.$refs.formObj.validate(valid => {
        if (valid) {
          const data = this.$serialize('edit-form-train-record-edit')
          data.type = this.formObj.type
          this._save(data)
        } else {
          this.$Message.error('表单验证失败')
          this._hideLoading()
        }
      })
    },

    _draft() {
      this.$refs.formObj.validate(valid => {
        if (valid) {
          const data = this.$serialize('edit-form-train-record-edit')
          console.log('---------------', data)
          console.log('***************', this.formObj)
          // data.type = this.formObj.type
          this._saveDraft(this.formObj)
        } else {
          this.$Message.error('表单验证失败')
          this._hideLoading()
        }
      })
    },
    _saveDraft: async function(data) {
      if (data.id === undefined || data.id === '') {
        const result = await staffTrainRecord.add(data)
        this._resultChange(result, '保存成功')
      } else {
        const result = await staffTrainRecord.edit({ id: data.id, obj: data })
        this._resultChange(result, '保存成功')
      }
    },
    // 添加、编辑保存
    _save: async function(data) {
      // 添加
      if (this.$string(this.id).isEmpty()) {
        const result = await sampleType.add(data)
        this._resultChange(result, '添加成功')
      } else {
        // 编辑
        const result = await sampleType.edit({
          id: this.formObj.id,
          obj: data
        })
        this._resultChange(result, '编辑成功')
      }
    },
    _cancel() {
      this.showEditModal = false
    },
    _open(formObj) {
      this.$refs.formObj.resetFields()
      this.showEditModal = true
      this._hideLoading()
      if (this.$string(formObj).isEmpty()) {
        this.id = ''
        this.modalTitle = '添加人员培训记录'
      } else {
        this.id = formObj.id
        this.formObj = formObj
        this.modalTitle = '编辑人员培训记录'
        if (undefined !== formObj.stime) {
          this.formObj.stime = this.$dateformat(
            new Date(formObj.stime),
            'yyyy-mm-dd HH:MM:ss'
          )
        } else {
          this.formObj.stime = ''
        }

        if (undefined !== formObj.etime) {
          this.formObj.etime = this.$dateformat(
            new Date(formObj.etime),
            'yyyy-mm-dd HH:MM:ss'
          )
        } else {
          this.formObj.etime = ''
        }
      }
    }
  }
}
</script>
