<template>
  <div id="ticket">
    <el-row>
      <el-col :span="24" class="main">
        <div class="w_1200">
          <el-col :span="24" class="one">
            <el-col :span="24" class="btn">
              <el-button type="primary" size="mini" @click="back">返回</el-button>
            </el-col>
            <el-col :span="24" class="text">环南湖科创先导区双创服务平台-创新券服务</el-col>
            <el-col :span="24" class="dwon">
              <el-form :model="form" :rules="rules" ref="form" label-width="100px">
                <el-form-item label="法人复印件" prop="qyfr">
                  <e-upload url="/files/cysci/qyfr_file/upload" :limit="1" v-model="form.qyfr" type="text"></e-upload>
                </el-form-item>
                <el-form-item label="企业营业执照" prop="yyzz">
                  <e-upload url="/files/cysci/yyzz_file/upload" :limit="1" v-model="form.yyzz" type="text"></e-upload>
                </el-form-item>
                <el-form-item label="企业利润表" prop="qylr">
                  <e-upload url="/files/cysci/qylr_file/upload" :limit="1" v-model="form.qylr" type="text"></e-upload>
                </el-form-item>
                <el-col :span="24" class="formBtn">
                  <el-button type="danger" size="mini" @click="back">取消申请</el-button>
                  <el-button type="primary" size="mini" @click="onSubmit('form')">提交申请</el-button>
                </el-col>
              </el-form>
            </el-col>
          </el-col>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
const _ = require('lodash');
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: ticket } = createNamespacedHelpers('ticket');
export default {
  name: 'ticket',
  props: {},
  components: {},
  data: function () {
    return {
      form: {
        qyfr: [],
        yyzz: [],
        qylr: [],
      },
      rules: {},
    };
  },
  created() {},
  methods: {
    ...ticket(['create']),
    onSubmit(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          const obj = { user_id: this.user._id };
          const material = this.resetData();
          if (material.length < 3) {
            this.$message.error('缺少所需资料');
            return;
          }
          obj.material = material;
          let res = await this.create(obj);
          if (this.$checkRes(res)) {
            this.$message({
              message: '创新券服务申报成功，请耐心等待结果',
              type: 'success',
            });
            // 去中台
            this.$router.push({ path: '/adminCenter/homeIndex' });
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    back() {
      this.$router.push({ path: '/service/index' });
    },
    resetData() {
      let data = _.cloneDeep(this.form);
      const arr = ['qyfr', 'yyzz', 'qylr'];
      const res = [];
      for (const key of arr) {
        const e = data[key];
        if (e.length <= 0) continue;
        const head = _.head(e);
        res.push(head);
      }
      return res;
    },
  },
  computed: {
    ...mapState(['user', 'menuParams']),
    pageTitle() {
      return `${this.$route.meta.title}`;
    },
  },
  metaInfo() {
    return { title: this.$route.meta.title };
  },
};
</script>

<style lang="less" scoped>
.main {
  min-height: 500px;
  margin: 10px 0;
  .one {
    min-height: 500px;
    border-radius: 5px;
    border: 1px solid #ccc;
    padding: 0 10px;
    .btn {
      padding: 10px;
      text-align: right;
    }
    .text {
      font-size: 30px;
      text-align: center;
      margin: 0 0 10px 0;
    }
    .dwon {
      padding: 10px;
      border: 1px dashed #ccc;
      border-radius: 5px;
      .formBtn {
        text-align: center;
      }
    }
  }
}
</style>
