<template>
  <el-dialog title="调整成本" :visible.sync="dialogVisible" width="500px" @close="cancelSubmit()">
    <div v-loading="loading">
      <el-form class="mt20">
        <el-form-item label="成本费用" required>
          <template>
            <!-- <dm-input-amount v-model="costPrice" @change="changeSingleCost(giftId, costPrice)" precision="2" :min="0" :max="999999.99"  style="width:200px"></dm-input-amount> -->
            <dm-input-amount v-model="costPrice" precision="2" :min="0" :max="999999.99"  style="width:200px"></dm-input-amount>
          </template>
        </el-form-item>
        <el-form-item label="调整备注" required>
          <template>
            <el-input style="width:260px" type="textarea" :rows="3" placeholder="请填入调整备注" v-model="textarea" :maxlength="120" show-word-limit></el-input>
          </template>
        </el-form-item>
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancelSubmit()">取消</el-button>
      <el-button type="primary" @click="submitProNanme()">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
// import request from '../service/request.js';
import api from '@/api/gift.js';
// const { realCostUpdate, giftGet } = api;
const { virtualCostUpdate } = api;
// import { deepClone } from '../../../utils/index';
export default {
  props: {
    updateCostData: {
      type: Object,
      default() {
        return {};
      }
    }
  },
  data() {
    return {
      dialogVisible: false,
      loading: false,
      textarea: '',
      giftId: '',
      costPrice: '',
      giftSkuId: ''
    };
  },
  mounted() {
    this.dialogVisible = true;
    this.giftId = this.updateCostData.id;
    this.costPrice = this.updateCostData.costPrice;
  },
  methods: {
    submitProNanme() {
      if(this.loading) return;
      if(!this.textarea){
        this.$message.error('请完善备注');
        return;
      }
      this.loading = true;
      let params = {
        giftId: this.updateCostData.id,
        costPrice: this.costPrice,
        remark: this.textarea
      };
      console.log(params);
      virtualCostUpdate(params).then(res => {
        if(res.code === '0000'){
          this.dialogVisible = false;
          setInterval(() => {
            this.$emit('closeUpdateCost', false);
          }, 10);
          this.$message.success('修改成功');
        }else{
          this.$message.error(res.message);
        }
      }).finally(() => this.loading = false);
    },
    // 取消修改
    cancelSubmit() {
      this.dialogVisible = false;
      setInterval(() => {
        this.$emit('closeUpdateCost', false);
      }, 10);
    },
    // changeSingleCost(id, cost){
    //   console.log(id);
    //   if(!cost){
    //     this.$message.error('请完善成本');
    //   }
    //   if(this.updateList.length > 0){
    //     let iii;
    //     let result = this.updateList.some((item, index) =>{
    //       if(item.giftSkuId == id){
    //         iii = index;
    //         return true;
    //       }
    //     });
    //     if(result){ // 如果存在
    //       this.updateList[iii].costPrice = cost;
    //     }else{
    //       this.updateList.push({ giftSkuId: id, costPrice: cost });
    //     }
    //   }else{
    //     this.updateList.push({ giftSkuId: id, costPrice: cost });
    //   }
    // }
  }
};
</script>
<style scoped></style>
<style>
.right-border {
  border-right: 1px solid #ebeef5;
}
.hidden-limit .dm-input__counter{
  display: none;
}
.popver-button .el-button:not(.el-button--text) {
  min-width: 50px;
}
</style>