<template>
  <div id="update">
    <el-row>
      <el-col :span="24" class="main">
        <el-col :span="24" class="top">
          <el-button type="primary" size="mini" @click="back">返回</el-button>
        </el-col>
        <el-col :span="24" class="down">
          <el-col :span="24" class="setp">
            <el-steps :active="active" align-center>
              <el-step title="（一）基本信息"> </el-step>
              <el-step title="（二）内容简介"> </el-step>
              <el-step title="（三）主研人员名单"></el-step>
              <el-step title="（四）评价委托方提供资料清单"></el-step>
            </el-steps>
          </el-col>
          <el-col :span="24" class="twoInfo">
            <basic :basicForm="form.basic" @resetBtn="back" @basicBtn="basicBtn" v-if="active == '1'"></basic>
            <brief :briefForm="form.brief" @briefUp="briefUp" @briefBtn="briefBtn" v-else-if="active == '2'"></brief>
            <research :researchForm="form.research" @researchUp="researchUp" @researchBtn="researchBtn" v-else-if="active == '3'"></research>
            <datalists :datalistForm="form.datalist" @detailedUp="detailedUp" @onSubmit="onSubmit" v-else-if="active == '4'"></datalists>
          </el-col>
        </el-col>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import basic from './updateParts/basic.vue';
import brief from './updateParts/brief.vue';
import research from './updateParts/research.vue';
import datalists from './updateParts/datalists.vue';
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: achieveApply } = createNamespacedHelpers('achieveApply');
export default {
  name: 'update',
  props: {},
  components: {
    basic,
    brief,
    research,
    datalists,
  },
  data: function() {
    return {
      // 步骤
      active: 1,
      form: {
        basic: {},
        brief: {},
        research: [],
        datalist: {},
      },
    };
  },
  async created() {
    console.log(this.user);
    await this.search();
  },
  methods: {
    ...achieveApply(['fetch', 'update', 'create']),
    async search() {
      if (this.id) {
        let res = await this.fetch(this.id);
        if (this.$checkRes(res)) {
          this.$set(this, `form`, res.data);
        }
      }
    },
    //基本信息下一步
    basicBtn() {
      this.active = 2;
    },
    // 内容简介上一步
    briefUp() {
      this.active = 1;
    },
    // 内容简介下一步
    briefBtn() {
      this.active = 3;
    },
    // 研发人员名单上一步
    researchUp() {
      this.active = 2;
    },
    // 研发人员名单下一步
    researchBtn({ data }) {
      this.$set(this.form, `research`, data);
      this.active = 4;
    },
    // 补充材料上一步
    detailedUp() {
      this.active = 3;
    },
    async onSubmit() {
      let data = this.form;
      data.status = this.status;
      if (data.id) {
        let res = await this.update(data);
        if (this.$checkRes(res)) {
          this.$message({
            message: '修改信息完成，等待审核',
            type: 'success',
          });
          this.back();
        }
      } else {
        data.user_id = this.user.id;
        let res = await this.create(data);
        if (this.$checkRes(res)) {
          this.$message({
            message: '申报完成，等待审核',
            type: 'success',
          });
          this.back();
        }
      }
    },
    // 返回
    back() {
      window.history.back();
    },
  },
  computed: {
    ...mapState(['user']),
    id() {
      return this.$route.query.id;
    },
    status() {
      return this.$route.query.status;
    },
  },
  metaInfo() {
    return { title: this.$route.meta.title };
  },
  watch: {
    test: {
      deep: true,
      immediate: true,
      handler(val) {},
    },
  },
};
</script>

<style lang="less" scoped>
.main {
  .top {
    text-align: right;
    margin: 0 0 10px 0;
  }
}
</style>
