<template>
  <div>
    <el-dialog title="修改"
               width="500px"
               :visible.sync="soundVisible">
      <el-form :model="soundForm"
               label-position="right"
               label-width="120px"
               :inline="true"
               ref="soundForm">
        <el-form-item label="原乐器">
          <el-input style="width: 330px"
                    disabled
                    v-model="soundForm.originalMusicalGoods"></el-input>
        </el-form-item>
        <el-form-item label="原教辅">
          <el-input style="width: 330px"
                    disabled
                    v-model="soundForm.originalAccessories"></el-input>
        </el-form-item>
        <el-form-item label="更换乐器">
          <el-input style="width: 330px"
                    disabled
                    v-model="soundForm.changeMusicalGoods"></el-input>
        </el-form-item>
        <el-form-item label="更换教辅">
          <el-input style="width: 330px"
                    disabled
                    v-model="soundForm.changeAccessories"></el-input>
        </el-form-item>
        <el-form-item label="支付差价">
          <el-input style="width: 330px"
                    disabled
                    v-model="soundForm.money"></el-input>
        </el-form-item>
        <el-form-item label="成本差(元)">
          <el-input style="width: 330px"
                    v-model="soundForm.costMargin"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer"
           class="dialog-footer">
        <el-button @click="soundVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="resetCostMargin">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<style lang="scss">
</style>
<script>
import numeral from "numeral";
import { updateCostMargin } from "@/api/businessManager";
export default {
  props: ["show", "item"],
  data () {
    return {
      soundForm: {
        originalMusicalGoods: null,
        originalAccessories: null,
        changeMusicalGoods: null,
        changeAccessories: null,
        money: null,
        costMargin: null,
        id: null,
      },
      soundVisible: false,
    };
  },
  mounted () { },
  methods: {
    accessoriesFormatter (accessories) {
      if (accessories?.length > 0) {
        let arr = accessories.map((res) => {
          return res.name;
        });
        return arr.join(",");
      } else {
        return ''
      }

    },
    resetCostMargin () {
      updateCostMargin({ id: this.soundForm.id, costMargin: this.soundForm.costMargin }).then(res => {
        if (res.code == 200) {
          this.$message.success('修改成功')
          this.$emit('getList')
        }
      })
    },
  },
  watch: {
    show (val) {
      this.soundVisible = val;
    },
    soundVisible (val) {
      if (!val) {
        this.$emit("closeReset");
      }
    },
    item: {
      immediate: true,
      deep: true,
      handler (row, oldValue) {
        if (row) {
          this.soundForm = {
            originalMusicalGoods: row.originalMusicalGoods
              ? row.originalMusicalGoods.name
              : null,
            originalAccessories:
              this.accessoriesFormatter(row.originalAccessoriesGoods) || null,
            changeMusicalGoods: row.changeMusicalGoods
              ? row.changeMusicalGoods.name
              : null,
            changeAccessories:
              this.accessoriesFormatter(row.changeAccessoriesGoods) || null,
            money: numeral(
              row.changeMusicalPrice +
              row.changeAccessoriesPrice -
              (row.originalMusicalPrice + row.originalAccessoriesPrice)
            ).format("0,0.00"),
            costMargin: numeral(row.costMargin).format("0,0.00"),
            id: row.id,
          };
        } else {
          this.soundForm = {
            originalMusicalGoods: null,
            originalAccessories: null,
            changeMusicalGoods: null,
            changeAccessories: null,
            money: null,
            costMargin: null,
            id: null,
          };
        }
      },
    },
  },
};
</script>