<!--
 * @Author: lvy lvy
 * @Date: 2023-05-11 17:47:30
 * @LastEditors: lvy lvy
 * @LastEditTime: 2023-06-02 19:18:57
 * @FilePath: /redmall-uniapp/pages/user/Fruit/form.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template >
  <view v-if="onPageReset">

    <key-words :mix="true" :show_key="showKeyWord" :show_forgetFuc="true" @closeFuc="showKeyWord = false"
      @getPassword="onConfirm">
    </key-words>
    <view class="site-title text-center">当前可领取次数 <text class="text-normal-c ml-3 mr-3">{{ fruitLenght }} </text> 次</view>
    <view class="site-page">
      <u--form labelPosition="left" :model="model" ref="uForm">
        <u-form-item class="site-form-item" label="收货地址" label-width="100" prop="addressName" borderBottom ref="item1"
          @click="selectAddr">
          <text class="address" v-if="addressData.threeAdcode">{{ addressData.threeAdcode | filterAddr }}{{
            addressData.addressDetails }}{{ addressData.addressSupplement }}</text>
          <u--input v-else readonly border="none" placeholder="请选择收货地址"></u--input>
        </u-form-item>
        <u-form-item class="site-form-item" label="选择领取次数" label-width="100" prop="count" borderBottom ref="item1"
          @click="showCount = true;">
          <u--input v-model="model.count" readonly border="none" placeholder="点击选择领取次数"></u--input>
        </u-form-item>
      </u--form>
      <u-button @click="onChangefruit" text="领取水果卡" class="custom-style"></u-button>

    </view>

    <!-- {{ addressList }}
        {{ fruitLenght }} -->
    <u-picker keyName="addressDetails" closeOnClickOverlay :show="showAdress" :columns="addressList" title="请选择地址"
      @close="showAdress = false" @confirm="addressSelect">
    </u-picker>
    <u-picker closeOnClickOverlay :show="showCount"
      :columns="[Array.from({ length: fruitLenght }, (value, index) => index + 1)]" title="请选择领取次数"
      @close="showAdress = false" @confirm="countSelect">
    </u-picker>
  </view>
</template>
<style>
.site-title {
  font-size: 42upx;
  margin-top: 50upx;
  font-weight: bold;
}

.custom-style {
  background: linear-gradient(135deg, #F7978A 0%, #F05549 100%);
  padding: 8px 0px;
  color: #F5F5F6;
  font-size: 28px !important;
  border-radius: 40upx;
}

.text-normal-c {
  color: #F05549;
}

.site-form-item {
  background: #F0F1F5;
  padding: 10upx 30upx;
  margin: 20upx;
  border: none !important;
  border-radius: 20upx;
}

.site-page {
  padding: 25upx;
  border-radius: 20upx;
  margin: 25upx;
  background: #fff;

}
</style>
<script>
import publics from "@/common/utils/public.js"
import keyWords from "@/components/bian-keywords/index.vue"
import {
  getFruitUnclaimedOrder
} from '@/api/system/user.js'
import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
import url from "@/common/http/url.js"
import { sendRequestJson } from "@/common/http/api.js"

export default {
  mixins: [MescrollMixin], // 使用mixin
  components: { keyWords },
  data () {
    return {
      rsaKey: null,
      showKeyWord: false,
      showCount: false,
      showAdress: false,
      model: {
        addressName: null,
        addressId: null,
        count: null,
        payPassword: '',
      },
      rules: {
        addressName: {
          type: 'string',
          required: true,
          message: '请选择收货地址',
          trigger: ['blur', 'change']
        },
        payPassword: {
          type: 'string',
          required: true,
          message: '请填写输入密码',
          trigger: ['blur', 'change']
        },
      },
      fruitLenght: 0,
      onPageReset: false,
      fruitId: null,
      isaddressStatus: false,
      addressList: [],
      show: false,
      dataList: [],
      tabsIndex: 0,
      query: {
        type: 1
      },
      tabsList: [
        {
          name: '未使用'
        }, {
          name: "已使用"
        }
      ],
      addressData: {},
    }
  },
  filters: {
    filterAddr (code) {
      if (!code) return
      let addrObj = publics.getLngAndLatByCode(code)
      let addrName = ""
      if (addrObj) {
        addrObj.map(v => {
          addrName = addrName + v.name
        })
      }
      return addrName
    }
  },
  async onShow () {
    this.onPageReset = false
    this.$nextTick(async () => {
      try {
        await Promise.all([
          this.$http("GET", 'address/list', { pageSize: 1, type: 1 }),
          this.$http("GET", 'fruit/orderDraw/enableCount')
        ]).then(res => {
          console.log(res)
          if (res[0].data.records?.length == 0) {
            uni.navigateTo({
              url: `/pages/user/address/index?type=1&source=true`
            });
          } else {
            this.addressList = [res[0].data.records]

          }
          // this.addressList.push({
          //   name: '测试用户',
          //   id: '1'
          // })
          this.fruitLenght = res[1].data.enableCount
          this.onPageReset = true
        })
      } catch (error) {
        this.onPageReset = true
      }

    })

  },
  async onLoad (opt) {
    this.fruitId = opt.id
    let res = await this.$http("GET", url.login.getPublicKey)
    this.rsaKey = res.data
    console.log(res.data)
  },
  methods: {
    async onConfirm (msg) {
      this.showKeyWord = false
      uni.showLoading({
        mask: true
      })
      console.log(this.rsaKey, 'rsaKey')
      console.log(msg, 'msg')
      let password = await publics.passwordEncryption(this.rsaKey, msg)
      sendRequestJson("POST", 'fruit/orderDraw/drawFruit', {
        addressId: this.model.addressId,
        count: this.model.count,
        payPassword: password,
        fruitId: this.fruitId
      }).then(res => {
        uni.hideLoading()
        uni.navigateBack({
          delta: 1,//返回层数，2则上上页
        })
      })
      // this.$http("POST", 'fruit/orderDraw/drawFruit', {
      //   addressId: this.model.addressId,
      //   count: this.model.count,
      //   payPassword: password,
      //   fruitId: this.fruitId
      // }).then(res => {
      //   uni.hideLoading()
      //   uni.navigateBack({
      //     delta: 1,//返回层数，2则上上页
      //   })
      // })

    },
    onChangefruit () {
      this.model.addressId = this.addressData.id
      this.model.addressName = this.addressData.addressDetails
      if (!this.model.addressName) return uni.showToast({
        title: "请选择收货地址",
        icon: 'error'
      })

      if (!this.model.count) return uni.showToast({
        title: "请选择领取次数",
        icon: 'error'

      })
      this.showKeyWord = true

    },
    countSelect (e) {
      console.log(e)
      this.model.count = e.value[0]
      this.showCount = false

    },
    // 选择收货地址
    selectAddr () {
      this.$navigateTo("/pages/user/address/index?type=1&source=true&id=" + this.addressData.id)
    },
    addressSelect (e) {
      console.log(e, '123')
      this.model.addressId = e.value[0].id
      this.model.addressName = e.value[0].addressDetails
      console.log(e)
      this.showAdress = false

    },
    async openModel () {
      uni.navigateTo({
        url: `/pages/user/Fruit/form?id=${id}`
      });
    },


  }
}
</script>
