<template>
  <div id="detail">
    <el-row>
      <el-col :span="24" class="main">
        <el-col :span="24" class="one">
          <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-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>
      </el-col>
    </el-row>
  </div>
</template>

<script>
const _ = require('lodash');
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: ticket } = createNamespacedHelpers('ticket');
export default {
  name: 'detail',
  props: {},
  components: {},
  data: function () {
    return {
      form: {
        qyfr: [],
        yyzz: [],
        qylr: [],
      },
      rules: {},
    };
  },
  async created() {
    if (this.id) await this.search();
  },
  methods: {
    ...ticket(['fetch', 'create', 'update']),
    async search() {
      let res = await this.fetch(this.id);
      if (this.$checkRes(res)) {
        console.log(res.data);
        // this.$set(this, `form`, res.data);
      }
    },
    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;
          if (obj.id) {
            console.log('修改');
          } else {
            let res = await this.create(obj);
            if (this.$checkRes(res)) {
              this.$message({
                message: '创新券服务申报成功，请耐心等待结果',
                type: 'success',
              });
              this.back();
            }
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    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;
    },
    // 返回
    back() {
      this.$router.push({ path: '/adminCenter/company/ticket' });
    },
  },
  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 {
  border-radius: 10px;
  box-shadow: 0 0 5px #cccccc;
  padding: 20px;
  .one {
    .top {
      text-align: right;
      margin: 0 0 15px 0;
    }
  }
}
.main:hover {
  box-shadow: 0 0 5px #409eff;
}
</style>
