<template>
<!--pages/user/resourcesOrder/index.wxml-->
<view class="order-page">
  <view class="tabs-box acea-row row-between">
    <view :class="'tab ' + (tabActive == 1 ? 'tab-a' : '')" data-i="1" @tap="tabClick">沟通中</view>
    <view :class="'tab ' + (tabActive == 2 ? 'tab-a' : '')" data-i="2" @tap="tabClick">进行中</view>
    <view :class="'tab ' + (tabActive == 3 ? 'tab-a' : '')" data-i="3" @tap="tabClick">已结束</view>
  </view>
  <view class="list">
    <view class="no-list" v-if="list.length == 0">
      <image src="https://download.cyjyyjy.com/no-needs.png"></image>
      <view>暂无订单</view>
      <view class="to-btn" @tap="toDemandHall">去需求广场</view>
    </view>
    <view v-for="(item, index) in list" :key="index" class="item" v-if="tabActive == 1">
      <view class="needs-info-box">
        <view class="n-title-box acea-row row-between">
          <view>{{item.resourceInfo.name}}</view>
          <!-- <view class="status" wx:if="{{item.comminuteState == 3}}">项目已成立</view> -->
        </view>
        <view class="n-price">需求预算 <text class="colO">{{item.resourceInfo.budget}}</text></view>
        <view class="n-desc">{{item.resourceInfo.content}}</view>
      </view>
      <view class="concat-list-box">
        <!-- <view class="open-btn acea-row row-middle" bindtap="openClick" data-idx="{{index}}">
         <text>{{!item.open ? '展开' : '收起'}}</text> 
         <image class="arrow-down {{!item.open ? '' : 'arrow-up'}}" src="../../../images/arrow-d.png"></image>
        </view> -->
        <!-- <view class="concat-list {{item.open ? 'list-open' : ''}}" > -->
          <view v-for="(info, index2) in item.partnerInfos" :key="index2" class="info-item">
            <view class=" acea-row row-middle">
              <image :src="info.logo"></image>
              <view class="info2">
                <view class="projectinfo-box acea-row-nowrap row-between">
                  <view class="acea-row row-middle line1">
                    <view class="p-label">合作方：</view>
                    <view>{{info.name}}</view>
                  </view>
                  <view class="status" v-if="info.comminuteState == 1">已申请</view>
                  <view class="status" v-if="info.comminuteState == 2">等待立项</view>
                  <view class="status" v-if="info.comminuteState == 3">项目成立中</view>
                  <view class="status" v-if="info.comminuteState == 4">已取消立项</view>
                </view>
                <!-- <view cl哦ass="tag-item" wx:if="{{info.comminuteState == 1}}">{{info.cooperationMode}}</view> -->
              </view>
            </view>
            <view class="btn-box acea-row row-between row-middle">
              <view class="start-time">{{info.createTime}}项目开启</view>
              <view class="btns acea-row">
                <view class="btn-item" v-if="info.comminuteState == 1" @tap="cancelCommunicate" :data-info="info">取消沟通</view>
                <view class="btn-item" v-if="info.comminuteState == 2 || info.comminuteState == 3" @tap="toCreateProject" :data-item="info">查看立项书</view>
                <view class="btn-item" v-if="info.comminuteState == 2" @tap="confirm" :data-item="item" :data-id="info.communicateId">确认立项</view>
                <view class="btn-item default" v-if="info.comminuteState == 2" @tap="cancelProject" :data-item="item" :data-id="info.communicateId">取消立项</view>
              </view>
            </view>
          </view>
        <!-- </view> -->
      </view>
    </view>
    <view v-for="(item, index) in list" :key="index" class="item" v-if="tabActive != 1">
      <view class="number acea-row row-between row-middle">
       <text>项目编号 {{item.projectCode}}</text>
       <!-- <view class="status">{{tabActive == 2 ? '进行中' : '已结束'}}</view> -->
       <view class="status" v-if="item.projectState != 5">{{tabActive == 1 ? '沟通中' : tabActive == 2 ? '进行中' : '已结束'}}</view>
       <view class="status" v-else>平台介入中</view>
      </view>
      <view class="info-box acea-row row-middle">
        <image :src="item.projectResource.logo"></image>
        <view class="info">
          <view class="i-title line1">{{item.resourcesDto.title || item.projectResource.name}}</view>
          <view class="tag-item">{{item.cooperationMode}}</view>
          <view class="i-desc" v-if="tabActive == 1">{{item.resourcesDto.detailedDescription}}</view>
          <view class="projectinfo-box">
            <view class="acea-row row-middle" v-if="tabActive != 1">
              <view class="p-label">项目名称：</view>
              <view>{{item.projectName}}</view>
            </view>
            <view class="acea-row row-middle line1" v-if="tabActive != 1">
              <view class="p-label">合作方：</view>
              <view>{{item.partner}}</view>
            </view>
          </view>
        </view>
      </view>
      <view class="progress-box" v-if="tabActive != 1">
        <view v-for="(progress, index2) in item.stageResultsDtoList" :key="index2" class="progress-item acea-row row-between row-middle">
          <view class="acea-row row-middle">
            <image src="/static/images/home/progress.png"></image>
            <text>{{progress.name}} ￥{{progress.money}}</text>
          </view>
          <view class="progress-status" v-if="progress.state == 0">未开始</view>
          <view class="progress-status" v-if="progress.state == 1">申请开启</view>
          <view class="progress-status" v-if="progress.state == 2">进行中</view>
          <view class="progress-status colorO" v-if="progress.state == 3">已结束</view>
        </view>
      </view>
      <view class="btn-box acea-row row-between row-bottom" v-if="tabActive != 1">
        <view class="start-time" v-if="tabActive == 2">{{item.examineTime}}项目开启</view>
        <view v-if="tabActive == 3">项目已圆满结束</view>
        <view class="btns acea-row">
          <view class="btn-item" v-if="tabActive == 2 && item.nextStageFlag" @tap="agreenOpen" :data-item="item">同意开启</view>
          <view class="btn-item" v-if="tabActive == 2  && item.deliveryFlag" @tap="deliveryProject" :data-item="item">同意交付</view>
          <view class="btn-item" v-if="tabActive == 2" @tap="toPlatform" :data-item="item">平台介入</view>
          
          <!-- <view class="btn-item"  wx:if="{{ tabActive == 3 }}">合作反馈</view> -->
        </view>
      </view>
    </view>
  </view>
</view>
</template>

<script>
// pages/user/needsOrder/index.js
const app = getApp();

export default {
  data() {
    return {
      tabActive: 1,
      list: []
    };
  },

  components: {},
  props: {},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getCommunicates();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {// this.getCommunicates()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    if (this.tabActive == 1) {
      this.getCommunicates();
    } else {
      this.getProjectList();
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  methods: {
    toDemandHall() {
      uni.switchTab({
        url: '/pages/demandHall/index'
      });
    },

    tabClick(e) {
      let i = e.currentTarget.dataset.i;
      this.setData({
        tabActive: i
      });

      if (i == 1) {
        this.getCommunicates();
      } else {
        this.getProjectList();
      }
    },

    // 获取 沟通中
    getCommunicates() {
      app.http('get', 'Communicate/demandCommunicates').then(res => {
        if (res.data.success) {
          if (res.data.data.length > 0) {
            res.data.data.map(item => {
              item.open = false;
            });
            res.data.data[0].open = true;
          }

          console.log(res.data.data);
          this.setData({
            list: res.data.data
          });
          uni.stopPullDownRefresh();
        }
      });
    },

    //点击展开
    openClick(e) {
      let idx = e.currentTarget.dataset.idx;
      console.log(idx);
      let list = this.list;
      list[idx].open = !list[idx].open;
      this.setData({
        list: list
      });
    },

    //获取进行中、已结束列表
    getProjectList() {
      app.http('get', 'Project/listProject', {
        projectState: this.tabActive - 1,
        projectType: 2
      }).then(res => {
        if (res.data.success) {
          res.data.data.map(item => {
            item.stageResultsDtoList.map(stage => {
              if (stage.state == 1) {
                item.showBtn = true;
              }
            });
          });
          this.setData({
            list: res.data.data
          });
          uni.stopPullDownRefresh();
        }
      });
    },

    //同意开启下一阶段
    agreenOpen(e) {
      let item = e.currentTarget.dataset.item;
      console.log(item);
      const stage = item.stageResultsDtoList.filter(item => {
        return item.state == 1;
      });
      uni.showModal({
        title: '提示！',
        content: '是否同意开启下阶段？',

        success(res) {
          if (res.confirm) {
            app.http('get', 'Project/startNextStage', {
              stageResultId: stage[0].id
            }).then(res => {
              if (res.data.success) {
                uni.showToast({
                  title: '操作成功！',
                  duration: 2000
                });
                this.getProjectList();
              }
            });
          }
        }

      });
    },

    // 平台介入
    toPlatform(e) {
      let code = e.currentTarget.dataset.item.projectCode;
      uni.navigateTo({
        url: '/pages/demandHall/platform/index?code=' + code
      });
    },

    // 取消沟通
    cancelCommunicate(e) {
      let info = e.currentTarget.dataset.info;
      console.log(info);
      let that = this;
      uni.showModal({
        title: '提示！',
        content: '是否取消沟通？',

        success(res) {
          if (res.confirm) {
            app.http('GET', 'Communicate/cancelCommunicates', {
              communicateId: info.communicateId
            }).then(res => {
              if (res.data.success) {
                uni.showToast({
                  title: '操作成功！',
                  duration: 1500
                });
                setTimeout(() => {
                  that.getCommunicates();
                }, 1500);
              }
            });
          }
        }

      });
    },

    //查看立项书
    toCreateProject(e) {
      let item = e.currentTarget.dataset.item;
      uni.navigateTo({
        url: '/pages/demandHall/createProject/index?state=' + item.comminuteState + '&id=' + item.communicateId
      });
    },

    confirm(e) {
      let item = e.currentTarget.dataset.item;
      let communicateId = e.currentTarget.dataset.id;
      let that = this;
      uni.showModal({
        title: '提示！',
        content: '是否确认立项？',

        success(res) {
          if (res.confirm) {
            app.http('GET', 'Communicate/aggreProject', {
              projectId: item.partnerInfos[0].projectId
            }).then(res => {
              if (res.data.success) {
                uni.showToast({
                  title: '操作成功！',
                  duration: 2000
                });
                setTimeout(() => {
                  that.getCommunicates();
                }, 2000);
              }
            });
          }
        }

      });
    },

    cancelProject(e) {
      let item = e.currentTarget.dataset.item;
      let communicateId = e.currentTarget.dataset.id;
      let that = this;
      uni.showModal({
        title: '提示！',
        content: '是否确认取消立项？',

        success(res) {
          if (res.confirm) {
            app.http('post', 'Communicate/cancelProject', {
              communicateId: communicateId,
              partnerInfos: item.partnerInfos
            }).then(res => {
              if (res.data.success) {
                uni.showToast({
                  title: '操作成功！',
                  duration: 2000
                });
                that.getCommunicates();
              }
            });
          }
        }

      });
    },

    //同意交付
    deliveryProject(e) {
      let item = e.currentTarget.dataset.item;
      uni.showModal({
        title: '提示',
        content: '是否确认提交申请交付',
        success: r => {
          if (r.confirm) {
            app.http('GET', 'Project/deliveryProject', {
              projectId: item.id
            }).then(res => {
              if (res.data.success) {
                uni.showToast({
                  title: '操作成功！'
                });
                this.getProjectList();
              }
            });
          }
        }
      });
    }

  }
};
</script>
<style>
/* pages/user/needsOrder/index.wxss */
@import "../resourcesOrder/index.css";

.needs-info-box{
  border: none !important;
}
.info2{
  width: calc(100% - 120rpx);
}
.info-item image{
  width: 90rpx;
  height: 90rpx;
  margin-right: 24rpx;
}
.info-item{
  font-size: 24rpx;
  border-top: 2rpx solid #F0F0F0;
  padding: 20rpx 0;
}
.projectinfo-box{
  font-size: 26rpx;
  margin: 2rpx 0;
}
.tag-item{
  /* transform: scale(0.8); */
  margin: 0;
}

.needs-info-box{
  /* padding-bottom: 22rpx; */
  border-bottom: 2rpx solid #F0F0F0;
  margin-bottom: 22rpx;
}
.n-title{
  color: #1D1D1D;
  font-size: 32rpx;
  line-height: 44rpx;
}
.n-price{
  color: #707070;
  font-size: 28rpx;
  margin: 8rpx 0;
}
.n-desc{
  font-size: 24rpx;
  line-height: 34rpx;
  color: #333333;
}
.btn-box{
  margin-top: 10rpx;
}
.btn-item {
  font-size: 24rpx;
  width: 130rpx;
  height: 50rpx;
  line-height: 50rpx;
  text-align: center;
  background: linear-gradient(134deg, #FFA782 0%, #FF6D31 100%);
  border-radius: 12rpx;
  color: #fff;
  margin-left: 8rpx;
}
.status {
  min-width: 150rpx;
  text-align: right;
}
</style>