<template>
    <view class="container" style="paddingBottom:1rpx;">
      <u-navbar
			leftIconSize="40rpx"
			leftIconColor="#333333"
			title="信用卡管理"
		>
		</u-navbar>
      <view class="section">
        <view class="section-title">{{ title}}</view>
        <view class="form-view">
          <u--form labelPosition="left" :model="form" :rules="rules" ref="uForm" label-width="auto"
            :labelStyle="{ color: '#333333', fontSize: '30rpx' }">
            <u-form-item label="信用卡名称" prop="name" required >
              <u--input v-model="form.name"  placeholder="请填写信用卡名称"
                inputAlign="right" border="none"></u--input>
            </u-form-item>
            <u-form-item label="信用卡卡号" prop="code" required >
              <u--input v-model="form.code"  placeholder="请填写信用卡卡号"
                inputAlign="right" border="none"></u--input>
            </u-form-item>
            <u-form-item label="信用卡额度" required prop="creditLimit" >
              <u--input v-model="form.creditLimit"  placeholder="请填写信用卡额度"
                inputAlign="right" border="none"></u--input>
            </u-form-item>
            <u-form-item label="账单日" required prop="billDate" >
              <u--input v-model="form.billDate"  placeholder="请填写账单日"
                inputAlign="right" border="none"></u--input>
            </u-form-item>
            <u-form-item label="还款日" required prop="payDate" >
              <u--input v-model="form.payDate"  placeholder="请填写还款日"
                inputAlign="right" border="none"></u--input>
            </u-form-item>
            <u-form-item label="信用卡安全码" prop="cvv" >
              <u--input v-model="form.cvv"  placeholder="请填写信用卡安全码"
                inputAlign="right" border="none"></u--input>
            </u-form-item>
          
            <u-form-item label="开户行" prop="openingBank" >
              <u--input v-model="form.openingBank"  placeholder="请填写开户行"
                inputAlign="right" border="none"></u--input>
            </u-form-item>
            <u-form-item label="开户日期" prop="activationDate"  @click="selectDate(1)">
              <u--input v-model="form.activationDate" disabled disabledColor="#ffffff" placeholder="请选择开户日期" inputAlign="right" border="none"></u--input>
						<u-icon slot="right" name="arrow-right"></u-icon>
            </u-form-item>
            <u-form-item label="宽限期" prop="delayPeriod" >
              <u--input v-model="form.delayPeriod"  placeholder="请填写宽限期"
                inputAlign="right" border="none"></u--input>
            </u-form-item>
            <u-form-item label="有效期" prop="effectiveDate"  @click="selectDate(2)">
              <u--input v-model="form.effectiveDate" disabled disabledColor="#ffffff" placeholder="请选择有效期" inputAlign="right" border="none"></u--input>
						<u-icon slot="right" name="arrow-right"></u-icon>
            </u-form-item>
            <u-form-item label="账单日消费" prop="isNextBillDateName"  @click="handleShowIsNextBillDate">
              <u--input v-model="form.isNextBillDateName" disabled disabledColor="#ffffff" placeholder="请选择账单日消费"
                inputAlign="right" border="none"></u--input>
              <u-icon slot="right" name="arrow-down"></u-icon>
            </u-form-item>
            <u-form-item label="账单日时间" prop="nextBillDateTime" >
              <u--input v-model="form.nextBillDateTime"  placeholder="请填写账单日时间"
                inputAlign="right" border="none"></u--input>
            </u-form-item>
            <u-form-item label="0账单操作" prop="isZeroBillName"  @click="handleShowIsZeroBill">
              <u--input v-model="form.isZeroBillName" disabled disabledColor="#ffffff" placeholder="请选择0账单操作"
                inputAlign="right" border="none"></u--input>
              <u-icon slot="right" name="arrow-down"></u-icon>
            </u-form-item>
            <u-form-item label="备注" prop="remark"  labelPosition="top">
              <u--textarea v-model="form.remark" placeholder="请填写备注" border="none" autoHeight inputAlign="right" count
                maxlength="20000" style="padding:18rpx 0;"></u--textarea>
            </u-form-item>
          </u--form>
          <view class="form-btn">
            <u-button type="primary" text="提交" @click="submit"></u-button>
          </view>
        </view>
      </view>
      <u-toast ref="uToast"></u-toast>
      <u-picker itemHeight="88" :show="showIsNextBillDate" :columns="isNextBillDateList" keyName="dictLabel" @cancel="handleCancel"
        @confirm="handleConfirm"></u-picker>
        <u-picker itemHeight="88" :show="showIsZeroBill" :columns="isZeroBillList" keyName="dictLabel" @cancel="handleIsZeroBillCancel"
        @confirm="handleIsZeroBillConfirm"></u-picker>
        <u-datetime-picker
			:show="datePickShow"
			mode="date"
      ref="createTimeRef" 
			@cancel="datePickShow=false"
			@confirm="datePickConfirm"
			itemHeight="88"
		></u-datetime-picker>
    </view>
  </template>
  
  <script setup>
import { getBankcardLend, addBankcardLend, updateBankcardLend } from '@/api/invest/bankcardlend'
  import { getDicts } from '@/api/system/dict/data.js'
  const { proxy } = getCurrentInstance()
  import dayjs from 'dayjs'
  import {onLoad,onReady} from "@dcloudio/uni-app";
    // 计算属性与监听属性是在vue中而非uniap中 需要注意！！！
import {reactive ,toRefs,ref,computed ,getCurrentInstance }from "vue";
const datePickShow = ref(false)
const showIsNextBillDate = ref(false)
const showIsZeroBill = ref(false)
const title = ref("信用卡管理")
const isNextBillDateList = ref([])
const isZeroBillList = ref([])
const dateType = ref(1)
const data = reactive({
  form: {
    id: null,
    name: null,
    type: '2',
    code: null,
    openingBank: null,
    activationDate: null,
    billDate: null,
    payDate: null,
    delayPeriod: null,
    creditLimit: null,
    effectiveDate: null,
    cvv: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    delFlag: null,
    remark: null,
    debitType: null,
    isNextBillDate: null,
    nextBillDateTime: null,
    isZeroBill: null
  },
  // dateType:'1',
  rules: {
          name: [{ type: 'string', required: true, message: '信用卡名称不能为空', trigger: ['change', 'blur'] }],
          code: [{ type: 'string', required: true, message: '信用卡卡号不能为空', trigger: ['change', 'blur'] }],
          billDate: [{ type: 'number', required: true, message: '账单日不能为空', trigger: ['change', 'blur'] }],
          payDate: [{ type: 'number', required: true, message: '还款日不能为空', trigger: ['change', 'blur'] }],  
          creditLimit: [{ type: 'number', required: true, message: '信用卡额度不能为空', trigger: ['change', 'blur'] }],  
  }
})
const { form, rules} = toRefs(data)

onLoad((option) => {
       form.value.id = option.id
       if(form.value.id!=null){
        title.value="信用卡管理-修改"
       }else{
        title.value="信用卡管理-新增"
       }
      getDict()
  })
  onReady(() => {
    form.value.activationDate = dayjs(new Date().getTime()).format("YYYY-MM-DD")
    form.value.effectiveDate = dayjs(new Date().getTime()).format("YYYY-MM-DD")
  })
  function  getDict() {
        // 类型
        getDicts('is_next_bill_date').then(res => {
        isNextBillDateList.value =[res.data] 
      })
       // 类型
       getDicts('is_zero_bill').then(res => {
        isZeroBillList.value =[res.data] 
      })
      if(form.value.id!=null){
        getBankcardLend(form.value.id).then(res => {
          form.value = res.data
           // 类型
        getDicts('is_next_bill_date').then(result => {
          form.value.isNextBillDateName=dictStr(form.value.isNextBillDate, result.data)
      })
          // 类型
          getDicts('is_zero_bill').then(result => {
          form.value.isZeroBillName=dictStr(form.value.isZeroBill, result.data)
      })
          
        })
      }
  }
  function dictStr(val, arr) {
      let str = ''
      arr.map(item => {
        if (item.dictValue === val) {
          str = item.dictLabel
        }
      })
      return str
    }

  function handleShowIsNextBillDate() {
        if (isNextBillDateList.value[0].length === 0) {
          proxy.$refs['uToast'].show({
            message: '账单日消费为空  ', type: 'warning'
          })
        } else {
          showIsNextBillDate.value = true
        }
      }
      function   handleConfirm(e) {
        form.value.isNextBillDateName = e.value[0].dictLabel
        form.value.isNextBillDate = e.value[0].dictValue
        showIsNextBillDate.value = false
      }
      function handleCancel() {
       showIsNextBillDate.value = false
      }
      function handleShowIsZeroBill() {
        if (isZeroBillList.value[0].length === 0) {
          proxy.$refs['uToast'].show({
            message: '0账单操作为空  ', type: 'warning'
          })
        } else {
          showIsZeroBill.value = true
        }
      }
      function   handleIsZeroBillConfirm(e) {
        form.value.isZeroBillName = e.value[0].dictLabel
        form.value.isZeroBill = e.value[0].dictValue
        showIsZeroBill.value = false
      }
      function handleIsZeroBillCancel() {
       showIsZeroBill.value = false
      }
      function  selectDate(type) {
        dateType.value=type
				datePickShow.value = true
        proxy.$refs['createTimeRef'].innerValue = new Date().getTime()
			}
			function datePickConfirm(e) {
         if(dateType.value===1){
          form.value.activationDate = dayjs(e.value).format("YYYY-MM-DD")
          }
          if(dateType.value===2){
          form.value.effectiveDate = dayjs(e.value).format("YYYY-MM-DD")
          }
				datePickShow.value = false
			}
      function submit() {
        proxy.$refs['uForm'].validate().then(() => {
          if (form.value.id != null) {
            updateBankcardLend(form.value).then(res => {
              proxy.$refs['uToast'].show({
              message: '修改成功', complete() {
                uni.navigateTo({ url: `/pages/work/base/creditCard/list` })
              }
            })
          })
          }else {
            addBankcardLend(form.value).then(res => {
              proxy.$refs['uToast'].show({
              message: '新增成功', complete() {
                uni.navigateTo({ url: `/pages/work/base/creditCard/list` })
              }
            })
          })
          }
        })
      }
  </script>
  
  <style lang="scss" scoped>
  .section {
    margin: 24rpx;
    padding: 16rpx 24rpx;
    background-color: #fff;
    border-radius: 8rpx;
  
    .section-title {
      width: 360rpx;
      color: #333333;
      line-height: 44rpx;
      font-size: 30rpx;
      border-left: 6rpx solid #2681FF;
      padding-left: 26rpx;
    }
  
    .form-view {
      padding: 20rpx 0rpx 0 10rpx;
  
      .form-btn {
        padding-top: 20rpx;
      }
    }
  }
  </style>