<template>
  <view class="page">
    <view :class="show ? 'cover' : ''"> </view>
    <view class="m-product-all">
      <view class="m-tab">
        <view class="m-navbar">
          <view
            :class="[
              'm-navbar-item ellipsis',
              tapindex == 1 ? 'm-navbar-item-on' : '',
            ]"
            @click="allOrders"
          >
            {{ __("全部") }}
          </view>
          <view
            :class="[
              'm-navbar-item ellipsis',
              tapindex == 2 ? 'm-navbar-item-on' : '',
            ]"
            @click="getTender"
          >
            {{ __("匹配中") }}
          </view>
          <view
            :class="[
              'm-navbar-item ellipsis',
              tapindex == 3 ? 'm-navbar-item-on' : '',
            ]"
            @click="getWorking"
          >
            {{ __("已结束") }}
          </view>
          <view
            :class="[
              'm-navbar-item ellipsis',
              tapindex == 4 ? 'm-navbar-item-on' : '',
            ]"
            @click="getFinished"
          >
            {{ __("已取消") }}
          </view>
        </view>
      </view>
      <view v-if="loadComplete">
        <scroll-view
          v-if="orderlist.length > 0"
          scroll-y="true"
          class="m-orderlist"
          @scrolltolower="scrollbottom"
        >
          <view
            v-for="(items, i) in orderlist"
            :key="i"
            class="m-panel m-panel-access"
          >
            <view class="m-panel-hd">
              <view>
                <text>{{ __("采购编号：") }}{{ items.purchase_order_id }}</text>
                <label>{{ items.supply_order_time }}</label>
              </view>
              <view>
                <label
                  style="padding-right: 10rpx"
                  v-if="items.supply_task_state == 2000"
                  >{{ __("竞标中") }}</label
                >
                <label
                  style="padding-right: 10rpx"
                  v-if="items.supply_task_state == 2010"
                  >{{ __("已结束") }}</label
                >
                <label
                  style="padding-right: 10rpx"
                  v-if="items.supply_task_state == 2020"
                  >{{ __("已取消") }}</label
                >
              </view>
              <view>
                <text
                  >{{ __("报价标题：") }}{{ items.supply_order_title }}</text
                >
              </view>
              <view>
                <text
                  >{{ __("报价描述：")
                  }}{{ items.supply_order_description }}</text
                >
              </view>
            </view>

            <view class="m-product-list">
              <navigator
                :url="
                  '/hall/purchase/detail?order_id=' + items.service_order_id
                "
                class="m-product-item"
              >
                <view class="m-product-item">
                  <view class="m-product-img">
                    <image :src="items.supply_images[0]" mode="aspectFill" />
                  </view>
                  <view class="m-product-info">
                    <view class="m-product-name" style="height: 65rpx">
                      <label>
                        {{ items.product_name }}
                      </label>
                    </view>
                    <view
                      class="m-product-name"
                      style="height: 65rpx"
                      v-if="items.supply_province && items.supply_city"
                    >
                      <label>
                        {{ items.supply_province }}/{{ items.supply_city }}/{{
                          items.supply_county
                        }}
                      </label>
                    </view>

                    <view class="m-product-price">
                      <block>
                        <label
                          >{{ __("￥") }}{{ items.product_unit_price }}
                        </label>
                        <label style="margin-left: 50rpx"
                          >x{{ items.supply_quantity }}</label
                        >
                      </block>
                    </view>
                  </view>
                </view>
                <view class="m-product-select">
                  <text
                    v-if="
                      items.supply_task_state == 2010 &&
                      items.supply_is_selected
                    "
                    >{{ __("已中标") }}</text
                  >
                </view>
              </navigator>
            </view>
            <!-- items.Type==2 ???? -->
            <view
              class="m-product-item"
              v-if="items.Type == 2"
              style="height: 154rpx"
            >
              <view class="m-product-img">
                <image
                  :src="items.store_logo"
                  mode="aspectFill"
                  style="width: 172rpx; height: 172rpx"
                />
              </view>
              <view class="m-product-info2" style="padding: 20rpx 0">
                <view class="m-product-name">
                  <label>{{ items.store_name }}</label>
                </view>
              </view>
            </view>
            <view class="m-total-btn">
              <view
                :data-on="items.order_id"
                v-if="items.order_lock_status"
                class="u-link-btn"
                >{{ __("退款/退货中...") }}</view
              >
              <view
                :data-on="items.order_id"
                v-if="
                  items.kind_id == 1202 &&
                  items.order_state_id == StateCode.ORDER_STATE_SHIPPED
                "
                class="u-link-btn"
                @click="writeOrder"
                >{{ __("确认服务") }}</view
              >
              <!-- <view :data-on="(items.supply_order_id)" v-if="(!items.supply_is_selected)" class="u-link-btn" @click="cancelOrder">{{__('取消订单')}}</view> -->
              <navigator
                :url="
                  '/hall/purchase/detail?order_id=' + items.purchase_order_id
                "
                class="u-link-btn"
                >{{ __("查看详情") }}</navigator
              >
            </view>
          </view>
          <view class="m-loading-box">
            <block v-if="ispage">
              <view class="u-loadmore">
                <label class="u-loading"></label>
                <text class="u-loadmore-tips">{{ __("正在加载") }}</text>
              </view>
              <view>
                <text></text>
              </view>
            </block>
            <block v-else>
              <view class="u-loadmore u-loadmore-line">
                <text class="u-loadmore-tips">{{
                  __("没有更多数据啦！")
                }}</text>
              </view>
            </block>
          </view>
        </scroll-view>

        <navigator
          url="/hall/purchase/lists"
          redirect="true"
          class="m-nullpage"
          v-else
        >
          <view class="m-nullpage-middle">
            <view class="m-null-tip">
              <label style="font-size: 32rpx">{{
                __("亲~您还没有任务哦，")
              }}</label
              ><label class="highlight-link">{{ __("快去看看吧") }}</label>
            </view>
          </view>
        </navigator>
      </view>
      <view class="alert" v-if="show">
        <image
          style="width: 50rpx; height: 50rpx"
          src="https://media-mall-prod-1259811287.cos.ap-guangzhou.myqcloud.com/static/xcxfile/appicon/images/true.png"
        ></image>
        <text style="margin-left: 20rpx">{{ Info }}</text>
      </view>
    </view>

    <payment-box
      :paymentDataDefault="paymentData"
      :order_id="order_id"
      ref="paymentBox"
      @onCancel="onCancel"
      @onPaid="onPaid"
      @onFail="onFail"
    ></payment-box>
  </view>
</template>


<script>
import paymentBox from "../../components/payment-box.vue";

import { mapState, mapMutations } from "vuex";

export default {
  data() {
    return {
      isdata: false,
      tapindex: 1,
      page: 1,
      rows: 10,
      ispage: true,
      flag: true,
      type: 0,
      kind_id: 1201,
      formId: "",
      orderlist: [],
      imgLogo: "",
      show: false,
      Info: "",
      order_id: "",
      paymentData: {},
      loadComplete: false,
    };
  },
  components: {
    paymentBox,
  },
  computed: mapState([
    "Config",
    "StateCode",
    "notice",
    "plantformInfo",
    "shopInfo",
    "userInfo",
    "hasLogin",
  ]),
  onLoad: function (options) {
    uni.setNavigationBarTitle({
      title: this.__("我的报价"),
    });

    this.setData({
      tapindex: options.sl ? options.sl : 1,
      page: 1,
      rows: 10,
      orderlist: [],
      type: options.type ? options.type : 0,
      kind_id: options.kind_id ? options.kind_id : 1201,
    });
    var t = this;
    this.notice.addNotification("RefreshMessage", t.RefreshMessage, t);
    this.getOrderlist();
  },
  onShow: function () {},

  onUnload: function () {
    //移除通知
    // 离开页面，注销事件
    var that = this;
    this.notice.removeNotification("RefreshMessage", that);

    if (this.$refs.paymentBox.showBoxView) {
      this.$refs.paymentBox.cancel();
    }
  },
  onBackPress() {
    if (this.$refs.paymentBox.showBoxView) {
      this.$refs.paymentBox.cancel();

      return true;
    }
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getOrderlist();
  },
  methods: {
    ...mapMutations([
      "login",
      "logout",
      "getPlantformInfo",
      "forceUserInfo",
      "getUserInfo",
    ]),
    RefreshMessage: function (e) {
      this.setData({
        tapindex: 1,
        page: 1,
        rows: 10,
        orderlist: [],
        ispage: true,
        flag: true,
        type: 0,
        kind_id: this.kind_id,
        loadComplete: false,
      }),
        this.getOrderlist();
    },
    allOrders: function () {
      this.setData({
        tapindex: 1,
        page: 1,
        rows: 10,
        orderlist: [],
        ispage: true,
        flag: true,
        type: 0,
        kind_id: this.kind_id,
        loadComplete: false,
      }),
        this.getOrderlist();
    },
    getTender: function () {
      this.setData({
        tapindex: 2,
        page: 1,
        rows: 10,
        orderlist: [],
        ispage: true,
        flag: true,
        type: 2000,
        kind_id: this.kind_id,
        loadComplete: false,
      }),
        this.getOrderlist();
    },
    getWorking: function () {
      this.setData({
        tapindex: 3,
        page: 1,
        rows: 10,
        orderlist: [],
        ispage: true,
        flag: true,
        type: 2010,
        kind_id: this.kind_id,
        loadComplete: false,
      }),
        this.getOrderlist();
    },
    receiptOfGoods: function () {
      this.setData({
        tapindex: 3,
        page: 1,
        rows: 10,
        ispage: true,
        flag: true,
        orderlist: [],
        type:
          this.kind_id == 1201
            ? this.StateCode.ORDER_STATE_SHIPPED
            : this.StateCode.ORDER_STATE_WAIT_SHIPPING,
        kind_id: this.kind_id,
        loadComplete: false,
      }),
        this.getOrderlist();
    },
    getFinished: function () {
      this.setData({
        tapindex: 4,
        page: 1,
        rows: 10,
        ispage: true,
        flag: true,
        orderlist: [],
        type: 2020,
        kind_id: this.kind_id,
        loadComplete: false,
      }),
        this.getOrderlist();
    },
    scrollbottom: function () {
      if (this.flag) {
        var that = this;
        that.setData({
          flag: false,
        });

        clearTimeout(t);
        var t = setTimeout(function () {
          that.setData({
            state_type: that.type,
            flag: false,
            page: parseInt(that.page) + 1,
            rows: 10,
          });

          that.getOrderlist();
        }, 100);
      }
    },
    getOrderlist: function () {
      var that = this;
      var params = {
        page: that.page,
        rows: that.rows,
        state_type: that.type,
        kind_id: this.kind_id,
      };
      that.$.request({
        url: this.Config.URL.hall.user_supply_order_lists,
        data: params,
        success: function (data, status, msg, code) {
          if (200 == status && data.items.length > 0) {
            if (data.page >= data.total) {
              that.setData({
                flag: false,
                ispage: false,
                orderlist: that.orderlist.concat(data.items),
              });
            } else {
              that.setData({
                flag: true,
                ispage: true,
                orderlist: that.orderlist.concat(data.items),
              });
            }

            that.setData({
              isdata: true,
              loadComplete: true,
            });
          } else {
            that.setData({
              flag: false,
              ispage: false,
              isdata: false,
              loadComplete: true,
            });
          }
        },
        fail: function (data) {
          that.setData({
            loadComplete: true,
          });
          that.$.alert("网络异常请重试！");
        },
      });
    },
    cancelOrder: function (e) {
      var that = this,
        params = {
          order_id: e.currentTarget.dataset.on,
        };
      that.$.confirm(
        that.__("是否取消订单"),
        function (e) {
          if (e.confirm) {
            that.$.request({
              url: that.Config.URL.user.order_cancel,
              data: params,
              success: function (data, status, msg, code) {
                if (status == 200) {
                  that.$.alert("取消成功！", function () {
                    that.setData({
                      orderlist: [],
                      page: 1,
                    });
                    that.getOrderlist();
                  });
                }
              },
            });
          }
        },
        true
      );
    },
    writeOrder: function (e) {
      var that = this,
        params = {
          order_id: e.currentTarget.dataset.on,
        };
      that.$.confirm(
        "是否核销订单？",
        function (e) {
          if (e.confirm) {
            that.$.request({
              url: that.Config.URL.user.order_receive,
              data: params,
              success: function (data, status, msg, code) {
                if (status == 200) {
                  that.$.alert("核销成功！", function () {
                    that.setData({
                      orderlist: [],
                      page: 1,
                      Info: msg,
                    });
                    that.getOrderlist();
                  });
                } else {
                  (that.setData({
                    show: true,
                  }),
                  setTimeout(function () {
                    that.setData({
                      show: false,
                    });
                  }, 2e3)),
                    that.getOrderlist();
                }
              },
            });
          }
        },
        true
      );
    },
    confirmReceipt: function (obj) {
      var params = {
        order_id: obj.currentTarget.dataset.on,
      };

      var that = this;
      that.$.confirm(
        that.__("是否确认收货？"),
        function (e) {
          if (e.confirm) {
            that.$.request({
              url: that.Config.URL.user.order_receive,
              data: params,
              success: function (data, status, msg, code) {
                if (status == 200) {
                  that.$.alert(that.__("收货成功！"), function () {
                    that.setData({
                      orderlist: [],
                      page: 1,
                    });
                    that.getOrderlist();
                  });
                }
              },
            });
          }
        },
        true
      );
    },
    gotopay: function (e) {
      var that = this;

      that.setData({
        order_id: e.currentTarget.dataset.on,
        paymentData: {
          order_id: e.currentTarget.dataset.on,
          orderSelMoneyAmount: e.currentTarget.dataset.order_payment_amount,

          user_money: this.userInfo.user_money,
          user_points: this.userInfo.user_points,
          user_recharge_card: this.userInfo.user_recharge_card,
          user_sp: this.userInfo.user_sp,
        },
      });

      this.$refs.paymentBox.show();
      return true;

      this.setData({
        formId: e.detail.formId,
      });
      var param = {
        order_id: e.currentTarget.dataset.on,
        openid: this.userInfo.openId,
        typ: "json",
        payment_channel_code: "wx_native",
        prepay_flag: 1,
      };

      that.$.request({
        url: this.Config.URL.pay.pay,
        data: param,
        success: function (data, status, msg, code) {
          if (status == 200) {
            that.$.requestPayment({
              timeStamp: data.timeStamp,
              nonceStr: data.nonceStr,
              package: data.package,
              signType: data.signType,
              paySign: data.paySign,
              success: function () {
                that.sendMessage(e.currentTarget.dataset.on),
                  that.$.alert(that.__("支付成功！"), function () {
                    that.setData({
                      orderlist: [],
                      page: 1,
                    }),
                      that.getOrderlist();
                  });
              },
              fail: function (e) {
                // that.$.alert(that.__("支付成功！"), e)
              },
            });
          } else {
            that.$.alert(msg);
          }
        },

        fail: function (err) {},
      });
    },

    onCancel: function (e) {
      return;
    },
    onPaid: function (e) {
      let that = this;
      that.$.alert(
        that.__("支付成功"),
        function () {
          that.$.gotopage("/member/order/detail?on=" + e.order_id);
        },
        2000
      );

      if (this.$refs.paymentBox.showBoxView) {
        this.$refs.paymentBox.cancel();
      }
    },
    onFail: function (e) {},

    sendMessage: function (order_id) {
      let that = this;
      var params = {
        api: orderapi.OrderPaySuccessWXMessage,
        pages: "/member/order/detail?on=" + order_id,
        formId: this.formId,
        WeiXinOpenId: this.userInfo.openId,
        value: {
          order_id: order_id,
        },
      };
      that.$.sendTpl(params);
    },
  },
};
</script>

<style lang="scss">
@import "../../styles/_variables";

.m-navbar-item {
  padding: 20rpx 0;
  font-size: 24rpx;
}

.m-navbar-item:after {
  border: none;
}

.m-navbar-item.m-navbar-item-on {
  background-color: #fff;
  color: $default-skin-bg;
}

.m-navbar-item.m-navbar-item-on::before {
  content: " ";
  position: absolute;
  left: 0;
  bottom: 0;
  right: 0;
  height: 6rpx;
  border-bottom: 6rpx solid $default-skin-bg;
  color: #cccccc;
  -webkit-transform-origin: 0 100%;
  transform-origin: 0 100%;
  -webkit-transform: scaleY(0.5);
  transform: scaleY(0.5);
  z-index: 3;
}

.m-panel-hd label {
  float: right;
  color: $default-skin-bg;
  font-size: 26rpx;
}

.m-total-info {
  text-align: right;
  padding: 10rpx;
  font-size: 24rpx;
}

.m-total-info label {
  font-size: 32rpx;
  color: $default-skin-bg;
}

.m-total-btn {
  text-align: right;
  padding: 20rpx 20rpx 20rpx 20rpx;
  position: relative;
}

.m-total-btn::before {
  content: " ";
  position: absolute;
  left: 0;
  top: 0;
  right: 0;
  height: 1px;
  border-top: 1px solid #ebebe7;
  color: #ebebe7;
  -webkit-transform-origin: 0 0;
  transform-origin: 0 0;
  -webkit-transform: scaleY(0.5);
  transform: scaleY(0.5);
}

.m-total-btn .u-link-btn {
  margin: 0 10rpx;
  vertical-align: middle;
  display: inline-block;
  line-height: 40rpx;
  background-color: #fff;
}

.m-sort {
  position: relative;
}

.m-sort image {
  width: 24rpx;
  height: 24rpx;
  position: absolute;
  /*top: 50%;*/
  margin-top: 10rpx;
  margin-left: 2rpx;
}

.m-product-price text {
  font-size: 24rpx;
  color: #999999;
  margin-left: 6rpx;
}

.m-tab {
  position: fixed;
  width: 100%;
  top: var(--window-top);
  z-index: 2;
}
.m-orderlist {
  padding-top: 70rpx;
  position: absolute;
  box-sizing: border-box;
  width: 100%;
  height: 100%;
}

.u-tuan-label {
  padding: 5rpx;
  background-color: $default-skin-bg;
  border-radius: 4rpx;
  font-size: 24rpx;
  color: #fff;
  margin-right: 5rpx;
}

.alert {
  font-size: 32rpx;
  width: 70%;
  height: 200rpx;
  padding: 0 40rpx;
  position: absolute;
  left: 10%;
  top: 40%;
  background: #fff;
  border-radius: 10rpx;
  justify-content: center;
  display: flex;
  align-items: center;
  z-index: 999;
}

.cover {
  position: fixed;
  width: 100%;
  height: 100%;
  top: 0px;
  background: rgba(0, 0, 0, 0.4);
  overflow: hidden;
  z-index: 998;
}

.m-product-info {
  padding: 20rpx 20rpx;
  width: 80%;
}

.m-product-select {
  padding: 20rpx 20rpx;
  width: 20%;
}
.m-upImg-item {
  width: 120rpx;
  height: 120rpx;
  border: 1px solid #eee;
  display: inline-block;
  margin: 9rpx;
  border-radius: 4rpx;
  vertical-align: middle;
  position: relative;
}
.m-upImg-item image {
  width: 100%;
  height: 100%;
}
</style>
