<template>
    <div class="app-container">
        <el-tabs v-model="goodsType" @tab-click="onChangeGoodsType">
            <el-tab-pane v-for="dict in dict.type.goods_type" :key="dict.value" :label="dict.label" :name="dict.value"></el-tab-pane>
        </el-tabs>

        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item prop="brandName">
                <el-input v-model="queryParams.brandName" placeholder="请输入品牌名称" clearable maxlenth="20" @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" @click="handleAdd" v-hasPermi="['goods:brand:add']">新增</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <Page uri="/mapi/core/brand/list" :request-params="queryParams" ref="pagination">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="品牌编号" align="center" prop="id" />
            <el-table-column label="品牌名称" align="center" prop="brandName" />
            <el-table-column label="状态" align="center" prop="status">
                <template slot-scope="scope">
                    <el-switch v-model="scope.row.status" active-value="0" inactive-value="1" @change="handleStatusChange(scope.row)"></el-switch>
                </template>
            </el-table-column>
            <el-table-column label="创建人" align="center" prop="createBy" />
            <el-table-column label="创建时间" align="center" prop="createTime" />
            <el-table-column label="修改人" align="center" prop="updateBy" />
            <el-table-column label="修改时间" align="center" prop="createTime" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['goods:brand:edit']">修改</el-button>
                    <el-button type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['goods:brand:remove']">删除</el-button>
                </template>
            </el-table-column>
        </Page>

        <!-- 添加或修改商品品牌管理对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
            <el-row :gutter="15">
                <el-form ref="form" :model="form" :rules="rules" label-width="100px">
                    <el-col :span="24">
                        <el-form-item label="品牌名称" prop="brandName">
                            <el-input v-model="form.brandName" placeholder="请输入品牌名称" maxlength="15" :style="{ width: '80%' }" />
                        </el-form-item>
                        <el-form-item label="商品类别" prop="goodsType">
                            <el-select v-model="form.goodsType" placeholder="请选择商品类别" disabled clearable>
                                <el-option v-for="dict in dict.type.goods_type" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-form>
            </el-row>

            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm" style="margin-left: 5%">提交</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { listBrand, getBrand, delBrand, addBrand, updateBrand, updateBrandStatus } from '@/api/core/brand'
export default {
    name: 'Brand',
    dicts: ['goods_type', 'sys_normal_disable'],
    data() {
        return {
            goodsType: '0',
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 商品品牌管理表格数据
            brandList: [],
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                brandName: null
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                brandName: [
                    { required: true, message: '品牌名称不能为空', trigger: 'blur' },
                    { min: 1, max: 15, message: '名称长度必须介于 1 和 15 之间', trigger: 'blur' }
                ],
                goodsType: [{ required: true, message: '商品类别不能为空', trigger: 'blur' }]
            }
        }
    },
    created() {
        this.getList()
    },
    methods: {
        onChangeGoodsType() {
            this.queryParams.goodsType = this.goodsType
            this.handleQuery()
        },
        /** 查询商品品牌管理列表 */
        getList() {
            this.$nextTick(() => {
                this.$refs.pagination.handleSearch(true)
            })
        },
        // 取消按钮
        cancel() {
            this.open = false
            this.reset()
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                brandName: null,
                createBy: null,
                createById: null,
                createTime: null,
                updateBy: null,
                updateById: null,
                updateTime: null,
                remark: null
            }
            this.resetForm('form')
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.getList()
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm('queryForm')
            this.handleQuery()
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset()
            this.form.goodsType = this.goodsType
            this.open = true
            this.title = '添加商品品牌管理'
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset()
            const id = row.id || this.ids
            getBrand(id).then((response) => {
                this.form = response.data
                this.open = true
                this.title = '修改商品品牌管理'
            })
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs['form'].validate((valid) => {
                if (valid) {
                    if (this.form.id != null) {
                        updateBrand(this.form).then((response) => {
                            this.$modal.msgSuccess('修改成功')
                            this.open = false
                            this.getList()
                        })
                    } else {
                        this.form.status = '0'
                        addBrand(this.form).then((response) => {
                            this.$modal.msgSuccess('新增成功')
                            this.open = false
                            this.getList()
                        })
                    }
                }
            })
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids
            this.$modal
                .confirm('是否确认删除商品品牌管理编号为"' + ids + '"的数据项？')
                .then(function () {
                    return delBrand(ids)
                })
                .then(() => {
                    this.getList()
                    this.$modal.msgSuccess('删除成功')
                })
                .catch(() => {})
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download(
                'core/brand/export',
                {
                    ...this.queryParams
                },
                `brand_${new Date().getTime()}.xlsx`
            )
        },
        checkClose(done) {
            this.$confirm('是否关闭表单，关闭后数据将丢失？')
                .then(function () {
                    done()
                })
                .then(() => {})
                .catch(() => {})
        },
        handleStatusChange(row) {
            let text = row.status === '0' ? '启用' : '停用'
            this.$confirm('确认要' + text + '吗？')
                .then(function () {
                    return updateBrandStatus(row.id, row.status)
                })
                .then(() => {
                    this.$modal.msgSuccess(text + '成功')
                })
                .catch(function () {
                    row.status = row.status === '0' ? '1' : '0'
                })
        }
    }
}
</script>
