<template>
    <view class="container" style="paddingBottom:1rpx;">
      <u-navbar
			leftIconSize="40rpx"
			leftIconColor="#333333"
			title="POS机管理"
		>
		</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="POS机名称" prop="name" required >
              <u--input v-model="form.name"  placeholder="请填写POS机名称"
                inputAlign="right" border="none"></u--input>
            </u-form-item>
            <u-form-item label="类型" required prop="typeName"  @click="handleShowType">
              <u--input v-model="form.typeName" 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="merchantName" required >
              <u--input v-model="form.merchantName"  placeholder="请填写商户名称"
                inputAlign="right" border="none"></u--input>
            </u-form-item>
            <u-form-item label="结算卡" prop="bankNameCode" required @click="handleDebitCard">
              <u--input v-model="form.bankNameCode" 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="刷卡费率" required prop="rate" >
              <u--input v-model="form.rate"  placeholder="请填写刷卡费率"
                inputAlign="right" border="none"></u--input>
            </u-form-item>
            <u-form-item label="费率+" required prop="ratePlus" >
              <u--input v-model="form.ratePlus"  placeholder="请填写费率+"
                inputAlign="right" border="none"></u--input>
            </u-form-item>
            <u-form-item label="厂商"  prop="manufacture" >
              <u--input v-model="form.manufacture"  placeholder="请填写厂商"
                inputAlign="right" border="none"></u--input>
            </u-form-item>
            <u-form-item label="支付公司" prop="payCompany" >
              <u--input v-model="form.payCompany"  placeholder="支付公司"
                inputAlign="right" border="none"></u--input>
            </u-form-item>
          
            <u-form-item label="编号" prop="code" >
              <u--input v-model="form.code"  placeholder="请填写编号"
                inputAlign="right" border="none"></u--input>
            </u-form-item>
            <u-form-item label="启用日期" prop="activationDate"  @click="selectDate()">
              <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="merchantTypeName"  @click="handleShowMerchantType">
              <u--input v-model="form.merchantTypeName" 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="merchantCode" >
              <u--input v-model="form.merchantCode"  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="showDebitCard" :columns="debitCardList" keyName="nameCodeAvailableLimit" @cancel="handleDebitCardCancel"
      @confirm="handleDebitCardConfirm"></u-picker>
      <u-picker itemHeight="88" :show="showType" :columns="typeList" keyName="dictLabel" @cancel="handleCancel"
        @confirm="handleConfirm"></u-picker>
        <u-picker itemHeight="88" :show="showMerchantType" :columns="merchantTypeList" keyName="dictLabel" @cancel="handleMerchantTypeCancel"
        @confirm="handleMerchantTypeConfirm"></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 { getPosmachine, addPosmachine, updatePosmachine } from '@/api/invest/posmachine'
  import { getDicts } from '@/api/system/dict/data.js'
  import { listAccounts } from '@/api/invest/accounts'
  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 showType = ref(false)
const showMerchantType = ref(false)
const showDebitCard = ref(false)
const title = ref("POS机管理")
const typeList = ref([])
const merchantTypeList = ref([])
const debitCardList = ref([])
const dateType = ref(1)
const data = reactive({
  form: {
    id: null,
    name: null,
    type: null,
    manufacture: null,
    payCompany: null,
    merchantName: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    delFlag: null,
    remark: null,
    code: null,
    rate: null,
    debitCard: null,
    merchantType: null,
    merchantCode: null,
    ratePlus: 0,
    activationDate: null
  },
  queryDebitCardParams: {
    pageNum: 1,
    type: '1',
    state: '1',
    pageSize: 1000
  },
  // dateType:'1',
  rules: {
          name: [{ type: 'string', required: true, message: 'POS机名称不能为空', trigger: ['change', 'blur'] }],
          typeName: [{ type: 'string', required: true, message: '类型不能为空', trigger: ['change', 'blur'] }],
          merchantName: [{ type: 'string', required: true, message: '商户名称不能为空', trigger: ['change', 'blur'] }],
          rate: [{ type: 'number', required: true, message: '刷卡费率不能为空', trigger: ['change', 'blur'] }],  
          ratePlus: [{ type: 'number', required: true, message: '费率+不能为空', trigger: ['change', 'blur'] }], 
          debitCard: [{ 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="POS机管理-修改"
       }else{
        title.value="POS机管理-新增"
       }
      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() {
        // 类型
        listAccounts(queryDebitCardParams.value).then((response) => {
       debitCardList.value = [response.rows] 
       })
        getDicts('pos_type').then(res => {
        typeList.value =[res.data] 
      })
       // 类型
       getDicts('merchant_type').then(res => {
        merchantTypeList.value =[res.data] 
      })
      if(form.value.id!=null){
        getPosmachine(form.value.id).then(res => {
          form.value = res.data
           // 类型
        getDicts('pos_type').then(result => {
          form.value.typeName=dictStr(form.value.type, result.data)
      })
          // 类型
          getDicts('merchant_type').then(result => {
          form.value.merchantTypeName=dictStr(form.value.merchantType, 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   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 handleShowType() {
        if (typeList.value[0].length === 0) {
          proxy.$refs['uToast'].show({
            message: '类型为空  ', type: 'warning'
          })
        } else {
          showType.value = true
        }
      }
      function   handleConfirm(e) {
        form.value.typeName = e.value[0].dictLabel
        form.value.type = e.value[0].dictValue
        showType.value = false
      }
      function handleCancel() {
       showType.value = false
      }
      function handleShowMerchantType() {
        if (merchantTypeList.value[0].length === 0) {
          proxy.$refs['uToast'].show({
            message: '商户类型为空  ', type: 'warning'
          })
        } else {
          showMerchantType.value = true
        }
      }
      function   handleMerchantTypeConfirm(e) {
        form.value.merchantTypeName = e.value[0].dictLabel
        form.value.merchantType = e.value[0].dictValue
        showMerchantType.value = false
      }
      function handleMerchantTypeCancel() {
       showMerchantType.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) {
            updatePosmachine(form.value).then(res => {
              proxy.$refs['uToast'].show({
              message: '修改成功', complete() {
                uni.navigateTo({ url: `/pages/work/base/posmachine/list` })
              }
            })
          })
          }else {
            addPosmachine(form.value).then(res => {
              proxy.$refs['uToast'].show({
              message: '新增成功', complete() {
                uni.navigateTo({ url: `/pages/work/base/posmachine/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>