<template>
  <div id="index">
    <el-row>
      <el-col :span="24">
        <el-col :span="24" class="leftTop"> <span>|</span> <span>我的发布</span> </el-col>
        <el-col :span="24" class="info">
          <el-col :span="24" class="infoOne" v-if="display">
            <el-tabs v-model="activeName" type="card">
              <el-tab-pane label="待发布" name="first">
                <stay @editBtn="editBtn"></stay>
              </el-tab-pane>
              <el-tab-pane label="审核中" name="second">
                <statusIn></statusIn>
              </el-tab-pane>
              <el-tab-pane label="已发布" name="third">
                <already></already>
              </el-tab-pane>
            </el-tabs>
            <el-button type="primary" size="mini" class="add" @click="add">信息发布</el-button>
          </el-col>
          <el-col :span="24" class="infoTwo" v-else>
            <infoRelease
              @back="back"
              :form="form"
              @draftBtn="draftBtn"
              @submitBtn="submitBtn"
              :addpatentform="addpatentform"
              @patentonSubmit="patentonSubmit"
              :diapatent="addpatent"
            ></infoRelease>
          </el-col>
        </el-col>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import stay from './part/stay.vue';
import statusIn from './part/statusIn.vue';
import already from './part/already.vue';
import infoRelease from './part/infoRelease.vue';
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: mapMarketproduct } = createNamespacedHelpers('marketproduct');
const { mapActions: market } = createNamespacedHelpers('market');
const { mapActions: expertsuser } = createNamespacedHelpers('expertsuser');
export default {
  name: 'index',
  props: {},
  components: {
    stay, //待发布
    statusIn, //审核中
    already, //已发布
    infoRelease, //信息发布
  },
  data: function() {
    return {
      activeName: 'first',
      // 发布信息
      display: true,
      form: {},
      // 企业信息&专家信息
      companyInfo: {},
      addpatentform: {},
      addpatent: [],
    };
  },
  created() {
    this.searchInfo();
  },
  methods: {
    ...mapMarketproduct({ productFetch: 'fetch', productCreate: 'create', productUpdate: 'update' }),
    ...market(['fetch', 'update']),
    ...expertsuser({ expertsuserFetch: 'fetch', expertsuserUpdate: 'update', expertsuserUpgrade: 'upgrade' }),
    // 查询信息
    async searchInfo() {
      if (this.user.role == '4' || this.user.role == '5') {
        let res = await this.fetch(this.user.userid);
        if (res.errcode === 0) {
          let message = {
            company: res.data.name,
            phone: res.data.phone,
            email: res.data.email,
            companytype: res.data.companytype,
            companydate: res.data.companydate,
            companycapital: res.data.companycapital,
            // contacts: '赵超',
            // qqwx: '0431-85262608',
            // achieveown: '中国科学院长春应用化学研究所',
            // field: '新材料技术',
          };
          console.log(message);
          this.$set(this, `form`, message);
          this.$set(this, `companyInfo`, res.data);
        }
      } else if (this.user.role == '6') {
        let res = await this.expertsuserFetch(this.user.userid);
        if (res.errcode === 0) {
          let message = {
            company: res.data.company,
            contacts: res.data.name,
            phone: res.data.phone,
            email: res.data.email,
            qqwx: res.data.qqwx,
          };
          this.$set(this, `form`, message);
          this.$set(this, `companyInfo`, res.data);
        }
      }
    },
    // 保存草稿
    async draftBtn({ data }) {
      if (data.id) {
        let res = await this.productUpdate(data);
        if (this.$checkRes(res)) {
          this.$message({
            message: '草稿修改成功',
            type: 'success',
          });
          window.location.reload();
        } else {
          this.$message.error('草稿保存失败');
        }
      } else {
        data.patent = this.addpatent;
        data.userid = this.user.uid;
        let res = await this.productCreate(data);
        if (this.$checkRes(res)) {
          this.$message({
            message: '草稿创建成功',
            type: 'success',
          });
          window.location.reload();
        } else {
          this.$message.error('草稿创建失败');
        }
      }
    },
    // 信息发布
    async submitBtn({ data }) {
      if (data.id) {
        data.status = '0';
        data.userid = this.user.uid;
        let res = await this.productUpdate(data);
        if (this.$checkRes(res)) {
          this.$message({
            message: '信息发布成功',
            type: 'success',
          });
          window.location.reload();
        } else {
          this.$message.error('信息发布失败');
        }
      } else {
        data.status = '0';
        data.userid = this.user.uid;
        let res = await this.productCreate(data);
        if (this.$checkRes(res)) {
          this.$message({
            message: '信息发布成功',
            type: 'success',
          });
          window.location.reload();
        } else {
          this.$message.error('信息发布失败');
        }
      }
    },
    // 信息发布
    add() {
      this.display = false;
    },
    // 返回
    back() {
      this.display = true;
    },
    // 修改信息
    editBtn(data) {
      this.$set(this, `form`, data);
      this.$set(this, `product_args`, data.product_args);
      this.display = false;
    },
    // 添加专利信息
    patentonSubmit({ data }) {
      this.addpatentform = {};
      this.addpatent.push(data);
      this.$message({
        message: '专利信息添加成功',
        type: 'success',
      });
    },
  },
  computed: {
    ...mapState(['user']),
    pageTitle() {
      return `${this.$route.meta.title}`;
    },
  },
  metaInfo() {
    return { title: this.$route.meta.title };
  },
};
</script>

<style lang="less" scoped>
.leftTop {
  font-size: 18px;
  width: 96%;
  height: 41px;
  line-height: 35px;
  border-bottom: 1px solid #e5e5e5;
  position: relative;
  bottom: 1px;
  margin: 10px;
  font-weight: 600;
  color: #22529a;
}
.info {
  padding: 0 40px 0 10px;
  .infoOne {
    position: relative;
    .add {
      position: absolute;
      top: 0;
      right: 0;
    }
  }
}
</style>
