<!--
 * @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">
        <div v-for="(item,index) in tableData" :key="index" :style="{display: 'flex',marginBottom: '10px',alignItems:tableId ? 'flex-start' : 'center'}">
          <div :style="{display: 'flex',minWidth: '150px',justifyContent: 'flex-end',padding: tableId ? '8px 0 0 0' : '0px'}">
            <dict-tag :options="dict.type.order_form_type" :value="item.source"/>：
          </div>
          <div v-if="!tableId" style="display: flex;margin-left: 20px;align-items: center;">
            <span style="margin-right: 10px;">库存</span>
            <el-input-number placeholder="请输入库存" v-model="tableData[index].stock" controls-position="right"></el-input-number>
          </div>
          <div v-if="tableId" style="margin-left: 20px;">
            <el-radio-group v-model="tableData[index].status" @change="(value)=>changeRadio(value,index)">
              <div>
                <el-radio :label="0">在售</el-radio>
                <el-input-number placeholder="请输入库存" :disabled="tableData[index].status == 1" v-model="tableData[index].stock" controls-position="right"></el-input-number>
              </div>
              <div style="margin-top: 10px;">
                <el-radio :label="1">售罄</el-radio>
              </div>
            </el-radio-group>
          </div>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button v-if="!loading" @click="cancel">取消</el-button>
        <el-button
          type="primary"
          @click="submitForm"
          :loading="loading"
        >
          <span v-if="loading">保存中...</span>
          <span v-else>保存</span>
        </el-button>
      </span>
    </el-dialog>
  </template>
  
  <script>
  import { saveCommonConfigApi,saveCommonConfigSingleApi } from "@/api/schedulingMr/schedulingMr";
  export default {
    name: "stockAll",
    dicts: ['order_form_type'],
    data() {
      return {
        title: "渠道默认库存设置",
        model: "EDIT",
        open: false,
        loading: false,
        tableData: [], // 票务
        stock: {},
        tableId: '',
        allData: {}
      };
    },
    methods: {
      /**
       * 打开弹框
       * @date 2023-11-22
       * @param {any} obj
       * @returns {any}
       */
      async openDialog(title,data,id,allData) {
        console.log("data,id,allData====",data,id)
        this.open = true;
        this.tableId = id
        this.stock = JSON.parse(JSON.stringify(data))
        this.tableData = JSON.parse(JSON.stringify(data.chennelStock))
        this.allData = JSON.parse(JSON.stringify(allData))
        this.title = title 
      },
      /**  票务信息  */
      async saveCommonConfigFun(value){
        try {
          let res = await saveCommonConfigApi()
          if(res.code == 200){
            this.tableData = res.data.stock
          }
        } catch (error) {      
        }
      },
      /**
       * 关闭弹框
       * @date 2023-11-22
       * @returns {any}
       */
      cancel() {
        this.open = false;
      },
      /**
       * 保存
       * @date 2023-11-22
       * @returns {any}
       */
      async submitForm() {
        let count = 0
        this.tableData.forEach((item,index)=>{
          count = count + (item.stock?item.stock:0) + (item.soldNum?item.soldNum:0)
        })
        console.log("count====",count)
        if( this.stock.stock < count ) {
          this.$message.error("渠道在售库存和已售数量总合不能大于总库存!");
          return
        }
        let params = JSON.parse(JSON.stringify(this.allData))
        this.allData.forEach((item,index)=>{
          if(item.seatTypeId == this.stock.seatTypeId) {
            params[index] = {
              ...this.stock,
              chennelStock: this.tableData
            }
          }
        })
        this.$emit("getList",params);
        this.cancel();
        // try {
        //   this.loading = true;
        //   let params = JSON.parse(JSON.stringify(this.allData))
        //   this.allData.forEach((item,index)=>{
        //     if(item.seatTypeId == this.stock.seatTypeId) {
        //       params[index] = {
        //         ...this.stock,
        //         chennelStock: this.tableData
        //       }
        //     }
        //   })
        //   let res = null
        //   if(this.tableId) {
        //     res = await saveCommonConfigSingleApi({ 
              
        //       stock: params
        //     });
        //   }else {
        //     res = await saveCommonConfigApi({ stock: params });
        //   }
        //   if (res.code === 200) {
        //     this.$message.success("操作成功!");
        //     this.loading = false;
        //     this.$emit("getList");
        //     this.cancel();
        //   }
        // } catch (error) {
        //   this.loading = false;
        // }
      },
      changeRadio(value,index) {
        if(value == 1){
          this.$set(this.tableData[index],'stock',0)
        }
      }
    },
  };
  </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>
  