<template>
    <el-dialog title="修改商品名称" :visible.sync="visible" width="86%" append-to-body>
        <u--input v-model="inputForm.tradeName" placeholder="请输入商品名称"></u--input>
        <span slot="footer">
            <el-button @click="visible = false">取消</el-button>
            <el-button type="primary" @click="submit">确定</el-button>
        </span>
    </el-dialog>
</template>

<script>
import WareHouseService from '@/api/psi/WareHouseService'

export default {
    name: 'EditTradeNameDialog',
    data() {
        return {
            visible: false,
            inputForm: {
                oldTradeName: '',
                tradeName: '',
                wareHouseType: '',
                wareHouseTypeName: ''
            }
        }
    },
    wareHouseService: null,
    created() {
        this.wareHouseService = new WareHouseService()
    },
    methods: {
        open(row) {
            this.inputForm = {
                oldTradeName: (row && row.tradeName) || '',
                tradeName: (row && row.tradeName) || '',
                wareHouseType: (row && row.wareHouseType) || '',
                wareHouseTypeName: (row && row.wareHouseTypeName) || ''
            }
            this.visible = true
        },
        submit() {
            if (!this.inputForm.tradeName) {
                uni.showToast({
                    title: '商品名称不能为空',
                    icon: 'none'
                })
                return
            }
            uni.showModal({
                title: '提示',
                content: `确定将 [${this.inputForm.wareHouseTypeName}] 商品 "${this.inputForm.oldTradeName}" 全部改为 "${this.inputForm.tradeName}" 吗？`,
                success: async res => {
                    if (!res.confirm) {
                        return
                    }
                    await this.wareHouseService.saveTradeName(this.inputForm.oldTradeName, this.inputForm.tradeName, this.inputForm.wareHouseType)
                    this.visible = false
                    uni.showToast({
                        title: '修改成功',
                        icon: 'success'
                    })
                    this.$emit('success')
                }
            })
        }
    }
}
</script>
