<template>
    <div>
        <el-table :data="setList" ref="table">
            <el-table-column v-if="multipleMode" align="center" type="selection" width="50"> </el-table-column>
            <el-table-column prop="name" label="名称" min-width="120px"> </el-table-column>
            <el-table-column prop="num" label="数量"> </el-table-column>
            <el-table-column prop="unit" label="单位"> </el-table-column>
            <el-table-column prop="price" label="价格"> </el-table-column>
            <el-table-column prop="remark" label="备注" min-width="100px"> </el-table-column>
            <el-table-column label="操作" align="center" fixed="right" min-width="150">
                <template slot-scope="{ row }">
                    <el-button @click="editRow(row)" type="primary" size="mini" plain>编辑</el-button>
                    <el-button @click="deleteRow(row)" type="danger" size="mini" plain>删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <div class="pagination-wrapper">
            <el-button @click="editRow()" type="primary">添加</el-button>
            <el-button @click="$router.go(-1)">取消</el-button>
        </div>

        <el-dialog :visible.sync="showDialog" width="500px" title="套餐内商品" :close-on-click-modal="false">
            <el-form :model="formData" :rules="rules" ref="form" label-width="80px" label-position="right" size="small">
                <el-form-item prop="goodsInfoId" label="名称">
                    <el-select v-model="formData.goodsInfoId" filterable @change="optionChange" class="cl-input">
                        <el-option v-for="item in goodsList" :key="item.id" :value="item.id" :label="item.name">
                            <span style="float: left">{{ item.name }}</span>
                            <span style="float: right; color: #8492a6; font-size: 13px">{{ item.unit }}</span>
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item prop="num" label="数量">
                    <el-input-number type="number" v-model="formData.num" class="cl-input" :min="1"></el-input-number>
                </el-form-item>
                <el-form-item prop="remark" label="备注">
                    <el-input v-model="formData.remark" class="cl-input"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer">
                <el-button type="primary" size="mini" @click="save" :loading="saving">保存</el-button>
            </span>
        </el-dialog>
    </div>
</template>
<script>
export default {
    name: 'PackageGoodsTable',
    data() {
        return {
            multipleMode: false,
            search: '',
            id: '',
            setList: [],
            showDialog: false,
            formData: {},
            saving: false,
            rules: {
                goodsInfoId: [
                    {
                        required: true,
                        message: '请选择商品',
                        trigger: 'blur'
                    }
                ],
                num: [
                    {
                        required: true,
                        message: '请输入数量',
                        trigger: 'blur'
                    }
                ]
            },
            goodsList: [],
            goods: {}
        };
    },
    computed: {
        selection() {
            return this.$refs.table.selection.map(i => i.id);
        }
    },
    mounted() {
        this.$http
            .post('goodsInfo/allList')
            .then(res => {
                this.goodsList = res;
            })
            .catch(e => {
                console.log(e);
                this.$message.error(e.error);
            });
    },
    methods: {
        /*beforeGetData() {
            return { search: this.search };
        },*/
        toggleMultipleMode(multipleMode) {
            this.multipleMode = multipleMode;
            if (!multipleMode) {
                this.$refs.table.clearSelection();
            }
        },
        addRow() {
            this.$router.push({
                path: '/packageGoodsEdit',
                query: {
                    ...this.$route.query
                }
            });
        },
        editRow(row, edit) {
            this.edit = edit;
            if (!row) {
                row = {
                    name: '',
                    price: ''
                };
            }
            this.formData = { ...row };
            this.showDialog = true;
        },
        download() {
            this.downloading = true;
            this.$axios
                .get('/packageGoods/excel', {
                    responseType: 'blob',
                    params: { size: 10000 }
                })
                .then(res => {
                    console.log(res);
                    this.downloading = false;
                    const downloadUrl = window.URL.createObjectURL(new Blob([res.data]));
                    const link = document.createElement('a');
                    link.href = downloadUrl;
                    link.setAttribute('download', res.headers['content-disposition'].split('filename=')[1]);
                    document.body.appendChild(link);
                    link.click();
                    link.remove();
                })
                .catch(e => {
                    console.log(e);
                    this.downloading = false;
                    this.$message.error(e.error);
                });
        },
        operation1() {
            this.$notify({
                title: '提示',
                message: this.selection
            });
        },
        operation2() {
            this.$message('操作2');
        },
        deleteRow(row) {
            this.$alert('删除将无法恢复，确认要删除么？', '警告', { type: 'error' })
                .then(() => {
                    return this.$http.post(`/packageGoods/del/${row.id}`);
                })
                .then(() => {
                    this.$message.success('删除成功');
                    this.$router.go(0);
                })
                .catch(e => {
                    if (e !== 'cancel') {
                        this.$message.error(e.error);
                    }
                });
        },
        getList(id) {
            if (id) {
                this.id = id;
            }
            this.$http
                .post('/packageGoods/getSetGoods', {
                    packageId: this.id
                })
                .then(res => {
                    this.setList = res;
                })
                .catch(e => {
                    console.log(e);
                });
        },
        save() {
            this.$refs.form.validate(valid => {
                if (valid) {
                    let data = { ...this.formData };
                    data.price = this.goods.price * data.num;
                    data.name = this.goods.name;
                    data.unit = this.goods.unit;
                    data.packageId = this.id;
                    this.saving = true;
                    this.$http
                        .post('/packageGoods/save', data, { body: 'json' })
                        .then(res => {
                            this.saving = false;
                            this.$message.success('成功');
                            this.showDialog = false;
                            this.$router.go(0);
                        })
                        .catch(e => {
                            console.log(e);
                            this.saving = false;
                            this.showDialog = false;
                            this.$message.error(e.error);
                        });
                } else {
                    return false;
                }
            });
        },
        optionChange(id) {
            this.goods = this.goodsList.find(item => {
                return item.id === id;
            });
        }
    }
};
</script>
<style lang="less" scoped>
.cl-input {
    width: 300px;
}
</style>
