<!--
  Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.

  https://www.mall4j.com/

  未经允许，不可做商业用途！

  版权所有，侵权必究！
-->

<template>
  <!-- 结算账户 新增/编辑 -->
  <!-- :style="{height:windowHeight+'px'}" -->
  <view class="Mall4j page-store-settings-account">
    <view class="uni-forms-box">
      <uni-forms
        ref="accountFormRef"
        :model-value="accounts"
        :rules="rules"
        validate-trigger="submit"
      >
        <!-- 银行名称 -->
        <view class="setting-info-item">
          <uni-forms-item name="bankName">
            <view class="title">
              <text class="star">
                *
              </text>
              <text class="txt">
                {{ $t('yinHangMingCheng') }}
              </text>
            </view>
            <view class="input-box">
              <uni-easyinput
                v-model="accounts.bankName"
                :placeholder="$t('qingShuRuYinHangMingCheng')"
                maxlength="20"
                @blur="checkInput('bankName')"
              />
            </view>
          </uni-forms-item>
        </view>

        <!-- 开户行 -->
        <view class="setting-info-item">
          <uni-forms-item name="openingBank">
            <view class="title">
              <text class="star">
                *
              </text>
              <text class="txt">
                {{ $t('yinHangKaiHuHang') }}
              </text>
            </view>
            <view class="input-box">
              <uni-easyinput
                v-model="accounts.openingBank"
                :placeholder="$t('qingShuRuYinHangKaiHuHang')"
                maxlength="20"
                @blur="checkInput('openingBank')"
              />
            </view>
          </uni-forms-item>
        </view>

        <!-- 账户名 -->
        <view class="setting-info-item">
          <uni-forms-item name="recipientName">
            <view class="title">
              <text class="star">
                *
              </text>
              <text class="txt">
                {{ $t('zhangHuMing') }}
              </text>
            </view>
            <view
              name="recipientName"
              class="input-box"
            >
              <uni-easyinput
                v-model="accounts.recipientName"
                :placeholder="$t('qingShuRuZhangHuMing')"
                maxlength="20"
                @blur="checkInput('recipientName')"
              />
            </view>
          </uni-forms-item>
        </view>

        <!-- 账户 -->
        <view class="setting-info-item">
          <uni-forms-item name="cardNo">
            <view class="title">
              <text class="star">
                *
              </text>
              <text class="txt">
                {{ $t('zhangHu') }}
              </text>
            </view>
            <view class="input-box">
              <uni-easyinput
                v-model="accounts.cardNo"
                :placeholder="$t('qingShuRuYinHangKaHao')"
                maxlength="30"
                @blur="checkInput('cardNo')"
              />
            </view>
          </uni-forms-item>
        </view>

        <!-- 账户类型 -->
        <view class="setting-info-item">
          <uni-forms-item>
            <view class="title">
              <text class="star">
                *
              </text>
              <text class="txt">
                {{ $t('zhangHuLeiXing') }}
              </text>
            </view>
            <view class="radio-con">
              <radio-group @change="changeAccountType">
                <!-- margin-right:50rpx; -->
                <radio
                  style="transform:scale(0.8);"
                  value="1"
                  color="#082BA6"
                  :checked="accounts.isDefault === 1"
                >
                  <text class="radio-txt">
                    {{ $t('moRenZhangHu') }}
                  </text>
                </radio>
                <radio
                  style="transform:scale(0.8)"
                  value="0"
                  color="#082BA6"
                  :checked="accounts.isDefault === 0"
                >
                  <text class="radio-txt">
                    {{ $t('puTongZhangHu') }}
                  </text>
                </radio>
              </radio-group>
            </view>
          </uni-forms-item>
        </view>
      </uni-forms>
    </view>
    <!-- 添加账户 -->
    <view
      v-if="reqType"
      class="account-footer"
    >
      <button
        type="default"
        class="button"
        hover-class="none"
        @tap="submit()"
      >
        {{ $t('queDing') }}
      </button>
    </view>
    <!-- 编辑账户 -->
    <view
      v-if="!reqType"
      class="account-footer account-edit"
    >
      <button
        v-if="$isAuthorities('shop:shopBankCard:update')"
        class="button"
        type="default"
        hover-class="none"
        @tap="putShopBankCard()"
      >
        {{ $t('baoCunZhangHu') }}
      </button>
      <button
        v-if="$isAuthorities('shop:shopBankCard:delete')"
        class="button"
        type="warn"
        hover-class="none"
        @tap="delShopBankCard()"
      >
        {{ $t('shanChuZhangHu') }}
      </button>
    </view>
  </view>
</template>

<!-- eslint-disable n/no-callback-literal -->
<script setup>
import { reactive } from 'vue'

const $isAuthorities = util.isAuthorities
const Data = reactive({
  // 编辑用户信息
  accounts: {
    bankName: '',
    openingBank: '',
    recipientName: '',
    cardNo: '',
    isDefault: 0
  },
  // 默认账户
  isDefault: 0,
  rules: {
    bankName: {
      rules: [
        { required: true, errorMessage: $t('qingShuRuYinHangMingCheng-0') },
        {
          validateFunction: (rule, value, data, callback) => {
            if (value.length < 2 || value.length > 20) {
              callback($t('yinHangMingChengChangDuZai_220GeZiFu'))
              return false
            }
            return true
          }
        }
      ]
    },
    openingBank: {
      rules: [
        { required: true, errorMessage: $t('qingShuRuYinHangKaiHuHang') },
        {
          validateFunction: (rule, value, data, callback) => {
            if (value.length < 2 || value.length > 20) {
              callback($t('yinHangKaiHuHangChangDuZai_220GeZiFu'))
              return false
            }
            return true
          }
        }
      ]
    },
    recipientName: {
      rules: [
        { required: true, errorMessage: $t('qingShuRuZhangHuMing') },
        {
          validateFunction: (rule, value, data, callback) => {
            if (value.length < 2 || value.length > 20) {
              callback($t('zhangHuMingChangDuZai_220GeZiFu'))
              return false
            }
            return true
          }
        }
      ]
    },
    cardNo: {
      rules: [
        { required: true, errorMessage: $t('qingShuRuZhangHu') },
        {
          validateFunction: (rule, value, data, callback) => {
            const reg = /^([0-9]{1})(\d{7,29})$/
            if (!reg.test(value)) {
              callback($t('qingShuRuZhengQueDeYinHangKaHao-0'))
              return false
            }
            return true
          }
        }
      ]
    }
  },
  reqType: true, //  新增用户/编辑用户
  windowHeight: 720 // 显示高度
})

const { accounts, rules, reqType } = toRefs(Data)

onLoad((options) => {
  getSysInfo()
  if (!options.id) {
    // 头部导航标题
    uni.setNavigationBarTitle({
      title: $t('xinZengZhangHu')
    })
  } else {
    Data.reqType = false
    // 头部导航标题
    uni.setNavigationBarTitle({
      title: $t('bianJiZhangHu')
    })
    getShopBankCard(options.id)
  }
})

onShow(() => {
  getCurrentPages()
  uni.setNavigationBarTitle({
    title: $t('jieSuanZhangHu-0')
  })
})

const getSysInfo = () => {
  uni.getSystemInfo({
    success: (res) => {
      Data.windowHeight = res.windowHeight
    }
  })
}

/**
  * 切换单选
  */
const changeAccountType = (val) => {
  Data.isDefault = Number(val.detail.value)
}

/**
 * 检查输入框空格
 */
const checkInput = (type) => {
  const reg = /^\s+$/g
  if (type === 'bankName' && reg.test(Data.accounts.bankName)) {
    Data.accounts.bankName = ''
  }
  if (type === 'openingBank' && reg.test(Data.accounts.openingBank)) {
    Data.accounts.openingBank = ''
  }
  if (type === 'recipientName' && reg.test(Data.accounts.recipientName)) {
    Data.accounts.recipientName = ''
  }
  if (type === 'cardNo' && reg.test(Data.accounts.cardNo)) {
    Data.accounts.cardNo = ''
  }
}

/**
 *  添加引号卡信息
 */
const addShopBankCard = () => {
  http.request({
    url: '/shop/shopBankCard',
    data: Data.accounts,
    method: 'POST'
  }).then(({ data }) => {
    setIsDefault(data)
  })
}

const accountFormRef = ref()
/**
 * 提交表单
 */
const submit = () => {
  accountFormRef.value.validate().then(() => {
    addShopBankCard()
  })
}

/**
 * @param {shopBankCardId} Number
 * 通过shopBankCardId获取银行卡信息
 */
const getShopBankCard = (shopBankCardId) => {
  http.request({
    url: '/shop/shopBankCard',
    data: {
      shopBankCardId
    },
    method: 'GET'
  }).then(({ data }) => {
    Data.accounts = data
  })
}

/**
 * 编辑用户
 */
const putShopBankCard = () => {
  accountFormRef.value.validate().then(() => {
    http.request({
      url: '/shop/shopBankCard',
      data: Data.accounts,
      method: 'PUT'
    }).then(() => {
      setIsDefault(Data.accounts.shopBankCardId)
    })
  })
}

/**
 * 删除用户
 */
const delShopBankCard = () => {
  uni.showModal({
    title: $t('tiShi'),
    content: $t('qingQueDingShiFouShanChuGaiZhangHu'),
    success: function (res) {
      if (res.confirm) {
        http.request({
          url: '/shop/shopBankCard/' + Data.accounts.shopBankCardId,
          method: 'DELETE'
        }).then(() => {
          uni.redirectTo({
            url: '/package-settings/pages/store-settings-financial/store-settings-financial'
          })
        })
      }
    }
  })
}

/**
 * @param {Number} shopBankCardId  银行卡id
 */
const setIsDefault = (shopBankCardId) => {
  // 是否修改账户默认类型
  if (Data.accounts.isDefault === Data.isDefault) {
    return uni.redirectTo({ url: '/package-settings/pages/store-settings-financial/store-settings-financial' })
  }
  http.request({
    url: '/shop/shopBankCard/' + (Data.isDefault ? 'setDefault' : 'setNotDefault?shopBankCardId=' + shopBankCardId),
    method: 'PUT',
    data: { shopBankCardId }
  }).then(() => {
    uni.redirectTo({
      url: '/package-settings/pages/store-settings-financial/store-settings-financial'
    })
  })
}
</script>

<style lang="scss" scoped>
@use "store-settings-account";
</style>
