<template>
  <div class="AddBuildingBox">
    <PageHeader :title="userid ? '编辑月卡' : '添加月卡'"></PageHeader>
    <div class="AddBuildingBody">
      <div class="title">基本信息</div>
      <div class="addBuildingFormBody">
        <el-form class="formBody" label-position="right" ref="formRef" :model="form" :rules="rules" label-width="130px">
          <el-row>
            <el-col :span="11">
              <el-form-item label="月卡编号" prop="cardCode">
                <el-input v-model.trim="form.cardCode" placeholder="请输入月卡编号" />
              </el-form-item>
            </el-col>
            <el-col :span="2"></el-col>
            <el-col :span="11">
              <el-form-item label="持卡人姓名" prop="cardName">
                <el-input v-model.trim="form.cardName" placeholder="请输入持卡人姓名" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="11">
              <el-form-item label="手机号码" prop="phone">
                <el-input v-model.number="form.phone" placeholder="请输入手机号码" />
              </el-form-item>
            </el-col>
            <el-col :span="2"></el-col>
            <el-col :span="11">
              <el-form-item label="绑定车牌号" prop="carNum">
                <el-input v-model.number="form.carNum" placeholder="请输入车牌号" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="11">
              <el-form-item label="办理人" prop="handleName">
                <el-input v-model.number="form.handleName" placeholder="请输入办理人姓名" />
              </el-form-item>
            </el-col>
            <el-col :span="2"></el-col>
            <el-col :span="11">
              <el-form-item label="有效日期" prop="phone">
                <el-date-picker
                  range-separator="到"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  v-model="checkDate"
                  type="daterange"
                  placeholder="选择日期"
                  @change="handlDate"
                  value-format="YYYY-MM-DD"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="11">
              <el-form-item label="费用" prop="cost">
                <el-input v-model.number="form.cost" placeholder="请输入费用">
                  <template #suffix>元</template>
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="2"></el-col>
            <el-col :span="11">
              <el-form-item label="支付方式" prop="payMethod">
                <el-select v-model="form.payMethod" placeholder="请选择支付方式" clearable>
                  <el-option v-for="dict in com_pay_method" :key="dict.value" :label="dict.label" :value="dict.value" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="备注" prop="remark">
                <el-input type="textarea" :rows="5" v-model.trim="form.remark" placeholder="请输入" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item style="margin-top: 30px">
            <el-button type="primary" @click="submitForm">提交</el-button>
            <el-button @click="cancelForm">返回</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import PageHeader from '@/components/Pageheader/index.vue';
import { addMonthCard, getMonthCard, updateMonthCard } from '@/api/system/MonthCard';
import { validator } from '@/utils/Reg';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { com_pay_method } = toRefs<any>(proxy?.useDict('com_pay_method'));

const router = useRouter();
const route = useRoute();

const formRef = ref();
const form = ref({
  id: null,
  cardCode: '', // 月卡编号
  cardName: null, // 持卡人姓名
  phone: '', // 手机号
  carNum: null, // 绑定车牌号
  handleName: '', //  办理人姓名
  cost: null, // 费用/元
  payMethod: '', // 支付方式
  remark: '', // 备注
  startTime: '', // 开始日期
  endTime: '' // 结束日期
});
const checkDate = ref([]);
function handlDate() {
  form.value.startTime = checkDate.value[0];
  form.value.endTime = checkDate.value[1];
}
const rules = ref({
  cardCode: [{ required: true, message: '请输入月卡编号', trigger: 'blur' }],
  cardName: [
    { required: true, message: '请输入姓名', trigger: 'blur' },
    {
      validator: (_, value) => validator(value, 'ChineseName'),
      message: '请输入正确的中文姓名',
      trigger: 'blur'
    }
  ],
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    {
      validator: (_, value) => validator(value, 'phone'),
      message: '请输入正确的手机号码',
      trigger: 'blur'
    }
  ],
  carNum: [
    { required: true, message: '请输入车牌号', trigger: 'blur' },
    {
      validator: (_, value) => validator(value, 'PlateNumber'),
      message: '请输入正确的车牌号',
      trigger: 'blur'
    }
  ],
  startTime: [{ required: true, message: '请选择有效日期', trigger: 'blur' }],
  payMethod: [{ required: true, message: '请选择支付方式', trigger: 'blur' }],
  cost: [
    {
      validator: (_, value) => validator(value, 'money'),
      message: '请输入正确的金额，最多两位小数',
      trigger: 'blur'
    }
  ]
});
const userid = ref(null);
if (route.query.id) {
  userid.value = route.query.id;
  getMonthCard(userid.value).then((res) => {
    form.value = {
      ...form.value,
      ...res.data
    };
    checkDate.value = [form.value.startTime, form.value.endTime];
  });
}
// !== 提交 =============================================================================
// 提交
const submitForm = () => {
  formRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      if (!form.value.startTime.trim() || !form.value.endTime.trim()) {
        ElMessage.warning('请选择有效日期');
      }
      if (userid.value) {
        updateMonthCard(form.value).then((res) => {
          console.log(res);
          ElMessage.success('编辑成功');
          cancelForm();
        });
      } else {
        addMonthCard(form.value).then((res) => {
          console.log(res);
          ElMessage.success('添加成功');

          cancelForm();
        });
      }
    }
  });
};
// 推出
const cancelForm = () => {
  router.push({
    path: '/carArea/monthCard'
  });
};
</script>

<style scoped lang="scss">
.AddBuildingBox {
  padding: 15px;
  height: 100%;
  width: 100%;
}
.AddBuildingBody {
  margin-top: 20px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  background-color: white;
  padding: 15px;
  .title {
    height: 40px;
    line-height: 40px;
    padding-left: 20px;
    background-color: rgba(255, 255, 255, 1);
    color: rgba(16, 16, 16, 1);
    border-bottom: 1px solid #bbbbbb;
    margin-bottom: 60px;
  }

  .formBody {
    width: 1050px;
    margin: 0 auto;
    &:deep(.el-form-item__content, .el-select, .el-input) {
      width: 350px !important;
      margin-right: 10px;
      .el-cascader {
        width: 100%;
      }
    }
    .el-form-item {
      margin-bottom: 20px;
      align-items: center;
    }
    .el-button {
      width: 80px;
      height: 35px;
      margin-right: 20px;
    }
  }
}
</style>
