<!--
 * @Description: 新增/编辑弹框
 * @Author: Sugar.
 * @Date: 2023-11-24 13:55:00
 * @LastEditors: Sugar.
 * @LastEditTime: 2023-11-24 13:55:00
 * @FilePath: \cattle_webui\src\views\venue\schedulingMr\dialog\AddOrEditDialog.vue
 * @Copyright: Copyright (c) 2016~2023 by Sugar., All Rights Reserved.
-->
<template>
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="900px"
      class="text-dia-log-class"
      append-to-body
      :close-on-click-modal="false"
      @close="cancel"
    >
      <div 
      class="dialog"
      v-loading="loading"
      >
      <span style="padding: 0 0 10px;display: flex;font-size: 16px;font-weight: 600;">渠道库存总计：{{ stockAllNum }}</span>
      <div v-if="tableId" style="display: flex;align-items: center;" @input="selectInput">
        <span>库存模式：</span>
        <el-radio-group v-model="useStock">
          <el-radio :label="0">演出厅库存</el-radio>
          <el-radio :label="1">默认渠道库存</el-radio>
        </el-radio-group>
      </div>
        <el-table
            :data="tableData"
            style="width: 100%">
            <el-table-column
                prop="seatTypeName"
                label="座位类型">
            </el-table-column>
            <el-table-column
                prop="stock"
                label="渠道库存小计(个)">
            </el-table-column>
            <el-table-column
                prop="address"
                label="操作项">
                <template slot-scope="scope">
                    <el-button type="text" v-if="!tableId" size="small" @click="addStockFun(scope.row,'修改渠道库存')">修改渠道库存</el-button>
                    <el-button type="text" v-if="tableId && useStock == 0" size="small" @click="addStockFun(scope.row,'自定义渠道')">自定义渠道</el-button>
                    <el-button type="text" v-if="tableId && useStock == 1" size="small" @click="addStockFun(scope.row,'场次库存修改')">修改渠道库存</el-button>
                  </template>
            </el-table-column>
            </el-table>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取消</el-button>
        <el-button
          type="primary"
          @click="submitForm"
          :loading="loading"
        >
          <span v-if="loading">提交中...</span>
          <span v-else>提交</span>
        </el-button>
      </span>
      <addStock ref="addStock" @getList="updateCommonConfigAllFun" />
    </el-dialog>
  </template>
  
  <script>
  import { commonConfigAllApi,commonConfigSingleApi } from "@/api/schedulingMr/schedulingMr";
  import { saveCommonConfigApi,saveCommonConfigSingleApi } from "@/api/schedulingMr/schedulingMr";
  import addStock from "./addStock.vue";
  
  export default {
    name: "stockAll",
    components: { addStock },
    data() {
      return {
        title: "",
        model: "EDIT",
        open: false,
        loading: false,
        tableData: [], // 票务
        tableId: '',
        stockAllNum: 0,
        useStock: 0,
      };
    },
    methods: {
      
      /**
       * 打开弹框
       * @date 2023-11-22
       * @param {any} obj
       * @returns {any}
       */
      async openDialog(title,row) {
        this.open = true;
        this.title = title;
        this.loading = true;
        this.tableId = row && row.id ? row.id : ''
        this.useStock = row && (row.useStock || row.useStock == 0) ? row.useStock : ''
        await this.commonConfigAllFun(this.tableId,true)
        this.loading = false;
      },
      /**  票务信息  */
      async commonConfigAllFun(id,type){
        try {
          this.loading = true;
          let res = null
          if(id) {
            res = await commonConfigSingleApi({id: id })
          }else {
            res = await commonConfigAllApi()
          }
          

          if(res.code == 200){
            let listCopy = []
            res.data.stock.forEach((item,index)=>{
              if(this.tableId) {
                listCopy.push({
                  id: item.id,
                  seatTypeName: item.seatTypeName,
                  "performTimeId": item.performTimeId?item.performTimeId:this.tableId,
                  "seatTypeId": item.seatTypeId,
                  "stock": item.stock,
                  "chennelStock": this.useStock == 0 && !type ? [] : item.chennelStock ? this.setData(item.chennelStock) : []
                })
              }else {
                listCopy.push({
                  seatTypeName: item.seatTypeName,
                  "seatTypeId": item.seatTypeId,
                  "stock": item.stock,
                  "chennelStock": item.chennelStock ? this.setData(item.chennelStock) : []
                })
              }
              
            })
            this.tableData = listCopy
            let stockAllNum = 0
            this.tableData.forEach((item,index)=>{
              if(item.stock){
                stockAllNum = stockAllNum + item.stock
              }
            })
            this.stockAllNum = stockAllNum
          }
          this.loading = false;
        } catch (error) {   
            console.error(error)
            this.loading = false;
            this.open = false
        }
      },
      /**  初始化值  */
      setData(list) {
        let listCopy = []
        list.forEach((item,index)=>{
          if(this.tableId){
            listCopy.push({
              id: item.id,
              "performTimeId": this.tableId,
              "stockId": item.stockId,
              "source": item.source,
              "stock": item.stock,
              "soldNum": item.soldNum,
              "status": item.status
            })
          }else {
            listCopy.push({
              "source": item.source,
              "stock": item.stock,
              "soldNum": item.soldNum,
              "status": item.status
            })
          }
          
        })
        return listCopy
      },
      /** 更新列表  */
      updateCommonConfigAllFun(data) {
        this.tableData = data
        let stockAllNum = 0
        this.tableData.forEach((item,index)=>{
          if(item.stock){
            stockAllNum = stockAllNum + item.stock
          }
        })
        this.stockAllNum = stockAllNum
      },
      /**
       * 关闭弹框
       * @date 2023-11-22
       * @returns {any}
       */
      cancel() {
        this.$emit('getList')
        this.open = false;
      },
      /**  场次库存设置   */
      addStockFun(row,title) {
        this.$refs["addStock"].openDialog(title,row,this.tableId,this.tableData,this.useStock);
      },
      /**
       * 保存
       * @date 2023-11-22
       * @returns {any}
       */
       async submitForm() {
        try {
          this.loading = true;
          let res = null
          if(this.tableId) {
            res = await saveCommonConfigSingleApi({ 
              "performTimeId": this.tableId,
              "useStock": this.useStock,
              stock: this.tableData
            });
          }else {
            res = await saveCommonConfigApi({ stock: this.tableData });
          }
          if (res.code === 200) {
            this.$message.success("操作成功!");
            this.loading = false;
            this.cancel();
          }
        } catch (error) {
          this.loading = false;
        }
      },
      /**   */
      selectInput() {
        this.commonConfigAllFun()
      }
    },
  };
  </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>
  