<!--
 * @Description: 新增/编辑弹框
 * @Author: Sugar.
 * @Date: 2023-11-24 13:55:00
 * @LastEditors: Sugar.
 * @LastEditTime: 22023-11-24 13:55:00
 * @FilePath: \cattle_webui\src\views\team\ticketMr\dialog\AddOrEditDialog.vue
 * @Copyright: Copyright (c) 2016~2023 by Sugar., All Rights Reserved.
-->
<template>
  <el-dialog
    :title="title"
    :visible.sync="open"
    width="700px"
    append-to-body
    :close-on-click-modal="false"
    @close="cancel"
  >
    <div class="dialog">
      <el-form :model="form" ref="form" :rules="rules" label-width="120px">
        <el-form-item label="选择剧目:" prop="performId">
          <el-select
            v-model="form.performId"
            placeholder="选择剧目"
            clearable
            @change="performChangeEven"
            style="width: 100%;"
          >
            <el-option
              v-for="dict in goodsList"
              :key="dict.id"
              :label="dict.name"
              :value="dict.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="票务管理:" prop="goodsId">
          <el-select
            v-model="form.goodsId"
            placeholder="票务管理"
            clearable
            @change="goodsChangeEven"
            style="width: 100%;"
          >
            <el-option
              v-for="dict in ticketList"
              :key="dict.id"
              :label="dict.goodsName"
              :value="dict.goodsId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="座位类型:" prop="seatTypeId">
          <el-select
            v-model="form.seatTypeId"
            placeholder="座位类型"
            clearable
            @change="seatChangeEven"
            style="width: 100%;"
          >
            <el-option
              v-for="dict in seatList"
              :key="dict.seatTypeId"
              :label="dict.seatTypeName"
              :value="dict.seatTypeId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="渠道票务别称:" prop="otherName">
          <el-input
            v-model="form.otherName"
            placeholder="请输入渠道票务别称"
            clearable
            style="width: 100%;"
          />
        </el-form-item>
        <el-form-item label="销售价:" prop="originalSalePrice">
          <el-input
            disabled
            v-model="form.originalSalePrice"
            placeholder="销售价"
            clearable
            style="width: 100%;"
          />
        </el-form-item>
        <el-form-item label="团购单价:" prop="salePrice">
          <el-input
            type="number"
            v-model="form.salePrice"
            placeholder=""
            clearable
            @change="changePriceAmount('salePrice')"
            style="width: 260px;"
          >
            <template slot="append">元/人</template>
          </el-input>
        </el-form-item>
        <el-form-item label="成团人数:" prop="teamPersonNum">
          <el-input
            type="number"
            v-model="form.teamPersonNum"
            placeholder=""
            clearable
            @change="changePriceAmount('teamPersonNum')"
            style="width: 260px;"
          >
          </el-input>
          <span style="margin-left: 5px">人起成团</span>
        </el-form-item>
        <el-form-item label="备注:">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="备注"
            clearable
            style="width: 100%;"
          />
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="cancel">取消</el-button>
      <el-button
        type="primary"
        @click="submitForm"
        v-loading.fullscreen.lock="loading"
        element-loading-text="提交中..."
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(0, 0, 0, 0.8)"
      >
        <span v-if="loading">提交中...</span>
        <span v-else>保存</span>
      </el-button>
    </span>
  </el-dialog>
</template>

<script>
// import { updateNoticeMgr } from "@/api/system/noticeMgr";
import { saveAndEdit, getSelectById } from "@/api/team/ticketMr";
import Editor from "@/components/Editor";
import { getToken } from "@/utils/auth";
import { goodsPageList } from '@/api/programmeMr/programmeMr'
import { seatPricePageList } from '@/api/distribution/ticketMr'
export default {
  name: "addAndEdit",
  props: {
    dict: {
      type: Object,
      default: () => [],
    },
  },
  components: {
    Editor,
  },
  data() {
    return {
      title: "编辑",
      model: "EDIT",
      activeName: '01',
      open: false,
      loading: false,
      form: {
        id: undefined,
      },
      rules: {
        performId: [{ required: true, message: "请选择选择剧目", trigger: ["change","blur"] }],
        goodsId: [{ required: true, message: "请选择票务", trigger: ["change","blur"] }],
        seatTypeId: [{ required: true, message: "请选择座位类型", trigger: ["change","blur"] }],
        salePrice: [{ required: true, message: "请输入团购单价", trigger: ["change","blur"] }],
        originalSalePrice: [{ required: true, message: "请输入销售价", trigger: ["change","blur"] }],
        teamPersonNum: [{ required: true, message: "请输入人数", trigger: ["change","blur"] }],
        otherName: [{ required: true, message: "请输入渠道票务别称", trigger: ["change","blur"] }]
      },
      uploadObj: {
        url: process.env.VUE_APP_UPLOAD_FILE_API + "/upload/single/minio",
        Headers: { Authorization: "Bearer " + getToken() },
      },
      goodsList: [],
      ticketList: [],
      seatList: []
    };
  },
  methods: {
    /**
     * 打开弹框
     * @date 2023-11-22
     * @param {any} obj
     * @returns {any}
     */
    openDialog(title, obj) {
      this.open = true;
      this.goodsPageList();
      this.reset();
      if (obj){
        this.title = "编辑票务管理";
        this.getSelectByIdApi(obj);
      }else{
        this.title = "添加票务管理";
        this.$nextTick(() => {
          this.$refs["form"].clearValidate();
        });
      }
    },
    /** 获取详情 */
    getSelectByIdApi(row) {
      const id = row.id
      getSelectById(id).then(response => {
        const obj = response.data;
        this.$nextTick(() => {
          this.$set(this.form, 'id', obj.id);
          this.$set(this.form, 'goodsId', obj.goodsId);
          this.$set(this.form, 'seatTypeId', obj.seatTypeId);
          this.$set(this.form, 'performId', obj.performId);
          this.$set(this.form, 'originalSalePrice', obj.originalSalePrice);
          this.$set(this.form, 'salePrice', obj.salePrice);
          this.$set(this.form, 'brokeragePrice', obj.brokeragePrice);
          this.$set(this.form, 'teamPersonNum', obj.teamPersonNum);
          this.$set(this.form, 'otherName', obj.otherName);
          this.$set(this.form, 'remark', obj.remark);
          this.ticketListApi();
          this.getSeatTypeList({goodsId: this.form.goodsId, performId: this.form.performId});
        });
      });
    },
    /** 剧目列表查询 */
    goodsPageList() {
      goodsPageList(this.addDateRange({pageNum: 1, pageSize: 100, status: 1}))
        .then(response => {
          this.goodsList = response.data.rows;
        });
    },
    /** 票务列表查询 */
    ticketListApi(performId) {
      this.ticketList = []
      seatPricePageList(this.addDateRange({pageNum: 1, pageSize: 100,  performId: performId,goodsStatus: 0}))
        .then(response => {
          let ticketList = []
          response.data.rows.forEach(item => {
            if(ticketList.indexOf(item.goodsId) == -1){
              this.ticketList.push(item);
              ticketList.push(item.goodsId);
            }
          })
        });
    },
    /** 座位类型列表 */
    getSeatTypeList(obj) {
      seatPricePageList(this.addDateRange({pageNum: 1, pageSize: 100, goodsId: obj.goodsId, performId: obj.performId}))
        .then(response => {
          let ticketList = []
          response.data.rows.forEach(item => {
            if(ticketList.indexOf(item.seatTypeId) == -1){
              this.seatList.push(item);
              ticketList.push(item.seatTypeId);
            }
          })
        });
    },
    /** 价格输入事件 */
    changePriceAmount(key) {
      if(this.form[key] * 1 < 0){
        this.$message.error("输入需大于或等于0!");
        this.$set(this.form, key, '');
        return false
      }
    },
    // 剧目改变事件
    performChangeEven(val) {
      this.seatList = [];
      this.ticketList = [];
      this.$set(this.form, 'originalSalePrice', '')
      this.$set(this.form, 'goodsId', '');
      this.$set(this.form, 'seatTypeId', '');
      this.ticketListApi(val);
    },
    // 票务改变事件
    goodsChangeEven() {
      this.seatList = [];
      this.$set(this.form, 'originalSalePrice', '');
      this.$set(this.form, 'seatTypeId', '');
      let selectMap = {goodsId: this.form.goodsId, performId: this.form.performId}
      this.getSeatTypeList(selectMap);
    },
    // 座位类型改变事件
    seatChangeEven(val) {
      this.seatList.forEach(item => {
        if(item.seatTypeId == val){
          this.$set(this.form, 'originalSalePrice', item.priceAmount);
        }
      })
    },
    /**
     * 保存
     * @date 2023-11-22
     * @returns {any}
     */
    submitForm() {
      this.$refs["form"].validate(async (valid) => {
        if (valid) {
          try {
            this.loading = true;
            const { code } = await saveAndEdit({ ...this.form });
            if (code === 200) {
              this.$message.success("操作成功!");
              this.$emit("getList");
              this.cancel();
            }
          } catch (error) {
          } finally {
            this.loading = false;
          }
        }
      });
    },
    /**
     * 重置
     * @date 2023-11-22
     * @returns {any}
     */
    reset() {
      this.$set(this.form, 'id', '');
      this.$set(this.form, 'goodsId', '');
      this.$set(this.form, 'seatTypeId', '');
      this.$set(this.form, 'performId', '');
      this.$set(this.form, 'originalSalePrice', '');
      this.$set(this.form, 'salePrice', '');
      this.$set(this.form, 'brokeragePrice', '');
      this.$set(this.form, 'teamPersonNum', '');
      this.$set(this.form, 'otherName', '');
      this.$set(this.form, 'remark', '');
    },
    /**
     * 关闭弹框
     * @date 2023-11-22
     * @returns {any}
     */
    cancel() {
      this.reset();
      this.open = false;
    },
    /**
     * 上传成功
     * @date 2023-11-22
     * @param {any} res
     * @returns {any}
     */
    handleAvatarSuccess(res) {
      if (res.code === 200) {
        // this.form.mainImg = res?.data?.url;
        this.$set(this.form, 'mainImg', res?.data?.url)
      }
    },
    /**
     * 上传文件之前之前
     * @date 2023-11-22
     * @param {any} file
     * @returns {any}
     */
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg" || "image/png";
      if (!isJPG) {
        this.$message.error("上传头像图片只能是jpg或png格式!");
      }
      return isJPG;
    },
    /**
     * 剧目海报上传成功
     * @date 2023-11-22
     * @param {any} res
     * @returns {any}
     */
    handlePhotoListSuccess(res) {
      if (res.code === 200) {
        let photo = {
          imageUrl: res?.data?.url,
          url: res?.data?.url,
          photoType: '2'
        }
        if(!this.form.photoList){
          this.form.photoList = []
        }
        // this.form.photoList.push(photo);
        this.$set(this.form.photoList, this.form.photoList.length, photo);
      }
    },
    handleRemove(file, fileList) {
      this.form.photoList.forEach((item, index) => {
        if(item.uid == file.uid){
          this.form.photoList.splice(index, 1)
        }
      })
    },
  },
};
</script>

<style lang="scss" scoped>
.dialog {
  padding: 0 30px;
  max-height: 65vh;
  overflow-y: auto;
}
.dialog {
  padding: 0 30px;
  .upload-btn {
    width: 100px;
    height: 100px;
    background-color: #fbfdff;
    border: dashed 1px #c0ccda;
    border-radius: 5px;
    i {
      font-size: 30px;
      margin-top: 20px;
    }
    &-text {
      margin-top: -10px;
    }
  }
  .avatar {
    cursor: pointer;
  }
}
</style>
