<template>
  <div id="detail">
    <el-row>
      <el-col :span="24">
        <el-col :span="24" class="top">
          <detailTopInfo :topTitle="topTitle" :display="display" @goBack="goBack"></detailTopInfo>
        </el-col>
        <el-col :span="24" class="main">
          <technicalServiceForm :ruleForm="ruleForm" @submitForm="submitForm" @resetForm="resetForm" :loading="loading"></technicalServiceForm>
        </el-col>
      </el-col>
    </el-row>
  </div>
</template>

<script>
  import detailTopInfo from '@/layout/custom/detailTopInfo.vue';
  import technicalServiceForm from '@/layout/technicalService/technicalServiceForm.vue';
  import { createNamespacedHelpers, mapGetters } from 'vuex';
  const { mapActions: tDigitalService } = createNamespacedHelpers('tDigitalService');

  export default {
    name: 'detail',
    props: {},
    components: {
      detailTopInfo, //头部导航
      technicalServiceForm,
    },
    data: () => ({
      display: 'block',
      topTitle: '技术服务管理',
      ruleForm: {},
      loading: true,
      type:'3',
    }),
    created() {
      this.select();
    },
    computed: {
      id() {
        return this.$route.query.id;
      },
    },
    methods: {
      ...tDigitalService(['create', 'delete', 'update', 'fetch', 'query']),
      // 返回
      goBack() {
        this.$router.go(-1);
      },
      async select() {
        this.$set(this, `loading`, false);
        if (this.id) {
          const res = await this.fetch(this.id);
          this.$set(this, `ruleForm`, res.data);
        }
        this.$set(this, `loading`, true);
      },
      // 提交
      async submitForm({ data }) {
        if(!data.image){
          this.$message.error('请上传图片');
          return ;
        }
        let res;
        if (this.id) {
          res = await this.update(data);
          if (res.errcode === 0) {
            this.$message({
              message: '信息修改成功',
              type: 'success',
            });
          }
        } else {
          res = await this.create({type:this.type,...data});
          if (res.errcode === 0) {
            this.$message({
              message: '信息创建成功',
              type: 'success',
            });
          }
        }
        if (this.$checkRes(res)) this.resetForm();
      },
      // 取消
      resetForm() {
        this.$router.push({ path: '/technicalService/index' });
      },
    },
  };
</script>

<style lang="less" scoped>
  .top {
    height: 50px;
    margin: 0 0 10px 0;
  }
  .main {
    min-height: 765px;
    background: #ffffff;
    padding: 20px;
  }
</style>
