<template>
  <div id="xmdj">
    <el-row>
      <el-col :span="24" class="main">
        <el-col :span="24" class="one">
          <div class="w_1200">
            <el-col :span="20" class="left">
              <el-col :span="24" class="leftone">
                <span>科技成果展洽会</span>
                <span>欢迎您</span>
              </el-col>
              <el-col :span="24" class="lefttwo">
                SCIENTIFIC AND TECHNOLOGICAL ACHIEVEEMENTS EXHIBITION
              </el-col>
            </el-col>
            <el-col :span="4" class="right">
              <el-link :underline="false">帮助中心</el-link>
            </el-col>
          </div>
        </el-col>
        <el-col :span="24" class="two">
          <div class="w_1200">
            <el-col :span="24" class="twotop">
              <el-col :span="18" class="left">
                <p>{{ dockInfo.title }}</p>
                <el-image :src="two1"></el-image>
              </el-col>
              <el-col :span="6" class="right">
                <el-image :src="two2"></el-image>
                <p>{{ dockInfo.start_time }}</p>
                <p>距离展会结束还有</p>
                <p>{{ djs }}</p>
              </el-col>
            </el-col>
            <el-col :span="24" class="twocen">
              <el-col :span="15" class="left"> <span>主办单位：</span>{{ dockInfo.sponsor }} </el-col>
              <el-col :span="8" class="right">
                <el-button class="btn1" @click="$router.push({ path: '/halltwo/directTwo', query: { id: dockInfo.id } })">返回展会进行页</el-button>
                <el-button class="btn2" @click="$router.push({ path: '/halltwo/detail', query: { dock_id: dockInfo.id } })">进入展会总结页</el-button>
              </el-col>
            </el-col>
          </div>
        </el-col>
        <el-col :span="24" class="three">
          <div class="w_1200">
            <el-col :span="24" class="threeinfo">
              <el-col :span="4" class="left">
                <el-col
                  :span="24"
                  v-for="(item, index) in menuList"
                  :key="index"
                  @click.native="changeMenu(item.name, index)"
                  :class="menuIndex == index ? 'indexClass' : ''"
                  class="menuList"
                >
                  <i :class="item.url"></i>
                  <span>{{ item.name }}</span>
                </el-col>
              </el-col>
              <el-col :span="20" class="right">
                <span v-if="menuIndex == '0'">
                  <span v-if="type == '0'">
                    <xmdjtechol :detailInfo="detailInfo"></xmdjtechol>
                  </span>
                  <span v-else-if="type == '1'">
                    <xmdjachieve :detailInfo="detailInfo"></xmdjachieve>
                  </span>
                  <span v-else-if="type == '2'">
                    商务信息
                  </span>
                  <span v-else>
                    <xmdjaexpert :detailInfo="detailInfo"></xmdjaexpert>
                  </span>
                </span>
                <span v-else-if="menuIndex == '1'">
                  <el-col :span="12" class="productImage" v-for="(item, index) in detailInfo.image" :key="index">
                    <el-image :src="item.url"></el-image>
                  </el-col>
                </span>
                <span v-else>
                  <xmdjchat v-if="room.id" :room="room" @onSubmit="onSubmit"></xmdjchat>
                </span>
              </el-col>
            </el-col>
          </div>
        </el-col>
        <el-col :span="24" class="four">
          <div class="w_1200">
            <ywlx></ywlx>
          </div>
        </el-col>
      </el-col>
      <div class="pz_down">
        <live-foot></live-foot>
      </div>
    </el-row>
  </div>
</template>

<script>
import ywlx from './parts/ywlx.vue';
import liveFoot from '@/layout/live/foot.vue';
import xmdjtechol from './parts/xmdjtechol.vue';
import xmdjachieve from './parts/xmdjachieve.vue';
import xmdjaexpert from './parts/xmdjaexpert.vue';
import xmdjchat from './parts/xmdjchat.vue';
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: dock } = createNamespacedHelpers('dock');
const { mapActions: transaction } = createNamespacedHelpers('transaction');
const { mapActions: expertsuser } = createNamespacedHelpers('expertsuser');
const { mapActions: personalRoom } = createNamespacedHelpers('personalroom');
var moment = require('moment');
export default {
  metaInfo() {
    return { title: this.$route.meta.title };
  },
  name: 'xmdj',
  props: {},
  components: {
    liveFoot,
    ywlx,
    // 技术对接
    xmdjtechol,
    // 成果对接
    xmdjachieve,
    // 对接聊天,
    xmdjchat,
    // 专家对接
    xmdjaexpert,
  },
  data: function() {
    return {
      two1: require('@a/sy_04.png'),
      two2: require('@a/dock2.png'),
      // 展會詳情
      dockInfo: {},
      // 指导单位
      erweima: require('@a/二维码.jpg'),
      // 产品列表
      productList: [],
      // 产品详情
      detailInfo: {},
      // 显示按钮
      displayBtn: true,
      // 倒计时
      djs: '',
      // 菜单列表
      menuList: [
        {
          url: 'el-icon-reading',
          name: '项目内容',
        },
        {
          url: 'el-icon-picture-outline',
          name: '项目图片',
        },
        {
          url: 'el-icon-chat-dot-square',
          name: '项目对接',
        },
      ],
      menuIndex: '0',
      // 对接聊天
      room: {},
    };
  },
  async created() {
    await this.searchInfo();
  },
  methods: {
    ...dock({ dockQuery: 'query', dockFetch: 'fetch', goodsquery: 'goodsquery' }),
    ...transaction({ buyProduct: 'create' }),
    ...expertsuser(['fetch']),
    ...personalRoom(['create', 'countDelete']),
    async searchInfo() {
      // 查询展会详情
      if (this.dock_id) {
        let res = await this.dockFetch(this.dock_id);
        if (this.$checkRes(res)) {
          this.$set(this, `dockInfo`, res.data);
          if (this.type == '3') {
            this.searchExpert();
          } else {
            // 查询产品
            let czxm = res.data.apply.map(item => item.goodsList);
            czxm = _.flattenDeep(czxm);
            let czxmNew = czxm.filter(item => item.dockStatus == '1');
            this.$set(this, `productList`, czxmNew);
            this.searchProduct();
          }
        }
      }
    },
    seachtime() {
      // 倒计时
      this.timer = setInterval(() => {
        this.setTime(this.dockInfo.start_time);
      }, 1000);
    },
    // 倒计时
    setTime(end) {
      let now = moment().format('YYYY-MM-DD HH:mm:ss');
      // let now = moment().format('2020-11-20 08:00:00');
      if (end == now) {
        clearInterval(this.timer);
        this.$set(this, `djs`, '展会开始');
      } else {
        let sec = moment(end).diff(now, 'seconds');
        let day = _.floor(sec / 60 / 60 / 24);
        let hour = _.floor(Math.abs(day * 24 - sec / 60 / 60));
        let dayhour = day * 24;
        let minute = _.floor(Math.abs(day * 24 * 60 + hour * 60 - sec / 60));
        let second = _.floor(Math.abs(day * 24 * 60 * 60 + hour * 60 * 60 + minute * 60 - sec));
        let str = `${hour + dayhour}小时${minute}分${second}秒`;
        this.$set(this, `djs`, str);
      }
    },
    // 查找产品详情
    searchProduct() {
      let productInfo = this.productList.find(i => i._id == this.id);
      if (productInfo) {
        this.$set(this, `detailInfo`, productInfo);
      }
    },
    // 查找专家详情
    async searchExpert() {
      let res = await this.fetch(this.id);
      if (res.errcode === 0) {
        this.$set(this, `detailInfo`, res.data);
      }
    },
    // 选择菜单
    changeMenu(name, index) {
      this.menuIndex = index;
      if (name == '项目对接') {
        this.btnPhone();
      }
    },
    // 聊天
    async btnPhone() {
      if (!this.room.id) {
        //TODO 请求房间号
        let obj = {};
        if (!this.user.uid) {
          // this.$message.error('游客身份无法与卖家对话,请先注册');
          this.$message({
            dangerouslyUseHTMLString: true,
            message: '<strong><a href="http://free.liaoningdoupo.com/platlive/webLogin" style="color:red;">游客身份无法与卖家对话,请先注册</a></strong>',
            type: 'error',
          });
          return;
        } else {
          obj.buyer_id = this.user.uid;
          obj.buyer_name = this.user.name;
        }
        if (!this.detailInfo.userid) {
          this.$message.error('缺少卖家信息,请联系卖家或管理员');
          return;
        } else {
          obj.seller_id = this.detailInfo.userid;
          obj.seller_name = this.detailInfo.contacts || this.detailInfo.name;
        }
        console.log(obj);
        let res = await this.create(obj);
        if (this.$checkRes(res)) {
          this.$set(this, `room`, res.data);
        }
      }
    },
    // 洽谈
    async onSubmit() {
      let data = this.detailInfo;
      let form = {};
      if (this.type == '3') {
        form = {
          userid: this.user.uid,
          username: this.user.name,
          product_id: data.id,
          product_name: data.name,
          market_userid: data.id,
          market_username: data.name,
          status: '0',
          type: '1',
        };
      } else {
        form = {
          userid: this.user.uid,
          username: this.user.name,
          product_id: data._id,
          product_name: data.name,
          market_userid: data.userid,
          market_username: data.contacts,
          status: '0',
          type: '1',
        };
      }
      if (!this.user.uid) {
        // this.$message.error('游客身份无法与卖家对话,请先注册');
        this.$message({
          dangerouslyUseHTMLString: true,
          message: '<strong><a href="http://free.liaoningdoupo.com/platlive/webLogin" style="color:red;">游客身份无法与卖家对话,请先注册</a></strong>',
          type: 'error',
        });
        return;
      } else {
        let res = await this.buyProduct(form);
        this.$checkRes(res, '购买申请成功', res.errmsg || '购买申请失败');
      }
    },
  },
  computed: {
    ...mapState(['user']),
    dock_id() {
      return this.$route.query.dock_id;
    },
    id() {
      return this.$route.query.id;
    },
    type() {
      return this.$route.query.type;
    },
  },
  watch: {
    dockInfo: {
      immediate: true,
      deep: true,
      handler(val) {
        this.seachtime();
      },
    },
  },
};
</script>

<style lang="less" scoped>
.main {
  .one {
    height: 100px;
    .left {
      height: 100px;
      .leftone {
        margin: 15px 0 0 0;
        span:nth-child(1) {
          font-size: 35px;
          color: #535353;
          font-weight: bold;
          font-family: cursive;
        }
        span:nth-child(2) {
          font-size: 15px;
          padding: 0 10px;
          color: #535353;
        }
      }
      .lefttwo {
        font-size: 12px;
        color: #535353;
      }
    }
    .right {
      height: 100px;
      line-height: 100px;
      text-align: right;
    }
  }
  .two {
    height: 610px;
    background: url('~@/assets/bj1.jpg');
    padding: 20px 0;
    .twotop {
      margin: 0 0 10px 0;
      .left {
        height: 357px;
        margin: 0 10px 0 0;
        background: url('~@/assets/dock1.png');
        p {
          float: left;
          width: 79%;
          font-size: 43px;
          color: #fff;
          font-weight: bold;
          padding: 13% 0 0 20px;
        }
        .el-image {
          float: left;
          width: 153px;
          height: 94px;
          margin: 4% 0 0 0;
        }
      }
      .right {
        background-color: #ffffff;
        width: 24%;
        height: 357px;
        text-align: center;
        padding: 0 10px;
        .el-image {
          width: 120px;
          height: 48px;
          margin: 50px 0 0px 0;
        }
        p:nth-child(2) {
          font-size: 18px;
          margin: 30px 0 5px 0;
        }
        p:nth-child(3) {
          font-size: 22px;
          font-weight: bold;
          margin-bottom: 30px;
        }
        p:nth-child(4) {
          background: #f5f5f5;
          padding: 20px 0px;
          border-radius: 5px;
          font-size: 30px;
        }
      }
      .right:hover {
        cursor: pointer;
        p:nth-child(3) {
          color: #535353;
        }
      }
    }
    .twocen {
      height: 130px;
      overflow: hidden;
      line-height: 130px;
      background-color: #fff;
      margin: 0 0 35px 0;
      .left {
        font-size: 18px;
        margin: 0 0 0 30px;
        span {
          font-size: 22px;
          font-weight: bold;
        }
      }
      .right {
        .btn1 {
          font-size: 16px;
          color: #fff;
          background: red;
          border: none;
          font-weight: bold;
        }
        .btn2 {
          font-size: 16px;
          color: #ff0000;
          border: 1px solid #ff0000;
          font-weight: bold;
        }
      }
    }
  }
  .three {
    min-height: 700px;
    overflow: hidden;
    position: relative;
    top: -50px;
    z-index: 999;
    .threeinfo {
      min-height: 700px;
      overflow: hidden;
      background-color: #fff;
      padding: 50px 15px 0 15px;
      box-shadow: 0 5px 15px rgba(33, 82, 203, 0.35);
      .left {
        .menuList {
          height: 50px;
          border-radius: 5px;
          background-color: #bccbef;
          color: #2152cb;
          margin: 0 0 30px 0;
          padding: 0 0 0 30px;
          i {
            font-size: 35px;
            padding: 6px 0;
          }
          span {
            position: relative;
            top: -8px;
            font-size: 18px;
            left: 6px;
          }
        }
        .indexClass {
          background: #2152cb;
          color: #fff;
          i {
            color: #fff;
          }
        }
        .menuList:hover {
          cursor: pointer;
          background: #2152cb;
          color: #fff;
          i {
            color: #fff;
          }
        }
      }
      .right {
        min-height: 650px;
        padding: 0 0 0 15px;
        .productImage {
          width: 48%;
          margin: 0 15px 15px 0;
          .el-image {
            width: 100%;
            border-radius: 10px;
          }
        }
      }
    }
  }
  .four {
    border-top: 2px solid #305798;
    height: 250px;
    .info {
      .left {
        h1 {
          padding: 15px 0;
          font-weight: bold;
          color: #4d4d4d;
        }
        p {
          font-size: 16px;
          color: #4d4d4d;
          padding: 5px 0;
        }
      }
      .right {
        text-align: center;
      }
    }
  }
}
</style>
