<template>
  <div class="app-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>{{ this.handbook.handbookName }}</span>
      </div>

      <el-col :span="18" :offset="3">
        <div class="form-conf" v-if="formOpen">
          <parser :key="new Date().getTime()" :form-conf="formData" @submit="submit" ref="parser" @getData="getData"/>
        </div>
      </el-col>
    </el-card>
  </div>
</template>

<script>
import { getProcessForm, startProcess_ } from '@/api/workflow/process'
import Parser from '@/utils/generator/parser'
import {addProject} from "@/api/scientific/project";
import { addApplication } from '@/api/scientific/application'

export default {
  name: 'WorkStart',
  components: {
    Parser
  },
  data() {
    return {
      definitionId: null,
      deployId: null,

      formOpen: false,
      formData: {},

      handbook: {},
    }
  },
  created() {
    this.initData();
  },
  methods: {
    initData() {
      this.deployId = this.$route.params && this.$route.params.deployId;
      this.definitionId = this.$route.params && this.$route.params.definitionId;
      this.handbook = this.$route.params && this.$route.params.row;

      getProcessForm({
        definitionId: this.definitionId,
        deployId: this.deployId,
        procInsId: undefined,
      }).then(res => {
        if (res.data) {
          this.formData = res.data;
          this.formOpen = true
        }
      })
    },
    /** 接收子组件传的值 */
    getData(data) {
      if (data) {
        const variables = [];
        data.fields.forEach(item => {
          let variableData = {};
          variableData.label = item.__config__.label
          // 表单值为多个选项时
          if (item.__config__.defaultValue instanceof Array) {
            const array = [];
            item.__config__.defaultValue.forEach(val => {
              array.push(val)
            })
            variableData.val = array;
          } else {
            variableData.val = item.__config__.defaultValue
          }
          variables.push(variableData)
        })
        this.variables = variables;
      }
    },
    submit(data) {
      if (data && this.definitionId) {
        // 启动流程并将表单数据加入流程变量
        console.log(data);
        startProcess_(this.definitionId, JSON.stringify(data.valData)).then(res => {
          const ori_data = data.valData;
          // zqjia:项目申报表单确定后这些信息要改
          let projectData = {};
          projectData.projectName = ori_data.projectName;
          projectData.projectStatus = 0;
          projectData.projectAppProcId = res.msg;
          projectData.projectDeclareStatus = 1;
          projectData.projectAppStTime = this.formatDateTime(new Date());
          projectData.projectAppEndTime = this.formatDateTime(new Date());
          projectData.projectLeader = "liye";
          projectData.projectBudget = 11;
          projectData.projectHandbookId = this.handbook.handbookId;
          projectData.projectHandbookName = this.handbook.handbookName;
          projectData.projectLeaderPhone = ori_data.projectLeaderPhone;
          projectData.projectCategory = this.handbook.handbookLevel;
          projectData.projectContent = ori_data.projectContent;

          console.log(projectData);

          addApplication(JSON.stringify(projectData)).then(response => {
            this.$modal.msgSuccess("申报完成");
          })

          this.$tab.closeOpenPage({
            path: '/work/own'
          })
        })
      }
    },

    formatDateTime(date) {
      const padZero = (num) => (num < 10 ? '0' + num : num);

      const year = date.getFullYear();
      const month = padZero(date.getMonth() + 1); // getMonth() 返回值的范围是 0-11，所以需要加 1
      const day = padZero(date.getDate());
      const hours = padZero(date.getHours());
      const minutes = padZero(date.getMinutes());
      const seconds = padZero(date.getSeconds());

      return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
    }
  }
}
</script>

<style lang="scss" scoped>
.form-conf {
  margin: 15px auto;
  width: 80%;
  padding: 15px;
}
</style>
