<template>
  <view class="content">
    <view class="picker">
      <picker
        @change="bindPickerChange"
        :value="index"
        :range="array"
        range-key="name"
      >
        <view class="picker_title">
          <view class="pickername">
            <view v-if="index != -1">
              {{ array[index].name }}
            </view>
            <view v-else>类型</view>
          </view>
          <view class="triangle-down"></view>
        </view>
      </picker>
    </view>
    <view class="supplyList">
      <view
        class="supplyCard"
        v-for="(supply, i) in supplyList"
        :key="i"
        @tap="toDetail(supply.id)"
      >
        <view class="image_content">
          <image :src="supply.image" />
        </view>
        <view class="info">
          <view class="title">
            {{ supply.title }}
          </view>
          <view class="time">发布时间：{{ supply.time }}</view>
        </view>
      </view>
    </view>
    <view class="menus">
      <navigator url="/pages/supply/putSupply">
        <view class="menu">
          <view>
            <image src="/static/supply/putSupply.png" />
          </view>
          <view class="menu_title">发布供需</view>
        </view>
      </navigator>
      <navigator url="/pages/supply/mySupply">
        <view class="menu">
          <view>
            <image src="/static/supply/mySupply.png" />
          </view>
          <view class="menu_title">我的供需</view>
        </view>
      </navigator>
    </view>
  </view>
</template>

<script>
import md5 from "@/common/md5.js";
export default {
  data() {
    return {
      supplyList: [],
      array: [{ name: "全部" }, { name: "供需" }, { name: "需求" }],
      index: -1,
    };
  },
  onLoad() {
    this.getMyList();
  },
  methods: {
    bindPickerChange(e) {
      this.index = e.detail.value;
      this.getMyList();
    },
    toDetail(index) {
      uni.navigateTo({
        url: "/pages/supply/supply_detail?id=" + index,
      });
    },
    getMyList() {
      let md5Sign = md5(
        "method=" +
          "need" +
          "&timestamp=" +
          getApp().globalData.globalTimestamp +
          "&secret=" +
          getApp().globalData.secret
      );
      let url =
        getApp().globalData.shareUrl +
        "api/api.php" +
        "?method=need&source=need&action=list&timestamp=" +
        getApp().globalData.globalTimestamp +
        "&sign=" +
        md5Sign;
        let postData = {
           s_type:this.index==2?1:2
        }
      uni.request({
        url: url,
        method: "POST",
        header: {
          "content-type": "application/x-www-form-urlencoded",
        },
        data: this.index==-1||this.index==0?{}:postData,
        success: (res) => {
          if (res.data.code === 200) {
            let list = res.data.data.list;
            /* 
                supplyList: new Array(5).fill({
                image: "/static/supply/u1779.png",
                title: "移动式空气消毒机",
                time: "2021-08-30 14:50:00",
                state: 1,
                }),
                
       */
            console.log(list);
            this.supplyList = list.map((item) => {
              let ob = {
                image: "",
                title: "",
                time: "",
                state: "",
                id: "",
              };
              ob.id = item.id;
              ob.title = item.title;
              let time = this.$options.filters["globalTime"](item.addtime);
              let timeSecond = this.$options.filters["globalTimeSecond"](
                item.addtime
              );
              ob.time = time + " " + timeSecond;
              ob.state = item.type;
              ob.image = item.url;
              return ob;
            });
            // console.log(res.data.data.list);
          }
        },
        fail: () => {
          console.log("连接失败");
        },
      });
    },
  },
};
</script>

<style lang="scss" scope>
.content {
  font-size: 30rpx;
  margin: 0 5%;
  .picker {
    position: fixed;
    top: 3%;
    left: 5%;
    .picker_title {
      width: 100rpx;
      display: flex;
      align-items: center;
      justify-content: space-between;
      .triangle-down {
        width: 0;
        height: 0;
        border-top: 15rpx solid rgb(173, 173, 173);
        border-left: 15rpx solid transparent;
        border-right: 15rpx solid transparent;
      }
      .pickername {
        font-weight: 100;
      }
    }
  }
  .supplyList {
    margin-top: 12%;
    .supplyCard {
      display: flex;
      width: 92%;
      margin: 2% 0;
      padding: 2% 4%;
      height: 5%;
      border-radius: 30rpx;
      box-shadow: rgba(100, 100, 111, 0.2) 0rpx 14rpx 50rpx 0rpx;
      .image_content {
        margin-right: 5%;
        image {
          width: 100rpx;
          height: 100rpx;
          border-radius: 20rpx;
          background-color: rgb(228, 228, 228);
        }
      }
      .info {
        display: flex;
        flex-flow: column;
        justify-content: space-around;
        .time {
          font-weight: 100;
          font-size: 25rpx;
        }
      }
    }
  }
  .menus {
    z-index: 999;
    display: flex;
    justify-content: space-around;
    width: 80%;
    // height: 6%;
    position: fixed;
    top: 85%;
    left: 50%;
    padding: 4%;
    border-radius: 40rpx;
    box-shadow: rgba(100, 100, 111, 0.2) 0rpx 14rpx 50rpx 0rpx;
    transform: translateX(-50%);
    background-color: #fff;
    image {
      width: 50rpx;
      height: 50rpx;
      border-radius: 10rpx;
    }
    .menu {
      display: flex;
      flex-flow: column;
      align-items: center;
      .menu_title {
        text-align: center;
        font-size: 25rpx;
      }
    }
  }
}
</style>
