<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="180rpx"
            :labelStyle="{ color: '#333333', fontSize: '30rpx', marginRight: '24rpx' }">
            <u-form-item label="理财账户名称" prop="name" required >
              <u--input v-model="form.name"  placeholder="请填写理财账户名称"
                inputAlign="left" :customStyle="inputBaseStyle"></u--input>
            </u-form-item>
          
            <u-form-item label="账号" prop="code" required >
              <u--input v-model="form.code"  placeholder="请填写账号"
                inputAlign="left" :customStyle="inputBaseStyle"></u--input>
            </u-form-item>
            <u-form-item label="余额" required prop="balance" >
              <u--input v-model="form.balance"  type="number" placeholder="请填写余额"
                inputAlign="left" :customStyle="inputBaseStyle"></u--input>
            </u-form-item>
            <u-form-item label="账户状态" required prop="statusName"  @click="handleShowStatus">
              <view class="input-with-arrow">
                <u--input v-model="form.statusName" readonly disabledColor="#ffffff" placeholder="请选择账户状态"
                inputAlign="left" :customStyle="inputBaseStyle"></u--input>
                <text class="arrow-icon">▼</text>
              </view>
            </u-form-item>
            <!-- <u-form-item label="密码" prop="password"  >
              <u--input v-model="form.password"  placeholder="请填写密码"
                inputAlign="left" :customStyle="inputBaseStyle"></u--input>
            </u-form-item> -->
             <u-form-item label="开户日期" prop="activationDate"  @click="selectDate()">
              <view class="input-with-arrow">
                <u--input v-model="form.activationDate" readonly disabledColor="#ffffff" placeholder="请选择开户日期" inputAlign="left" :customStyle="inputBaseStyle"></u--input>
                <text class="arrow-icon">▼</text>
              </view>
            </u-form-item>
            <u-form-item label="关联储蓄卡" prop="bankNameCode"  @click="handleDebitCard">
              <view class="input-with-arrow">
                <u--input v-model="form.bankNameCode" readonly disabledColor="#ffffff" placeholder="请选择关联储蓄卡"
                inputAlign="left" :customStyle="inputBaseStyle"></u--input>
                <text class="arrow-icon">▼</text>
              </view>
            </u-form-item>
           
          
            <u-form-item label="佣金费率" prop="commission" >
              <u--input v-model="form.commission"  placeholder="请填写佣金费率"
                inputAlign="left" :customStyle="inputBaseStyle"></u--input>
            </u-form-item>
            <u-form-item label="备注" prop="remark"  labelPosition="top">
              <u--textarea v-model="form.remark" placeholder="请填写备注" autoHeight inputAlign="left" count
                maxlength="20000" style="border: 2rpx solid #dcdfe6 !important; height: 160rpx;"></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="showDebitCard" :columns="debitCardList" keyName="nameCodeAvailableLimit" @cancel="handleDebitCardCancel"
      @confirm="handleDebitCardConfirm"></u-picker>
      <u-picker itemHeight="88" :show="showStatus" :columns="statusList" keyName="dictLabel" @cancel="handleStatusCancel"
      @confirm="handleStatusConfirm"></u-picker>
        <u-datetime-picker
			:show="datePickShow"
			mode="date"
      :minDate="-2209017600000"
      ref="createTimeRef" 
			@cancel="datePickShow=false"
			@confirm="datePickConfirm"
			itemHeight="88"
		></u-datetime-picker>
    </view>
  </template>
  
  <script setup>
import { getFutureStocks, addFutureStocks, updateFutureStocks } from '@/api/invest/futureStocks'
  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 showDebitCard = ref(false)
const title = ref("理财账户管理")
const debitCardList = ref([])
const dateType = ref(1)
const showStatus = ref(false)
const statusList = ref([])
const inputBaseStyle = {
  background: '#ffffff',
  border: '2rpx solid #dcdfe6',
  borderRadius: '8rpx',
  padding: '0 24rpx',
  height: '68rpx',
  width: '100%',
  boxSizing: 'border-box'
}

const data = reactive({
  form: {
    id: null,
    name: null,
    type: '3',
    code: null,
    password: null,
    tradingCenterCode: null,
    tradingCenterPassword: null,
    fundPassword: null,
    commission: null,
    balance: null,
    availableLimit: null,
    bond: null,
    debitCard: null,
    activationDate: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    delFlag: null,
    remark: null,
    status: '1',
    statusName: '正常使用'
  },
  queryDebitCardParams: {
    pageNum: 1,
    type: '1',
    debitType: '1',
    status: '1',
    pageSize: 1000
  },
  // dateType:'1',
  rules: {
          name: [{ type: 'string', required: true, message: '理财账户名称不能为空', trigger: ['change', 'blur'] }],
          code: [{ type: 'string', required: true, message: '账号不能为空', trigger: ['change', 'blur'] }],
          balance: [{ type: 'number', required: true, message: '余额不能为空', trigger: ['change', 'blur'] }],  
          statusName: [{ type: 'string', required: true, message: '账户状态不能为空', trigger: ['change', 'blur'] }], 
  }
})
const { form, queryDebitCardParams, 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('account_status').then(res => {
        statusList.value =[res.data] 
      })
        // 类型
        listAccounts(queryDebitCardParams.value).then((response) => {
       debitCardList.value = [response.rows] 
       })
      
      if(form.value.id!=null){
        getFutureStocks(form.value.id).then(res => {
          form.value = res.data
          // 类型
       getDicts('account_status').then(result => {
          form.value.statusName=dictStr(form.value.status, result.data)
      })
        })
      }
  }
  function dictStr(val, arr) {
      let str = ''
      arr.map(item => {
        if (item.dictValue === val) {
          str = item.dictLabel
        }
      })
      return str
    }
    function handleDebitCard() {
        if (debitCardList.value[0].length === 0) {
          proxy.$refs['uToast'].show({
            message: '结算卡为空  ', type: 'warning'
          })
        } else {
          showDebitCard.value = true
        }
     }
     function handleShowStatus() {
        if (statusList.value[0].length === 0) {
          proxy.$refs['uToast'].show({
            message: '账户状态操作为空  ', type: 'warning'
          })
        } else {
          showStatus.value = true
        }
      }
      function   handleStatusConfirm(e) {
        form.value.statusName = e.value[0].dictLabel
        form.value.status = e.value[0].dictValue
        showStatus.value = false
      }
      function handleStatusCancel() {
       showStatus.value = false
      }
  function   handleDebitCardConfirm(e) {
       form.value.bankNameCode = e.value[0].nameCodeAvailableLimit
        form.value.debitCard = e.value[0].id
        showDebitCard.value = false    
  }
 function handleDebitCardCancel() {
       showDebitCard.value = false
    }
      function  selectDate(type) {
        dateType.value=type
				datePickShow.value = true
        proxy.$refs['createTimeRef'].innerValue = new Date().getTime()
			}
			function datePickConfirm(e) {
          form.value.activationDate = dayjs(e.value).format("YYYY-MM-DD")
				datePickShow.value = false
			}
      function submit() {
        proxy.$refs['uForm'].validate().then(() => {
          if (form.value.id != null) {
            updateFutureStocks(form.value).then(res => {
              proxy.$refs['uToast'].show({
              message: '修改成功', complete() {
                uni.navigateTo({ url: `/pages/work/base/financials/list` })
              }
            })
          })
          }else {
            addFutureStocks(form.value).then(res => {
              proxy.$refs['uToast'].show({
              message: '新增成功', complete() {
                uni.navigateTo({ url: `/pages/work/base/financials/list` })
              }
            })
          })
          }
        })
      }
  </script>
  
  <style lang="scss" scoped>
  .container {
    background: #f5f7fa;
  }
  
  .section {
    margin: 24rpx;
    padding: 0;
    background-color: #fff;
    border-radius: 16rpx;
    box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.08);
    overflow: hidden;
  
    .section-title {
      padding: 16rpx 24rpx;
      background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
      color: #ffffff;
      line-height: 1.2;
      font-size: 28rpx;
      font-weight: 600;
      display: flex;
      align-items: center;
      
      &::before {
        content: '';
        width: 6rpx;
        height: 28rpx;
        background: #ffffff;
        border-radius: 3rpx;
        margin-right: 12rpx;
      }
    }
  
    .form-view {
      padding: 24rpx;
  
    .form-btn {
        padding-top: 32rpx;
      }
    }
  }
  </style>
  
  <style lang="scss">
  .form-view {
    ::v-deep .u-form-item--required::before {
      color: #f5576c !important;
      font-size: 28rpx !important;
      margin-right: 4rpx !important;
      line-height: 1 !important;
      vertical-align: middle !important;
      position: relative !important;
      top: -2rpx !important;
    }
  }
  

  

  </style>