<template>
  <el-dialog
    :title="(formState ? '添加' : '编辑') + '票档'"
    :visible.sync="show"
    width="600px"
  >
    <el-form ref="form" :model="form" label-width="90px" :rules="rules">
      <el-form-item label="票档名称" prop="tierName">
        <el-input v-model="form.tierName"></el-input>
      </el-form-item>
      <el-form-item label="票档价格" prop="tierPrice">
        <el-input type="number" :min="0.01" v-model="form.tierPrice"  placeholder="请输入票档价格"></el-input>
      </el-form-item>
      <el-form-item label="座位数" prop="seatsInTier">
        <el-input-number
          v-model="form.seatsInTier"
          placeholder="请输入可用座位数"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="可用座位数" prop="seatsAvailableInTier">
        <el-input-number
          v-model="form.seatsAvailableInTier"
          placeholder="请输入可用座位数"
        ></el-input-number>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button v-loading="loading" type="primary" @click="saveHandler">{{
        btn_title
      }}</el-button>
      <el-button @click="close">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
/**
 * Created on 2024-11-27
 * @description 票档和场次关联表
 * @author yxz
 */
import { addObj, putObj } from "@/api/zshow/zTicketTier";
import customSelect from "@/components/CustomSelect";

export default {
  components: { customSelect },
  name: "zTicketTierForm",
  data() {
    return {
      form: {},
      show: false,
      loading: false,
      rules: {
        // example :
        // field: [{ required: true, trigger: "blur" }],
      },
    };
  },
  computed: {
    formState() {
      return !this.form.tierId;
    },
    btn_title() {
      return this.formState ? "新建" : "保存";
    },
  },
  methods: {
    open(origin) {
      if (origin) {
        // 编辑初始化
        let form = Object.assign({}, origin.row);
        this.form = form;
      } else {
        // 新建初始化
        this.form = {};
        this.form.showId = origin.showId;
        this.form.scheduleId = origin.scheduleId;
      }
      this.show = true;
    },
    close() {
      this.show = false;
    },
    saveHandler() {
      this.$refs.form.validate((valid) => {
        if (!valid) return;
        this.loading = true;
        if (this.formState) this.createHandler();
        else this.editHandler();
      });
    },
    createHandler() {
      addObj(this.form)
        .then((res) => {
          if (res.code === "success") {
            this.finish();
          }
        })
        .finally(() => {
          this.loading = false;
        });
    },
    editHandler() {
      putObj(this.form)
        .then((res) => {
          if (res.code === "success") {
            this.finish();
          }
        })
        .finally(() => {
          this.loading = false;
        });
    },
    finish() {
      this.close();
      this.$emit("reloadTable");
    },
  },
};
</script>
