<template>
  <layout-card>
    <el-divider content-position="left">车辆选择</el-divider>
    <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
      <el-row>
        <el-col :span="6">
          <el-form-item label="登记编号" prop="RECORD_NUMBER">
            <el-input
              v-model="form.RECORD_NUMBER"
              placeholder="请输入登记编号"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="挂车车辆" prop="PLATE_NUMBER">
            <el-input
              v-model="form.PLATE_NUMBER"
              placeholder="请输入挂车车辆"
              disabled
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-divider content-position="left">基本信息</el-divider>
      <el-row>
        <el-col :span="6">
          <el-form-item label="车牌号" prop="PLATE_NUMBER">
            <el-input v-model="form.PLATE_NUMBER" placeholder="--" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="车架号" prop="FRAMES_NUMBER">
            <el-input v-model="form.FRAMES_NUMBER" placeholder="--" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="车辆类型" prop="VEHICLE_TYPE">
            <el-input v-model="form.VEHICLE_TYPE" placeholder="--" disabled />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="6">
          <el-form-item label="车主业主" prop="CAR_OWNERS">
            <el-input v-model="form.CAR_OWNERS" placeholder="--" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="车主电话" prop="VEHICLEOWNER_NUMBER">
            <el-input
              v-model="form.VEHICLEOWNER_NUMBER"
              placeholder="--"
              disabled
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="初登日期" prop="FIRSTSHOW_DATE">
            <el-input v-model="form.FIRSTSHOW_DATE" placeholder="--" disabled />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="6">
          <el-form-item label="所属省份" prop="PROVINCE">
            <el-input v-model="form.PROVINCE" placeholder="--" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="所属地市" prop="CITY">
            <el-input v-model="form.CITY" placeholder="--" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="所属区县" prop="COUNTY">
            <el-input v-model="form.COUNTY" placeholder="--" disabled />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="6">
          <el-form-item label="行驶证照片" prop="DRIVINGLICENSE_PHOTO">
            <layout-upload
              v-model:file-list="form.DRIVINGLICENSE_PHOTO"
              list-type="picture-card"
              accept=".jpg,.jpeg,.png"
              disabled
              delete-to-server
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-divider content-position="left">挂车税务信息</el-divider>
      <el-row>
        <el-col :span="6">
          <el-form-item label="车主业主" prop="VEHICLEOWNER">
            <el-input
              v-model="form.VEHICLEOWNER"
              placeholder="请填写车主业主"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="联系电话" prop="CONTACT_NUMBER">
            <el-input
              v-model="form.CONTACT_NUMBER"
              placeholder="请填写联系电话"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="登记人" prop="REGISTRANT">
            <el-input v-model="form.REGISTRANT" placeholder="请填写登记人" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="6">
          <el-form-item label="办理日期" prop="TRANSACTION_DATA">
            <el-date-picker
              v-model="form.TRANSACTION_DATA"
              value-format="YYYY-MM-DD"
              format="YYYY-MM-DD"
              type="date"
              placeholder="请选择办理日期"
              @change="fnGetExpireDate"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="税费周期" prop="TAXESDUES_PERIOD">
            <el-select v-model="form.TAXESDUES_PERIOD"
                       @change="fnGetExpireDate"
            >
              <el-option
                v-for="item in TAXES_PERIOD"
                :key="item.NAME"
                :label="item.NAME"
                :value="item.NAME"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="到期日期" prop="EXPIRE_DATE">
            <el-date-picker
              v-model="form.EXPIRE_DATE"
              value-format="YYYY-MM-DD"
              format="YYYY-MM-DD"
              type="date"
              disabled
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="6">
          <el-form-item label="提醒天数" prop="REMINDER_DAY">
            <el-input
              v-model="form.REMINDER_DAY"
              placeholder="请填写提醒天数"
              @change="fnGetReminderData"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="提醒日期" prop="REMINDER_DATA">
            <el-date-picker
              v-model="form.REMINDER_DATA"
              value-format="YYYY-MM-DD"
              format="YYYY-MM-DD"
              type="date"
              disabled
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="提醒状态">
            <el-switch
              v-model="form.REMINDER_STATUS"
              active-value="0"
              inactive-value="1"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="税费资料" prop="TAXESDUES_MATERIAL">
            <el-input
              v-model="form.TAXESDUES_MATERIAL"
              placeholder="请填写税费资料"
              type="textarea"
              :autosize="{ minRows: 3 }"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="备注" prop="REMARK">
            <el-input
              v-model="form.REMARK"
              placeholder="请填写备注"
              type="textarea"
              :autosize="{ minRows: 3 }"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="tc mt-10">
      <el-button type="primary" @click="fnSubmit">保存</el-button>
    </div>
  </layout-card>
</template>

<script setup>
import { ref } from "vue";
import { debounce } from "throttle-debounce";
import useFormValidate from "@/assets/js/useFormValidate.js";
import {
  editTaxationManageInfo,
  findByIdTaxationManageInfo,
} from "@/request/taxation_manage.js";
import { getFreightTrailerView } from "@/request/enterprise_management.js";
import { ElMessage } from "element-plus";
import { useRoute, useRouter } from "vue-router";
import LayoutUpload from "@/components/upload/index.vue";
import { addingPrefixToFile } from "@/assets/js/utils.js";
import { TAXES_PERIOD } from "@/assets/js/constant.js";

const route = useRoute();
const router = useRouter();
const { TAXATION_ID } = route.query;

const rules = {
  TRAILER_VEHICLE: [
    { required: true, message: "挂车车辆不能为空", trigger: "change" },
  ],
  VEHICLEOWNER: [
    { required: true, message: "车主/业主不能为空", trigger: "blur" },
  ],
  CONTACT_NUMBER: [
    { required: true, message: "联系电话不能为空", trigger: "blur" },
  ],
  REGISTRANT: [{ required: true, message: "登记人不能为空", trigger: "blur" }],
  TRANSACTION_DATA: [
    { required: true, message: "办理日期不能为空", trigger: "change" },
  ],
  TAXESDUES_PERIOD: [
    { required: true, message: "税费周期不能为空", trigger: "change" },
  ],
  EXPIRE_DATE: [
    { required: true, message: "到期日期不能为空", trigger: "change" },
  ],
  REMINDER_DAY: [
    { required: true, message: "提醒天数不能为空", trigger: "blur" },
    { pattern: /^([1-9][0-9]*)$/, message: "请输入正整数", trigger: "blur" },
  ],
  REMINDER_DATA: [
    { required: true, message: "提醒日期不能为空", trigger: "change" },
  ],
};
const formRef = ref(null);
const form = ref({
  TAXATION_ID: "",
  RECORD_NUMBER: "",
  PLATE_NUMBER: "",
  VEHICLEOWNER: "",
  CONTACT_NUMBER: "",
  TRANSACTION_DATA: "",
  TAXESDUES_PERIOD: "",
  EXPIRE_DATE: "",
  REGISTRANT: "",
  TRANSPORT_ENTERPRISES: "",
  REMINDER_STATUS: "",
  EXPIRE_STATUS: "",
  RECORD_STATUS: "",
  TRAILER_VEHICLE: "",
  FRAMES_NUMBER: "",
  VEHICLE_TYPE: "",
  VEHICLEOWNER_NUMBER: "",
  FIRSTSHOW_DATE: "",
  PROVINCE: "",
  CITY: "",
  COUNTY: "",
  REMINDER_DAY: "",
  REMINDER_DATA: "",
  TAXESDUES_MATERIAL: "",
  REMARK: "",
  REGISTER_NUMBER: "",
  VEHICLE_MODEL: "",
  VEHICLE_BRAND: "",
  DRIVINGLICENSE_PHOTO: [],
  TAXATION_NUMBER: "",
  DRIVING_LICENSE_IMG: [],
  FREIGHTTRAILER_ID: "",
  CAR_OWNERS: "",
});

const fnGetExpireDate = async () => {
  if(form.value.TRANSACTION_DATA !== '' && form.value.TAXESDUES_PERIOD !== ''){
    const TAXESDUES_PERIOD = Number(form.value.TAXESDUES_PERIOD.substring(0,form.value.TAXESDUES_PERIOD.indexOf("个")));
    const date = new Date(form.value.TRANSACTION_DATA);
    date.setMonth(date.getMonth() + TAXESDUES_PERIOD);
    const year = date.getFullYear();
    let month = date.getMonth() + 1;
    month = month < 10 ? "0" + month : month;
    let day = date.getDate();
    day = day < 10 ? "0" + day : day;
    form.value.EXPIRE_DATE = year + "-" + month + "-" + day;
  }
  fnGetReminderData()
};
const fnGetReminderData = async () => {
  if(form.value.REMINDER_DAY !== '') {
    const REMINDER_DAY = Number(form.value.REMINDER_DAY);
    const date = new Date(form.value.EXPIRE_DATE);
    date.setDate(date.getDate() - REMINDER_DAY);
    const year = date.getFullYear();
    let month = date.getMonth() + 1;
    month = month < 10 ? "0" + month : month;
    let day = date.getDate();
    day = day < 10 ? "0" + day : day;
    form.value.REMINDER_DATA = year + "-" + month + "-" + day;
  }

};
fnGetReminderData();

const fnGetData = async () => {
  if (!TAXATION_ID) return;
  const resData = await findByIdTaxationManageInfo({ TAXATION_ID });
  form.value = resData.pd;
  const FREIGHTTRAILER_ID = form.value.FREIGHTTRAILER_ID;
  const info = await getFreightTrailerView({ FREIGHTTRAILER_ID });
  form.value.CAR_OWNERS = info.pd.CAR_OWNERS;
  form.value.DRIVINGLICENSE_PHOTO = addingPrefixToFile(info.drivingLicenseImgs);
};
fnGetData();
const fnSubmit = debounce(
  1000,
  async () => {
    await useFormValidate(formRef);
    await editTaxationManageInfo({ ...form.value, TAXATION_ID });
    ElMessage.success("提交成功");
    router.back();
  },
  { atBegin: true }
);
</script>

<style scoped lang="scss"></style>
