<template>
  <view class="global-wrapper bgF2">
    <view class="mch-header">
      <JHeaderTitle title="创建代理商" bgColor="#f2f2f2" />
    </view>
    <JMainCard pd="0" wrapPd="15rpx 30rpx">
      <JInput
        name="代理商全称"
        :isBorder="true"
        v-model:value="agentInfo.agentName"
        :rules="{ name: 'agentName', rule: 'REG_NotNUll' }"
        place="请输入商代理商全称"
      ></JInput>
      <JInput
        name="代理商简称"
        v-model:value="agentInfo.agentShortName"
        :rules="{ name: 'agentShortName', rule: 'REG_NotNUll' }"
        place="请输入代理商简称"
      ></JInput>
      <JInput
        name="代理商登录名"
        v-model:value="agentInfo.loginUsername"
        :rules="{ name: 'loginUsername', rule: 'REG_LoginName' }"
        place="字母开头6到18位"
      ></JInput>
      <JInput name="是否允许发展下级">
        <switch
          :checked="agentInfo.addAgentFlag == 1 ? true : false"
          style="margin-left: 20rpx; transform: scale(1.2)"
          color="#7737fe"
          @change="switchState($event, 'addAgentFlag')"
        />
      </JInput>
      <JInput name="代理商状态">
        <switch
          :checked="agentInfo.state == 1 ? true : false"
          style="margin-left: 20rpx; transform: scale(1.2)"
          color="#7737fe"
          @change="switchState($event, 'state')"
        />
      </JInput>
    </JMainCard>
    <JMainCard pd="0" wrapPd="15rpx 30rpx">
      <JInput
        name="联系人姓名"
        :isBorder="true"
        v-model:value="agentInfo.contactName"
        :rules="{ name: 'contactName', rule: 'REG_NotNUll' }"
        place="请输入联系人姓名"
      ></JInput>
      <JInput
        name="联系人手机号"
        v-model:value="agentInfo.contactTel"
        :rules="{ name: 'contactTel', rule: 'REG_Phone' }"
        place="请输入联系人手机号"
      ></JInput>
      <JInput name="联系人邮箱" v-model:value="agentInfo.contactEmail" place="请输入联系人邮箱"></JInput>
    </JMainCard>
    <JMainCard pd="0" wrapPd="15rpx 30rpx">
      <JInput name="是否发送开通提醒" :isBorder="true">
        <switch
          :checked="agentInfo.isNotify == 1 ? true : false"
          style="margin-left: 15rpx; transform: scale(1.2)"
          color="#7737FE"
          @change="switchState($event, 'isNotify')"
        />
      </JInput>
      <JInput name="是否使用默认密码">
        <switch
          :checked="agentInfo.defaultPass == 1 ? true : false"
          style="margin-left: 15rpx; transform: scale(1.2)"
          color="#7737FE"
          @change="switchState($event, 'defaultPass')"
        />
      </JInput>
      <JInput
        name="自定义密码"
        v-model:value="agentInfo.loginPassword"
        :rules="{ name: 'loginPassword', rule: 'REG_NotNUll' }"
        place="请输入自定义密码"
        v-if="!agentInfo.defaultPass == 1"
      ></JInput>
    </JMainCard>
    <JMainCard pd="0" wrapPd="15rpx 30rpx">
      <JInput name="备注" v-model:value="agentInfo.remark" place="请输入备注"></JInput>
    </JMainCard>
    <JButton size="max" pd="30rpx" pdTop="0" @HandleTouch="createAgent">创建代理商</JButton>
  </view>
</template>
<script setup>
import { ref, reactive } from "vue"
import { onLoad } from "@dcloudio/uni-app"
import { $addAgent, $getPasswordRules } from "@/http/apiManager.js"
import JHeaderTitle from "@/components/newComponents/JHeaderTitle/JHeaderTitle"
import JMainCard from "@/components/newComponents/JMainCard/JMainCard"
import JInput from "@/components/newComponents/JInput/JInput"
import JButton from "@/components/newComponents/JButton/JButton"
import { validateArray } from "@/hooks/rules"
import valid from "@/hooks/validate"
onLoad(() => {
  getRules()
})
const agentInfo = reactive({
  defaultPass: 1,
  isNotify: 0,
  state: 1,
  addAgentFlag: 1,
})
const switchState = (e, v) => {
  agentInfo[v] = e.detail.value ? 1 : 0
}
const rules = ref({})
const getRules = () => {
  $getPasswordRules().then((res) => {
    rules.value.rule = new RegExp(res.bizData.regexpRules)
    rules.value.ruleText = res.bizData.errTips
  })
}
const createAgent = () => {
  if (validateArray(agentInfo)) {
    if (!!agentInfo.contactEmail) {
      if (!valid.REG_Email(agentInfo.contactEmail)) return uni.showToast({ title: "邮箱格式不正确", icon: "none" })
    }
    if (agentInfo.loginPassword && !rules.value.rule.test(agentInfo.loginPassword)) {
      return uni.showToast({
        title: rules.value.ruleText,
        icon: "none",
      })
    }
    $addAgent(agentInfo).then((res) => {
      uni.showToast({
        icon: "success",
        title: "创建成功",
      })
      uni.navigateBack()
    })
  }
}
</script>
