<template>
  <div class="app-container">
    <!-- 添加或修改订单打印设置对话框 -->
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="制单人" prop="preparer">
          <el-input v-model="form.preparer" placeholder="请输入制单人" />
        </el-form-item>
        <el-form-item label="审核人" prop="checker">
          <el-input v-model="form.checker" placeholder="请输入审核人" />
        </el-form-item>
        <el-form-item label="打印人" prop="printed">
          <el-input v-model="form.printed" placeholder="请输入打印人" />
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入电话" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
  </div>
</template>

<script>
import {  getPrintSetting,  addPrintSetting, updatePrintSetting } from "@/api/mall/printSetting";

export default {
  name: "PrintSetting",
  data() {
    return {
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        preparer: [
          { required: true, message: "制单人不能为空", trigger: "blur" }
        ],
        checker: [
          { required: true, message: "审核人不能为空", trigger: "blur" },
          { min: 2, max: 20, message: '名称长度必须介于 2 和 20 之间', trigger: 'blur' }
        ],
        printed: [
          { required: true, message: "打印人不能为空", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "电话不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.handleUpdate();
  },
  methods: {
    /** 修改按钮操作 */
    handleUpdate() {
      getPrintSetting('1').then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改打印设置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.printSettingId != null) {
            updatePrintSetting(this.form).then(response => {
              this.msgSuccess("修改成功");
            });
          } else {
            addPrintSetting(this.form).then(response => {
              this.msgSuccess("新增成功");
            });
          }
        }
      });
    },
  }
};
</script>
