<!--
 * @Author: zwq
 * @Date: 2021-11-18 14:16:25
 * @LastEditors: DY
 * @LastEditTime: 2023-11-22 10:27:43
 * @Description:
-->
<template>
	<el-form
		:model="dataForm"
		:rules="dataRule"
		ref="dataForm"
		@keyup.enter.native="dataFormSubmit()"
		label-width="100px">
		<el-row :gutter="20">
			<el-col :span="12">
				<el-form-item label="供应商编号" prop="code">
					<el-input v-model="dataForm.code" clearable placeholder="请输入供应商编号" />
				</el-form-item>
			</el-col>
			<el-col :span="12">
				<el-form-item label="供应商名称" prop="name">
					<el-input v-model="dataForm.name" clearable placeholder="请输入供应商名称" />
				</el-form-item>
			</el-col>
		</el-row>
		<el-row :gutter="20">
			<el-col :span="12">
				<el-form-item label="联系人" prop="contact">
					<el-input v-model="dataForm.contact" clearable placeholder="请输入联系人" />
				</el-form-item>
			</el-col>
			<el-col :span="12">
				<el-form-item label="联系电话" prop="telephone">
					<el-input
						v-model="dataForm.telephone"
						maxlength="11"
						placeholder="请输入联系电话" />
				</el-form-item>
			</el-col>
		</el-row>
		<el-form-item label="地址" prop="address">
			<el-input
				v-model="dataForm.address"
				placeholder="请输入地址" />
		</el-form-item>
		<el-form-item label="备注" prop="remark">
			<el-input
				v-model="dataForm.remark"
				placeholder="请输入备注" />
		</el-form-item>
	</el-form>
</template>

<script>
import basicAdd from '../../core/mixins/basic-add';
import { createCoreSupplier, updateCoreSupplier, getCoreSupplier, getCode } from "@/api/base/coreSupplier";

export default {
	mixins: [basicAdd],
	data() {
		return {
			urlOptions: {
			  isGetCode: true,
        codeURL: getCode,
				createURL: createCoreSupplier,
				updateURL: updateCoreSupplier,
				infoURL: getCoreSupplier
			},
			dataForm: {
        id: undefined,
        code: undefined,
        name: undefined,
        telephone: undefined,
				contact: undefined,
				address: undefined,
        remark: undefined
			},
			dataRule: {
        code: [{ required: true, message: "供应商编号不能为空", trigger: "blur" }],
        name: [{ required: true, message: "供应商名称不能为空", trigger: "blur" }],
				contact: [{ required: true, message: "联系人不能为空", trigger: "blur" }],
				telephone: [
					{ required: false, trigger: "blur", message: "手机号不能为空" },
          {
            validator: function (rule, value, callback) {
							if (value) {
								if (/^(?:(?:\+|00)86)?1(?:3[\d]|4[5-79]|5[0-35-9]|6[5-7]|7[0-8]|8[\d]|9[189])\d{8}$/.test(value) === false) {
									callback(new Error("手机号格式错误"));
								} else {
									callback();
								}
							} else {
								callback();
							}
            }, trigger: "blur"
          }
        ]
      }
		};
	},
	mounted() {},
	methods: {
		// 表单提交
    dataFormSubmit() {
			console.log('你好', this.dataForm)
      this.$refs["dataForm"].validate((valid) => {
        if (!valid) {
          return false;
        }
        // 修改的提交
        if (this.dataForm.id) {
          this.urlOptions.updateURL(this.dataForm).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.visible = false; 
            this.$emit("refreshDataList");
          });
          return;
        }
        // 添加的提交
        this.urlOptions.createURL(this.dataForm).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.visible = false;
          this.$emit("refreshDataList");
        });
      });
    },
	}
};
</script>
