<template>
  <div id="update">
    <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-col :span="24" class="name">
                <el-form-item label="企业名称" prop="name">
                  <el-input v-model="form.name" disabled></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="24" class="down_1">
                <el-col :span="24" class="down_1text"><span>企业资质资料</span></el-col>
                <el-col :span="24" class="file">
                  <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-col>
                <el-col :span="24" class="file">
                  <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-col>
                <el-col :span="24" class="file">
                  <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>
              </el-col>
              <el-col :span="24" class="down_1" v-if="form.status != '-1'">
                <el-col :span="24" class="down_1text"><span>企业准备订单</span></el-col>
                <el-col :span="24" class="file">
                  <el-form-item label="选择中介">
                    <el-select v-model="form.mechanism_id" placeholder="">
                      <el-option v-for="item in mechanismList" :key="item.id" :label="item.name" :value="item.id"> </el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="24" class="file">
                  <el-form-item label="法人复印件" prop="qyfr1">
                    <e-upload url="/files/cysci/qyfr1_file/upload" :limit="1" v-model="form.qyfr1" type="text"></e-upload>
                  </el-form-item>
                </el-col>
                <el-col :span="24" class="file">
                  <el-form-item label="企业营业执照" prop="yyzz1">
                    <e-upload url="/files/cysci/yyzz1_file/upload" :limit="1" v-model="form.yyzz1" type="text"></e-upload>
                  </el-form-item>
                </el-col>
                <el-col :span="24" class="file">
                  <el-form-item label="企业利润表" prop="qylr1">
                    <e-upload url="/files/cysci/qylr1_file/upload" :limit="1" v-model="form.qylr1" type="text"></e-upload>
                  </el-form-item>
                </el-col>
              </el-col>
              <el-col :span="24" class="down_1" v-if="form.status == '3'">
                <el-col :span="24" class="down_1text"><span>合同资料上传</span></el-col>
                <el-col :span="24" class="file">
                  <el-form-item label="上传合同" prop="contract">
                    <e-upload
                      url="/files/cysci/contract_file/upload"
                      :limit="2"
                      v-model="form.contract"
                      type="text"
                      :remove="fileRemove"
                      :extra="{ user_id: user._id }"
                    ></e-upload>
                  </el-form-item>
                </el-col>
              </el-col>
              <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');
const { mapActions: mechanism } = createNamespacedHelpers('mechanism');
export default {
  name: 'update',
  props: {},
  components: {},
  data: function () {
    return {
      form: {},
      rules: {},
      // 中介机构
      mechanismList: [],
    };
  },
  async created() {
    await this.searchOther();
    if (this.id) await this.search();
  },
  methods: {
    ...ticket(['fetch', 'update']),
    ...mechanism(['query']),
    async search() {
      let res = await this.fetch(this.id);
      if (this.$checkRes(res)) {
        res.data.qyfr = res.data.material.length > 0 ? [res.data.material[0]] : [];
        res.data.yyzz = res.data.material.length > 0 ? [res.data.material[1]] : [];
        res.data.qylr = res.data.material.length > 0 ? [res.data.material[2]] : [];
        res.data.qyfr1 = res.data.medium_material.length > 0 ? [res.data.medium_material[0]] : [];
        res.data.yyzz1 = res.data.medium_material.length > 0 ? [res.data.medium_material[1]] : [];
        res.data.qylr1 = res.data.medium_material.length > 0 ? [res.data.medium_material[2]] : [];
        this.$set(this, `form`, res.data);
      }
    },
    onSubmit(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          if (this.status == '-1') {
            this.oneSubmit();
          } else if (this.status == '1') {
            this.twoSubmit();
          } else if (this.status == '-3') {
            this.thrSubmit();
          } else if (this.status == '3') {
            this.fourSubmit();
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    // 状态为-1时
    async oneSubmit() {
      const material = this.oneMap();
      if (material.length < 3) {
        this.$message.error('缺少所需资料');
        return;
      }
      const data = this.form;
      data.material = material;
      data.status = '0';
      let res = await this.update(data);
      if (this.$checkRes(res)) {
        this.$message({
          message: '创新券重申成功，请耐心等待结果',
          type: 'success',
        });
        this.back();
      }
    },
    // 状态为1时
    async twoSubmit() {
      const material = this.oneMap();
      const medium_material = this.twoMap();
      if (material.length < 3) {
        this.$message.error('缺少所需资料');
        return;
      }
      if (medium_material.length < 3) {
        this.$message.error('缺少所需资料');
        return;
      }
      const data = this.form;
      data.material = material;
      data.medium_material = medium_material;
      data.status = '2';
      let res = await this.update(data);
      if (this.$checkRes(res)) {
        this.$message({
          message: '创新券信息资料提交成功，请耐心等待结果',
          type: 'success',
        });
        this.back();
      }
    },
    // 状态为-3时
    async thrSubmit() {
      const material = this.oneMap();
      const medium_material = this.twoMap();
      if (material.length < 3) {
        this.$message.error('缺少所需资料');
        return;
      }
      if (medium_material.length < 3) {
        this.$message.error('缺少所需资料');
        return;
      }
      const data = this.form;
      data.material = material;
      data.medium_material = medium_material;
      data.status = '2';
      let res = await this.update(data);
      if (this.$checkRes(res)) {
        this.$message({
          message: '创新券信息资料提交成功，请耐心等待结果',
          type: 'success',
        });
        this.back();
      }
    },
    // 状态为3时
    async fourSubmit() {
      const material = this.oneMap();
      const medium_material = this.twoMap();
      if (material.length < 3) {
        this.$message.error('缺少所需资料');
        return;
      }
      if (medium_material.length < 3) {
        this.$message.error('缺少所需资料');
        return;
      }
      const data = this.form;
      data.material = material;
      data.medium_material = medium_material;
      data.status = '3';
      let res = await this.update(data);
      if (this.$checkRes(res)) {
        this.$message({
          message: '创新券合同资料提交成功，请耐心等待结果',
          type: 'success',
        });
        this.back();
      }
    },
    oneMap() {
      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;
    },
    twoMap() {
      let data = _.cloneDeep(this.form);
      const arr = ['qyfr1', 'yyzz1', 'qylr1'];
      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' });
    },
    // 查询其他
    async searchOther() {
      let res = await this.query();
      if (this.$checkRes(res)) {
        this.$set(this, `mechanismList`, res.data);
      }
    },
    // 删除合同之前判断
    fileRemove(file) {
      if (file.user_id === this.user._id) {
        return { result: true };
      } else {
        return { result: false, msg: '不允许删除自己上传以外的合同' };
      }
    },
  },
  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;
    }
    .down {
      .name {
        border-bottom: 1px dashed #ccc;
        margin: 0 0 10px 0;
      }
      .down_1 {
        border-bottom: 1px dashed #ccc;
        margin: 0 0 10px 0;
        .down_1text {
          font-weight: bold;
          margin: 0 0 10px 0;
          span {
            display: inline-block;
            background-color: #242f42;
            color: #fff;
            padding: 8px;
            border-radius: 10px;
          }
        }
        .file {
          border: 1px dashed #ccc;
          padding: 10px 50% 10px 0;
        }
      }
      .formBtn {
        text-align: center;
      }
    }
  }
}
.main:hover {
  box-shadow: 0 0 5px #409eff;
}
</style>
