<!--
 * @Descripttion: 
 * @version: 
 * @Author: 王三华
 * @Date: 2023-10-17 14:09:39
-->
<template>
  <view class="content">
    <view class="kuang"
          v-for="(item,index) in checkAdressList"
          :key="index">
      <view class="tt">
        <u--form labelPosition="left"
                 labelWidth="95px"
                 :model="item"
                 ref="uForm">
          <u-form-item :label="'订单编号'">
            <u--input fontSize="12"
                      disabled
                      v-model="item.orderCode"></u--input>
          </u-form-item>
          <u-form-item :label="'收货人'">
            <u--input fontSize="12"
                      v-model="item.recName"></u--input>
          </u-form-item>
          <u-form-item :label="'联系方式'">
            <u--input fontSize="12"
                      v-model="item.recPhone"></u--input>
          </u-form-item>

          <u-form-item :label="'收货地址'"
                       @click='getDiqu(index)'>
            <u--input disabled
                      fontSize="12"
                      disabledColor="#ffffff"
                      v-model="item.nameList"
                      suffixIcon="arrow-down"
                      suffixIconStyle="font-size: 12px;"></u--input>
          </u-form-item>
          <u-form-item label="详细地址">
            <u--textarea fontSize="12"
                         v-model="item.recAddress"></u--textarea>
          </u-form-item>
          <u-form-item :label="'扣款币种'">
            <u--input fontSize="12"
                      disabled
                      v-model="item.accountKeyVal"></u--input>
          </u-form-item>
          <u-form-item label="扣款金额(￥)">
            <u--input fontSize="12"
                      disabled
                      v-model="item.cost"></u--input>
          </u-form-item>
        </u--form>
      </view>
    </view>
    <view class="zhan"></view>
    <view class="table_a">
      <view class="table_f">
        <u-button size="small"
        @click="toBack"
                :text="'取消'"></u-button>
        <u-button size="small"
                  @click="saveAdress(1)"
                  color="rgba(0, 155, 88, 1)"
                  v-hasButtons="['orderAdressMq']"
                  :text="'免签'"></u-button>
        <u-button size="small"
                  @click="saveAdress(2)"
                  color="rgba(243, 169, 0, 1)"
                  v-hasButtons="['orderAdressMf']"
                  :text="'免付'"></u-button>
        <u-button size="small"
                  @click="saveAdress(3)"
                  color="rgba(228, 27, 27, 1)"
                  v-hasButtons="['orderAdressQm']"
                  :text="'全免'"></u-button>
        <u-button size="small"
                  @click="saveAdress(0)"
                  color="rgba(72, 178, 253, 1)"
                  v-hasButtons="['orderAdressQr']"
                  :text="'确认'"></u-button>
      </view>
    </view>
    <!-- 选择地址 -->
    <v-address ref="address"
               :defaultCode="defaultCode"
               @addressData="addressData"></v-address>
  </view>
</template>

<script>
import address from '@/components/address.vue'
import * as api from '@/api/information'
export default {
  components: {
    'v-address': address,
  },
  data() {
    return {
      checkAdressList: [],
      oCheckAdressList: [],
      torderCodeList: '',
      defaultCode: [],
      changeIndex: 0,
    }
  },
  onLoad(options) {
    this.torderCodeList = options.order
    this.getData()
  },
  methods: {
    getDiqu(index) {
      this.changeIndex = index
      this.$refs.address.setShow()
    },
    getData() {
      api.areaList().then((res) => {
        this.areaData = res.data
        this.getList()
      })
    },
    addressData(diqu, obj) {
      let index = this.changeIndex
      this.checkAdressList[index].nameList = diqu
      this.checkAdressList[index].recProvince = obj.province
      this.checkAdressList[index].recCity = obj.city
      this.checkAdressList[index].recCounty = obj.county
      this.$forceUpdate()
    },
    getList() {
      api
        .getOrderBusinessAddressData({ orderList: this.torderCodeList })
        .then((res) => {
          // ssq
          res.data.forEach((item) => {
            if (item.recCounty) {
              this.$set(item, 'ssq', [
                item.recProvince + '',
                item.recCity + '',
                item.recCounty + '',
              ])
            } else {
              this.$set(item, 'ssq', [item.recProvince + '', item.recCity + ''])
            }
          })
          this.checkAdressList = res.data
          this.oCheckAdressList = JSON.parse(JSON.stringify(res.data))
          // 把地址id转成汉字地址
          let arr = this.flatten(this.areaData)
          this.oCheckAdressList.forEach((btem) => {
            btem.nameList = ''
            arr.forEach((item) => {
              btem.ssq.forEach((ctem) => {
                if (ctem == item.id) {
                  btem.nameList += item.name
                }
              })
            })
          })
          this.checkAdressList.forEach((btem) => {
            btem.nameList = ''
            arr.forEach((item) => {
              btem.ssq.forEach((ctem) => {
                if (ctem == item.id) {
                  btem.nameList += item.name + ' '
                }
              })
            })
          })
          // 把旧地址放入新地址列表
          this.checkAdressList.forEach((item) => {
            this.oCheckAdressList.forEach((ctem) => {
              item.oldList = ctem
            })
          })
          console.log(
            '%c [  this.checkAdressList ]-121',
            'font-size:13px; background:#45a97f; color:#89edc3;',
            this.checkAdressList
          )
          this.$forceUpdate()
        })
    },
    toBack(){
      uni.navigateBack({ delta: 1 })
    },
    saveAdress(index) {
      api.addressCheck({ orderList: this.torderCodeList }).then((res) => {
        if (res.code != 200) return false
        let obj
        if (index == 0) {
          obj = {
            itemList: this.checkAdressList,
            payFlag: 1,
          }
          uni.navigateTo({
            url:
              '/pages/information/fixAddress/onlineSign?data=' +
              JSON.stringify(obj) +
              '&index=231',
          })

          // 免签 payFlag == 1
        } else if (index == 1) {
          obj = {
            itemList: this.checkAdressList,
            payFlag: 1,
          }
          api.addressFree([obj]).then((res) => {
            uni.showToast({
              title: res.msg,
              icon: 'none',
              duration: 1500,
            })
            setTimeout(()=>{
              uni.navigateBack({ delta: 1 })
            },1500)
            
          })
          // 免付 payFlag == 0
        } else if (index == 2) {
          obj = {
            itemList: this.checkAdressList,
            payFlag: 0,
          }
          uni.navigateTo({
            url:
              '/pages/information/fixAddress/onlineSign?data=' +
              JSON.stringify(obj) +
              '&index=231',
          })
          // 全免 payFlag == 0
        } else if (index == 3) {
          obj = {
            itemList: this.checkAdressList,
            payFlag: 0,
          }
          api.addressFree([obj]).then((res) => {
            if (res.code == 200) {
              uni.showToast({
              title: res.msg,
              icon: 'none',
              duration: 1500,
            })
            setTimeout(()=>{
              uni.navigateBack({ delta: 1 })
            },1500)
            }
          })
        }
      })
    },
    flatten(arr) {
      return arr.reduce((result, item) => {
        return result.concat(
          item,
          Array.isArray(item.children) ? this.flatten(item.children) : []
        )
      }, [])
    },
  },
}
</script>

<style lang="scss" scoped>
.content {
  background: #f9f9f9;
  // padding: 20rpx;
  min-height: 94vh;
  padding: 20rpx 0;
}
.kuang {
  margin: 20rpx;
  background: #fff;
  border-radius: 10px 10px 10px 10px;
  padding: 20rpx;
}
.zhan {
  height: 100px;
}
.table_a {
  position: fixed;
  width: 100%;
  bottom: 0;
  background: #fff;
  padding: 20rpx 0 40px 0;
}
.table_f {
  display: flex;

  uni-button {
    margin: 20rpx 10rpx;
  }
}
</style>