<template>
  <view>
    <!-- 顶部搜索加tab栏部分，点击批量打折是隐藏该部分 -->
    <view class="top-tab-search" v-if="!batch">
      <view class="search">
        <u--input
          v-model="keywords"
          :placeholder="i18n.searchGoods"
          shape="circle"
          prefixIcon="search"
          prefixIconStyle="font-size: 22px;color: #909399"
          @change="search"
        ></u--input>
        <view class="screen" @click="openPopup">
          {{ i18n.screen }}
          <image
            class="screen-icon"
            src="../../static/mine/334.png"
            mode=""
          ></image>
        </view>
      </view>
      <view class="tab">
        <view class="top-tab">
          <view
            :class="{ commodity: follow == '' }"
            class="tab"
            @click="tabSwitch('')"
          >
            {{ i18n.whole }}
          </view>
          <view
            :class="{ commodity: follow == 'normal' }"
            class="tab"
            @click="tabSwitch('normal')"
          >
            {{ i18n.alreadyListed }}
          </view>
          <view
            :class="{ commodity: follow == 'down' }"
            class="tab"
            @click="tabSwitch('down')"
          >
            {{ i18n.removed }}
          </view>
        </view>
      </view>
    </view>
    <!-- 顶部搜索加tab栏部分，点击批量打折是隐藏该部分 -->

    <!-- 内容 -->
    <view class="page">
      <u-checkbox-group
        v-model="selectedGoods"
        placement="column"
        @change="checkboxChange"
      >
        <view v-for="item in goodsList" :key="item.id">
          <view class="list">
            <view class="radio" v-if="batch">
              <u-checkbox
                :name="item.id"
                shape="circle"
                color="#f83224"
                activeColor="#f83224"
                :disabled="item.status == 'down'"
              >
              </u-checkbox>
            </view>
            <GoodsInformation
              :batch="batch"
              productAndCommodity="commodity"
              @openDiscountsPopup="openDiscountsPopup"
              :status="status"
              :itemInfo="item"
              @toDetail="toDetail"
              @deleteGoods="deleteGoods"
              @addAndRemove="addAndRemove"
              @cancelDiscount="cancelDiscount"
            />
          </view>
        </view>
      </u-checkbox-group>
    </view>
    <view class="footer" v-if="!batch">
      <view class="batch" @click="batchDiscount">
        {{ i18n.batchDiscount }}
      </view>
      <button class="btn-1" @click="toPlatform">{{ i18n.listingGoods }}</button>
    </view>
    <view class="footer" v-else>
      <view class="batch" @click="allSelect">
        <radio
          :checked="value"
          style="transform: scale(0.7)"
          color="#f83224"
          activeBackgroundColor="#f83224"
        ></radio>
        {{ i18n.selectall }}
      </view>
      <view
        style="
          font-size: 26rpx;
          margin-left: 20rpx;
          color: rgba(54, 54, 54, 0.5);
        "
        @click="cancelBatchDiscount"
        >{{ i18n.cancelDiscount }}</view
      >
      <button class="btn" @click="wantDiscount">{{ i18n.wantDiscount }}</button>
    </view>
    <!-- 筛选弹窗 -->
    <ScreenPopup :show="show" @close="close" :follow="follow" />
    <!-- 筛选弹窗 -->

    <!-- 打折弹窗 -->
    <DiscountsPopup
      :show="discountsShow"
      :selectGoodsInformation="selectGoodsInformation"
      @close="closeDiscountsPopup"
    />
    <!-- 打折弹窗 -->

    <!-- 批量打折 -->
    <BatchDiscountPopup
      :show="batchPopup"
      @close="closeDiscountsPopup"
      :selectGoodsInformation="selectGoodsInformation"
    />

    <agreePopup :agreeShow="agreeShow" :title="title" @close="closeDelete" />
    <u-toast ref="uToast"></u-toast>
  </view>
</template>

<script>
import GoodsInformation from "./component/goodsInformation.vue";
import ScreenPopup from "./component/screenPopup.vue";
import DiscountsPopup from "./component/discountsPopup.vue";
import BatchDiscountPopup from "./component/batchDiscountPopup.vue";
import agreePopup from "../components/agreePopup";
export default {
  components: {
    GoodsInformation,
    ScreenPopup,
    DiscountsPopup,
    BatchDiscountPopup,
    agreePopup,
  },
  data() {
    return {
      follow: "",
      status: "1",
      show: false,
      discountsShow: false,
      selectGoodsInformation: {},
      batch: false,
      value: false,
      batchPopup: false,
      goodsList: [],
      keywords: "", //搜索关键字
      source: "",
      selectedGoods: [],
      userId: "",
      agreeShow: false,
      needDeleteGoods: {},
      title: "",
      page: 1, //分页
      total: 0,
      limit: 10,
    };
  },
  computed: {
    i18n() {
      return this.$t("index");
    },
    //可选的商品数量，去掉下架的商品
    goodsLength() {
      let num = 0;
      this.goodsList.map((item) => {
        if (item.status != "down") {
          num++;
        }
      });
      return num;
    },
  },
  onLoad(options) {
    uni.getStorage({
      key: "user_info",
      success: ({ data }) => {
        this.userId = data.merchant.id;
      },
    });
  },
  watch: {
    selectedGoods(newVal) {
      if (this.goodsLength == newVal.length) {
        this.value = true;
      } else {
        this.value = false;
      }
    },
  },
  methods: {
    //删除商品
    deleteGoods(value) {
      console.log(value);
      this.agreeShow = true;
      this.title = this.i18n.deleteGoods;
      this.needDeleteGoods = value;
    },

    //关闭确认弹窗
    closeDelete(value) {
      this.agreeShow = false;
      if (value) {
        if (this.title == "是否下架商品") {
          this.goodsApi(this.needDeleteGoods, "down");
        } else {
          uni.$u.http
            .post(`/api/goods/merchant_goods_del`, {
              merchant_goods_id: this.needDeleteGoods.id,
            })
            .then((res) => {
              this.getCommodityList(this.follow);
              uni.showToast({
                title: this.i18n.beenDeleted,
                icon: "none",
              });
            });
        }
      }
    },

    //取消打折
    cancelDiscount(item) {
      uni.$u.http
        .post(`/api/goods/cancel_discount`, {
          merchant_goods_id: item.id,
        })
        .then((res) => {
          this.getCommodityList();
          uni.showToast({
            title: this.i18n.cannotDiscount,
            icon: "none",
          });
        });
    },

    //商品上架 下架的接口调用
    goodsApi(item, value) {
      uni.$u.http
        .post(`/api/goods/modify_status`, {
          merchant_goods_id: item.id,
          status: value,
        })
        .then((res) => {
          uni.showToast({
            title: value == "down" ? this.i18n.takenDown : this.i18n.takenUp,
          });
          this.getCommodityList(this.follow);
        });
    },

    //商品上架 or 下架
    addAndRemove(item, value) {
      if (value == "down") {
        this.title = this.i18n.removeGoods;
        this.agreeShow = true;
        // this.goodsApi(item, value);
        this.needDeleteGoods = item;
      } else {
        this.goodsApi(item, value);
      }
    },
    //切换横向tab栏
    tabSwitch(num) {
      this.follow = num;
      this.getCommodityList(num);
    },

    //跳转平台库商品
    toPlatform() {
      uni.navigateTo({
        url: `/pageD/platformProducts/platformProducts`,
      });
    },
    checkboxChange(e) {
      console.log(e);
    },
    //搜索
    search(e) {
      this.getCommodityList();
    },
    //打开筛选弹窗
    openPopup() {
      this.show = true;
    },

    //关闭筛选弹窗
    close(data) {
      this.show = false;
      if (data) {
        this.keywords = data.keywords;
        this.source = data.source;
        this.getCommodityList(data.status);
      } else {
        this.keywords = "";
        this.source = "";
        this.getCommodityList();
      }
      // this.follow = num;
    },
    //全选
    allSelect() {
      //判断是否已经全选
      if (this.selectedGoods.length == this.goodsLength) {
        this.selectedGoods = [];
      } else {
        this.goodsList.map((item) => {
          if (
            this.selectedGoods.indexOf(item.id) == -1 &&
            item.status != "down"
          ) {
            this.selectedGoods.push(item.id);
          }
        });
      }
    },
    //批量打折
    batchDiscount() {
      this.batch = !this.batch;
      uni.setNavigationBarTitle({
        title: this.i18n.batchDiscount,
      });
    },
    cancelBatchDiscount() {
      this.batch = false;
      uni.setNavigationBarTitle({
        title: this.i18n.productManagement,
      });
      this.selectedGoods = [];
    },
    //打开我要打折窗口
    wantDiscount() {
      if (this.selectedGoods == 0) {
        this.$refs.uToast.show({
          type: "default",
          title: "默认主题",
          message: this.i18n.selectGoods,
        });
        return;
      }
      this.batchPopup = true;
    },

    //关闭折扣弹窗
    closeDiscountsPopup(value) {
      //判断是批量打折还是单个打折
      if (this.batchPopup) {
        this.batchPopup = false;
        if (value) {
          uni.$u.http
            //将选中商品的id使用","拼接
            .post(`/api/goods/discount`, {
              merchant_goods_id: this.selectedGoods.join(","),
              mer_discount: value,
            })
            .then((res) => {
              this.getCommodityList(this.follow);
            });
          this.batch = false;
          uni.setNavigationBarTitle({
            title: this.i18n.productManagement,
          });
        }
      } else if (this.discountsShow) {
        this.discountsShow = false;
        if (value) {
          uni.$u.http
            .post(`/api/goods/discount`, {
              merchant_goods_id: this.selectGoodsInformation.id,
              mer_discount: value,
            })
            .then((res) => {
              this.getCommodityList(this.follow);
            });
        }
      }
    },
    //打开折扣弹窗
    openDiscountsPopup(value) {
      this.selectGoodsInformation = value;
      this.discountsShow = true;
    },
    toDetail(item) {
      uni.navigateTo({
        url: `/pageD/productDetails/productDetails?goodsId=${item.id}`,
      });
    },
    getCommodityList(status = "", value = "") {
      uni.$u.http
        .post(`/api/goods/merchant_goods`, {
          page: this.page,
          limit: 10,
          merchant_id: this.userId,
          status: status,
          keywords: this.keywords,
          source: this.source,
        })
        .then((res) => {
          if (!value) {
            this.goodsList = res.data;
          } else {
            this.goodsList = this.goodsList.concat(res.data);
          }
          this.total = res.total;
        });
    },
  },
  //触底加载
  onReachBottom() {
    if (this.goodsList.length < this.total) {
      this.page++;
      this.getCommodityList("", 1);
    }
  },
  mounted() {
    this.getCommodityList();
    uni.setNavigationBarTitle({
      title: this.i18n.productManagement,
    });
  },
};
</script>

<style lang="scss" scoped>
.top-tab-search {
  background-color: #fff;
  padding: 10rpx 24rpx;
  z-index: 20174;

  .search {
    display: flex;
    align-items: center;

    .screen {
      display: flex;
      align-items: center;
      font-size: 26rpx;
      color: #333;
      margin-left: 20rpx;

      .screen-icon {
        width: 24rpx;
        height: 24rpx;
        margin-left: 5rpx;
      }
    }
  }

  .top-tab {
    margin-top: 10rpx;
    padding: 0 20rpx;
    display: flex;
    justify-content: space-between;

    .tab {
      font-size: 26rpx;
      color: rgba(34, 34, 34, 0.8);
      flex-shrink: 0;
      height: 44rpx;
      display: flex;
      align-items: flex-end;
      padding-bottom: 12rpx;
    }

    .commodity {
      position: relative;
      font-weight: 600;
    }

    .commodity::before {
      content: "";
      display: block;
      height: 4rpx;
      width: 50%;
      background: #f83224;
      position: absolute;
      bottom: -10rpx;
      opacity: 0.8;
      left: 50%;
      transform: translate(-50%, 50%);
    }
  }
}

.page {
  padding: 20rpx 24rpx;
  background-color: #fff;
  margin-bottom: 150rpx;

  .list {
    display: flex;
    flex-wrap: nowrap;
    align-items: center;

    .radio {
      margin-right: 20rpx;
    }
  }
}

.footer {
  position: fixed;
  display: flex;
  align-items: center;
  justify-content: space-between;
  bottom: 0;
  background-color: #fff;
  width: 93%;
  padding: 16rpx 24rpx 70rpx 24rpx;

  .batch {
    color: #333;
    font-size: 28rpx;
    margin-right: 50rpx;
    display: flex;
    align-items: center;
  }

  .btn-1 {
    width: 236rpx;
    height: 76rpx;
    border-radius: 52rpx;
    background-color: #fff;
    color: #f83224;
    border: 2rpx solid #f83224;
    padding: 0;
    margin: 0;
    font-size: 30rpx;
  }

  .btn {
    width: 236rpx;
    height: 76rpx;
    border-radius: 52rpx;
    background-color: #f83224;
    color: #fff;
    border: none;
    padding: 0;
    margin: 0;
    font-size: 30rpx;
  }
}
</style>
