<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="70%" v-dialogDrag append-to-body>
      <el-form ref="formRef" :model="formData" :rules="formRules" v-loading="formLoading" label-width="100px">
        <el-collapse v-model="activeNames">
          <el-collapse-item title="车辆基本信息" name="1">
            <el-row :gutter="20">
              <el-col :span="8">
                <el-form-item label="车牌号" prop="licenseNumber">
                  <el-input v-model="formData.licenseNumber" placeholder="请输入车牌号"/>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="发动机号码" prop="engineNumber">
                  <el-input v-model="formData.engineNumber" placeholder="请输入发动机号码"/>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="车架号" prop="vin">
                  <el-input v-model="formData.vin" placeholder="请输入车架号"/>
                </el-form-item>
              </el-col>

            </el-row>


            <el-row :gutter="20">
              <el-col :span="8">
                <el-form-item label="品牌型号" prop="brandAndModel">
                  <CarBrandSelector v-model="formData.brandAndModel" ref="brandForm"/>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="车辆类别" prop="carCategory">
                  <el-select v-model="formData.carCategory" placeholder="请选择车辆类别">
                    <el-option v-for="dict in this.getDictDatas(DICT_TYPE.DICT_CAR_CATEGORY)" :key="dict.value"
                               :label="dict.label" :value="dict.value"/>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="车辆性质" prop="carNature">
                  <el-select v-model="formData.carNature" placeholder="请选择车辆性质">
                    <el-option v-for="dict in this.getDictDatas(DICT_TYPE.DICT_CAR_NATURE)" :key="dict.value"
                               :label="dict.label" :value="dict.value"/>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="8">
                <el-form-item label="注册日期" prop="carRegisterDate" label-width="auto">
                  <el-date-picker clearable v-model="formData.carRegisterDate" type="date" value-format="timestamp"
                                  placeholder="选择车辆注册日期"/>
                </el-form-item>
              </el-col>
            </el-row>
          </el-collapse-item>
          <!--    车辆业务信息      -->
          <el-collapse-item title="车辆业务信息" name="2">
            <el-row :gutter="20">
              <el-col :span="8">
                <el-form-item label="最近办理业务" prop="recentlyHandledBusiness">
                  <el-select v-model="formData.recentlyHandledBusiness" placeholder="请选择业务类别" clearable>
                    <el-option v-for="dict in this.getDictDatas(DICT_TYPE.DICT_CUS_BUSI_TYPE)" :key="dict.value"
                               :label="dict.label" :value="dict.value"/>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="最近业务时间" prop="recentlyHandleBusinessTime">
                  <el-date-picker clearable v-model="formData.recentlyHandleBusinessTime" type="date"
                                  value-format="timestamp" placeholder="选择最近办理业务的时间"/>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="二级维护时间" prop="checkDate">
                  <el-date-picker clearable v-model="formData.checkDate" type="date" value-format="timestamp"
                                  placeholder="选择二级维护时间"/>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="8">
                <el-form-item label="保养日期" prop="maintenanceDate">
                  <el-date-picker clearable v-model="formData.maintenanceDate" type="date" value-format="timestamp"
                                  placeholder="选择保养日期"/>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="年检日期" prop="inspectionDate">
                  <el-date-picker clearable v-model="formData.inspectionDate" type="date" value-format="timestamp"
                                  placeholder="选择年检日期"/>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="保险日期" prop="insuranceDate">
                  <el-date-picker clearable v-model="formData.insuranceDate" type="date" value-format="timestamp"
                                  placeholder="选择保险日期"/>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="8">
                <el-form-item label="保养里程" prop="maintenanceMileage">
                  <el-input v-model="formData.maintenanceMileage" placeholder="请输入保养里程"/>
                </el-form-item>
              </el-col>
              <el-col :span="16">
                <el-button :loading="buttonLoading" icon="el-icon-date" @click="compute" size="small" type="primary">计算</el-button>
              </el-col>
            </el-row>
          </el-collapse-item>

          <el-collapse-item title="测算" name="3">
            <el-row :gutter="20">
              <el-col :span="8">
                <el-form-item label="保险到期时间" prop="insuranceExpiryDate">
                  <el-date-picker clearable v-model="formData.insuranceExpiryDate" type="date" value-format="timestamp"
                                  placeholder="选择下次保险时间"/>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="下次年检时间" prop="nextInspectionDate">
                  <el-date-picker clearable v-model="formData.nextInspectionDate" type="date" value-format="timestamp"
                                  placeholder="选择下次年检时间"/>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="下次保养时间" prop="nextMaintenanceDate">
                  <el-date-picker clearable v-model="formData.nextMaintenanceDate" type="date" value-format="timestamp"
                                  placeholder="选择下次保养时间"/>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="下次保养里程" prop="nextMaintenanceMileage">
                  <el-input v-model="formData.nextMaintenanceMileage" placeholder="请输入下次保养里程"/>
                </el-form-item>
              </el-col>
            </el-row>
          </el-collapse-item>
          <el-collapse-item title="附件信息" name="4">
            <el-form-item>
              <ImageUpload v-model="formData.carLicenseImg"/>
            </el-form-item>
          </el-collapse-item>
        </el-collapse>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="formLoading">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as CarMainApi from '@/api/base/carmain';
import ImageUpload from '@/components/ImageUpload';
import {getDictDatas, DICT_TYPE} from '@/utils/dict';
import CarBrandSelector from '@/layout/components/CarBrandSelector';

export default {
  name: "CarMainForm",
  components: {
    ImageUpload,
    CarBrandSelector,
  },
  data() {
    return {
      //折叠面板默认展开
      activeNames: ['1', '2', '3', '4'],
      // 弹出层标题
      dialogTitle: "",
      // 是否显示弹出层
      dialogVisible: false,
      // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
      formLoading: false,
      // 表单参数
      formData: {
        id: undefined,
        engineNumber: undefined,
        vin: undefined,
        licenseNumber: undefined,
        brandAndModel: [],
        maintenanceDate: undefined,
        maintenanceMileage: undefined,
        inspectionDate: undefined,
        insuranceDate: undefined,
        checkDate: undefined,
        carBrand: undefined,
        carNature: undefined,
        carRegisterDate: undefined,
        carLicenseImg: undefined,
        carCategory: undefined,
        insuranceExpiryDate: undefined,
        nextInspectionDate: undefined,
        nextMaintenanceDate: undefined,
        nextMaintenanceMileage: undefined,
      },
      buttonLoading:false,
      // 表单校验
      formRules: {
        engineNumber: [{required: true, message: '发动机号码不能为空', trigger: 'blur'}],
        vin: [{required: true, message: '车架号不能为空', trigger: 'blur'}],
        licenseNumber: [{required: true, message: '车牌号不能为空', trigger: 'blur'}],
        carCategory: [{required: true, message: '车辆类别不能为空', trigger: 'blur'}],
        carNature: [{required: true, message: '车辆性质不能为空', trigger: 'blur'}],
        carRegisterDate: [{required: true, message: '车辆注册日期不能为空', trigger: 'blur'}],
        brandAndModel: [{required: true, message: '品牌型号不能为空', trigger: 'blur'}],
      },
    };
  },
  methods: {
    /**计算车辆信息*/
    async compute(){
      this.buttonLoading = true
      try{
        const data = this.formData;
        console.log(data,'adadadad')
        debugger
        const res = await CarMainApi.compute(data);
        const result = res.data;
        this.formData.insuranceExpiryDate = result.insuranceExpiryDate
        this.formData.nextInspectionDate = result.nextInspectionDate
        this.formData.nextMaintenanceDate = result.nextMaintenanceDate
        this.formData.nextMaintenanceMileage = result.nextMaintenanceMileage
      } finally {
        this.buttonLoading = false
      }
    },

    /** 打开弹窗 */
    async open(id) {
      this.dialogVisible = true;
      this.reset();
      // 修改时，设置数据
      if (id) {
        this.formLoading = true;
        try {
          const res = await CarMainApi.getCarMain(id);
          this.formData = res.data;
          this.title = "修改车辆信息";
        } finally {
          this.formLoading = false;
        }
      }
      this.title = "新增车辆信息";
    },

    /** 提交按钮 */
    async submitForm() {
      // 校验主表
      // await this.$refs["formRef"].validate();
      this.formLoading = true;
      try {
        const data = this.formData;
        // 修改的提交
        if (data.id) {
          await CarMainApi.updateCarMain(data);
          this.$modal.msgSuccess("修改成功");
          this.dialogVisible = false;
          this.$emit('success');
          return;
        }
        // 添加的提交
        await CarMainApi.createCarMain(data);
        this.$modal.msgSuccess("新增成功");
        this.dialogVisible = false;
        this.$emit('success');
      } finally {
        this.formLoading = false;
      }
    },
    /** 表单重置 */
    reset() {
      this.formData = {
        id: undefined,
        engineNumber: undefined,
        vin: undefined,
        licenseNumber: undefined,
        brandAndModel: [],
        maintenanceDate: undefined,
        maintenanceMileage: undefined,
        inspectionDate: undefined,
        insuranceDate: undefined,
        checkDate: undefined,
        carBrand: undefined,
        carNature: undefined,
        carRegisterDate: undefined,
        carLicenseImg: undefined,
      };
      this.resetForm("formRef");
    }
  }
};
</script>
