<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="outAccountName" required @click="handleOutAccount">
              <u--input v-model="form.outAccountName" 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="inAccountName" required @click="handleInAccount">
              <u--input v-model="form.inAccountName" 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="createTime" required @click="selectDate()">
              <u--input v-model="form.createTime" 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="amount" required >
              <u--input v-model="form.amount"  placeholder="请填写转账金额"
                inputAlign="right" border="none"></u--input>
            </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="showOutAccount" :columns="outAccountList" keyName="nameCodeAvailableLimit" @cancel="handleOutAccountCancel"
        @confirm="handleOutAccountConfirm"></u-picker>
        <u-picker itemHeight="88" :show="showInAccount" :columns="inAccountList" keyName="nameCodeAvailableLimit" @cancel="handleInAccountCancel"
        @confirm="handleInAccountConfirm"></u-picker>
        <u-datetime-picker
			:show="datePickShow"
			mode="datetime"
      ref="createTimeRef" 
			@cancel="datePickShow=false"
			@confirm="datePickConfirm"
			itemHeight="88"
		></u-datetime-picker>
    </view>
  </template>
  
  <script setup>
  import {getAccountsTransferRecord, addAccountsTransferRecord, updateAccountsTransferRecord } from '@/api/invest/accountsTransferRecord'
import { listAccounts } from '@/api/invest/accounts'
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 showOutAccount = ref(false)
const showInAccount = ref(false)
const title = ref("储蓄账户记账")
const inAccountList = ref([])
const outAccountList = ref([])
const data = reactive({
  form: {
    id: null,
    name: null,
    type: '4',
    outAccountId: null,
    inAccountId: null,
    posId: null,
    commission: null,
    amount: null,
    actualAmount: null,
    dealType: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    delFlag: null,
    remark: null
  },
  queryOutAccountParams: {
    pageNum: 1,
    type: '1',
    state: '1',
    pageSize: 1000
  },
  queryInAccountParams: {
    pageNum: 1,
    type: '1',
    state: '1',
    pageSize: 1000
  },
  rules: {
    inAccountName: [{ required: true, message: '转入账户不能为空', trigger:['change', 'blur'] }],
    outAccountName: [{ required: true, message: '转出账户不能为空', trigger: ['change', 'blur'] }],
    amount: [{ required: true, message: '转账金额不能为空', trigger: ['change', 'blur'] }],
    createTime: [{ required: true, message: '转账时间不能为空', trigger: ['change', 'blur'] }]
  }
})
const { form, queryOutAccountParams, queryInAccountParams, rules} = toRefs(data)


onLoad((option) => {
       form.value.id = option.id
       if(form.value.id!=null){
        title.value="储蓄账户记账-修改"
       }else{
        title.value="储蓄账户记账-新增"
       }
      getData()
  })
  onReady(() => {
       form.value.createTime = dayjs(new Date().getTime()).format("YYYY-MM-DD HH:mm:ss")
  })
  function  getData() {
      listAccounts(queryInAccountParams.value).then((response) => {
        inAccountList.value = [response.rows] 
      })
      listAccounts(queryOutAccountParams.value).then((response) => {
       outAccountList.value = [response.rows] 
       })
      if(form.value.id!=null){
        getAccountsTransferRecord(form.value.id).then(res => {
          form.value = res.data   
        })
      }
  }

  function handleOutAccount() {
        if (outAccountList.value[0].length === 0) {
          proxy.$refs['uToast'].show({
            message: '转出账户为空  ', type: 'warning'
          })
        } else {
          showOutAccount.value = true
        }
     }
  function   handleOutAccountConfirm(e) {
       form.value.outAccountName = e.value[0].nameCodeAvailableLimit
        form.value.outAccountId = e.value[0].id
        showOutAccount.value = false
        
  }
 function handleOutAccountCancel() {
       showOutAccount.value = false
    }
function handleInAccount() {
        if (inAccountList.value[0].length === 0) {
          proxy.$refs['uToast'].show({
            message: '转入账户为空  ', type: 'warning'
          })
        } else {
          showInAccount.value = true
        }
     }
  function   handleInAccountConfirm(e) {
        form.value.inAccountName = e.value[0].nameCodeAvailableLimit
        form.value.inAccountId = e.value[0].id
        showInAccount.value = false
  }
 function handleInAccountCancel() {
       showInAccount.value = false
  }
  function  selectDate() {
				datePickShow.value = true
        proxy.$refs['createTimeRef'].innerValue = new Date().getTime()
		}
	function datePickConfirm(e) {
				form.value.createTime = dayjs(e.value).format("YYYY-MM-DD HH:mm:ss")
				datePickShow.value = false
		}
  function submit() {
        proxy.$refs['uForm'].validate().then(() => {
          if (form.value.id != null) {
            updateAccountsTransferRecord(form.value).then(res => {
              proxy.$refs['uToast'].show({
              message: '修改成功', complete() {
                uni.navigateTo({ url: `/pages/work/accounts/debitTransferRecord/list` })
              }
            })
          })
          }else {
            addAccountsTransferRecord(form.value).then(res => {
              proxy.$refs['uToast'].show({
              message: '新增成功', complete() {
                uni.navigateTo({ url: `/pages/work/accounts/debitTransferRecord/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>