<!--
 * @Description: 新增/编辑弹框
 * @Author: Sugar.
 * @Date: 2023-11-24 13:55:00
 * @LastEditors: Sugar.
 * @LastEditTime: 2023-11-24 13:55:00
 * @FilePath: \cattle_webui\src\views\otaMr\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" style="padding: 0">
      <el-table ref="tables" v-loading="tabLoading" :data="dataList" border @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="50" align="center" />
        <el-table-column label="序号" align="center" type="index" width="50"></el-table-column>
        <el-table-column label="票务名称" align="center" prop="goodsName" />
        <el-table-column label="座位类型" align="center" prop="goodsName" />
        <el-table-column label="市场价" align="center" prop="salePrice" width="160">
          <template slot-scope="scope">
            <span>￥{{ scope.row.salePrice }}</span>
          </template>
        </el-table-column>
        <el-table-column label="销售价" align="center" prop="salePrice" width="160">
          <template slot-scope="scope">
            <span>￥{{ scope.row.salePrice }}</span>
          </template>
        </el-table-column>
        <el-table-column label="结算价" align="center" prop="salePrice" width="160">
          <template slot-scope="scope">
            <el-input
              type="number"
              v-model="scope.row.salePrice"
              placeholder=""
              clearable
              @change="changePriceAmount('salePrice')"
              style="width: 260px;"
            >
            </el-input>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </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 { goodsList, saveAndEdit } from '@/api/otaMr/otaMr'
import Editor from "@/components/Editor";
import { getToken } from "@/utils/auth";
export default {
  name: "addAndEdit",
  props: {
    dict: {
      type: Object,
      default: () => [],
    },
  },
  components: {
    Editor,
  },
  data() {
    return {
      title: "编辑",
      model: "EDIT",
      open: false,
      loading: false,
      tabLoading: false,
      dataList: [],
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ota: '1'
      },
      selectList: []
    };
  },
  methods: {
    /**
     * 打开弹框
     * @date 2023-11-22
     * @param {any} obj
     * @returns {any}
     */
    openDialog(title, obj) {
      this.open = true;
      this.title = "票务信息";
      this.getList(obj);
    },
    /** 价格输入事件 */
    changePriceAmount(row) {
      if(row.salePrice * 1 < 0){
        this.$message.error("输入需大于或等于0!");
        row.salePrice = '';
        return false
      }
    },
    /** 查询列表 */
    getList(obj) {
      this.tabLoading = true;
      this.queryParams.ota = obj.ota;
      goodsList(this.queryParams)
        .then(response => {
            // this.dataList = response.data.rows;
            // this.total = response.data.total;
            // this.tabLoading = false;
          this.dataList = response.data;
          this.tabLoading = false;
          }
        ).catch(() => {
        this.tabLoading = false;
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.selectList = selection;
    },
    /**
     * 保存
     * @date 2023-11-22
     * @returns {any}
     */
    async submitForm() {
      try {
        if(this.selectList.length <= 0) {
          this.$message.error("请勾选商品!");
          return false
        }
        let postList = [];
        this.selectList.forEach(item => {
          postList.push({
            "ota": this.queryParams.ota,
            "goodsId": item.id,
            "otaPrice": item.salePrice
          })
        })
        this.loading = true;
        const { code } = await saveAndEdit(postList);
        if (code === 200) {
          this.$message.success("操作成功!");
          this.$emit("getList");
          this.cancel();
        }
      } catch (error) {
      } finally {
        this.loading = false;
      }
    },
    /**
     * 重置
     * @date 2023-11-22
     * @returns {any}
     */
    reset() {

    },
    /**
     * 关闭弹框
     * @date 2023-11-22
     * @returns {any}
     */
    cancel() {
      this.open = false;
    }
  },
};
</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>
