<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item prop="clothItemId">
                <el-select v-model="queryParams.clothItemId" filterable placeholder="选择衣服" clearable>
                    <el-option v-for="item in clothItemList" :label="item.name" :value="item.id" :key="item.name"> </el-option>
                </el-select>
            </el-form-item>
            <!--            <el-form-item prop="washModeId">-->
            <!--                <el-select v-model="queryParams.washModeId" filterable placeholder="洗衣类型" clearable>-->
            <!--                    <el-option v-for="item in washModeList" :label="item.name" :value="item.id" :key="item.name"> </el-option>-->
            <!--                </el-select>-->
            <!--            </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="['activity:clothInfo:add']">新增</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <Page uri="/mapi/activity/clothItem/list" :request-params="queryParams" ref="pagination">
            <el-table-column type="selection" width="55" align="center" fixed="left" />
            <el-table-column label="序号" align="center" prop="id" />
            <el-table-column label="衣服名称" align="center" prop="clothItemName" />
            <el-table-column label="指定折扣" align="center" prop="discount" />
            <!--            <el-table-column label="指定价格" align="center" prop="price" />-->
            <!--            <el-table-column label="洗衣类型" align="center" prop="washModeName" />-->
            <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" fixed="right" width="150">
                <template slot-scope="scope">
                    <el-button type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['activity:clothInfo:edit']">修改</el-button>
                    <el-button type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['activity:clothInfo:remove']">删除</el-button>
                </template>
            </el-table-column>
        </Page>

        <!-- 添加或修改指定衣服折扣活动明细对话框 -->
        <el-dialog :title="title" :visible.sync="open" size="30%" width="50%" append-to-body>
            <el-row :gutter="15">
                <el-form ref="form" :model="form" :rules="rules" label-width="150px">
                    <el-col :span="24">
                        <el-form-item label="衣服" prop="clothItemId">
                            <el-select v-model="form.clothItemId" filterable placeholder="选择衣服" clearable>
                                <el-option v-for="item in clothItemList" :label="item.name" :value="item.id" :key="item.name"> </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <!-- <el-col :span="24">
                        <el-form-item label="洗衣类型" prop="washModeIds">
                            <el-select v-model="washModeIds" filterable placeholder="洗衣类型" clearable multiple>
                                <el-option v-for="item in washModeList" :label="item.name" :value="item.id" :key="item.name"> </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col> -->
                    <!--                    <el-col :span="24">-->
                    <!--                        <el-form-item label="指定价格（元）" prop="price">-->
                    <!--                            <el-input-number v-model="form.price" placeholder="请输入指定价格" :precision="2" :min="0" :max="999999" />-->
                    <!--                        </el-form-item>-->
                    <!--                    </el-col>-->
                    <el-col :span="24">
                        <el-form-item label="指定折扣" prop="discount">
                            <el-input-number v-model="form.discount" placeholder="请输入指定折扣" :precision="2" :min="0" :max="1" :step="0.01" />
                        </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 { listActivityClothItem, getActivityClothItem, delActivityClothItem, addActivityClothItem, updateActivityClothItem, updateActivityClothItemStatus } from '@/api/activity/clothItem'

import { listClothItem } from '@/api/core/clothItem'

import { listWashMode } from '@/api/core/washMode'

export default {
    name: 'ClothItem',
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            washModeIds: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 指定衣服折扣活动明细表格数据
            activityClothItemList: [],
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {},
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                clothItemId: [{ required: true, message: '衣服ID不能为空', trigger: 'change' }],
                price: [{ required: true, message: '指定价格不能为空', trigger: 'blur' }]
            },
            activityId: null,
            clothItemList: [],
            washModeList: []
        }
    },
    created() {
        this.activityId = this.$route.params && this.$route.params.activityId
        listClothItem({ pageSize: 99999, pageNum: 1, status: '0' }).then((res) => {
            this.clothItemList = res.rows
        })
        listWashMode({ pageSize: 99999, pageNum: 1, status: '0' }).then((res) => {
            this.washModeList = res.rows
        })
        this.getList()
    },
    methods: {
        /** 查询指定衣服折扣活动明细列表 */
        getList() {
            this.$nextTick(() => {
                this.$refs.pagination.handleSearch(true)
            })
        },
        // 取消按钮
        cancel() {
            this.open = false
            this.reset()
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                activityId: null,
                clothItemId: null,
                price: null,
                washModeIds: null,
                delFlag: null,
                createById: null,
                createBy: null,
                createTime: null,
                updateById: null,
                updateBy: 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.open = true
            this.title = '添加指定衣服折扣活动明细'
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset()
            const id = row.id || this.ids
            getActivityClothItem(id).then((response) => {
                this.form = response.data
                this.washModeIds = this.form.washModeIds.split(',')
                this.open = true
                this.title = '修改指定衣服折扣活动明细'
            })
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs['form'].validate((valid) => {
                if (valid) {
                    this.form.washModeIds = this.washModeIds.join(',')
                    if (this.form.id != null) {
                        updateActivityClothItem({ ...this.form, ...{ activityId: this.activityId } }).then((response) => {
                            this.$modal.msgSuccess('修改成功')
                            this.open = false
                            this.getList()
                        })
                    } else {
                        addActivityClothItem({ ...this.form, ...{ activityId: this.activityId } }).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 delActivityClothItem(ids)
                })
                .then(() => {
                    this.getList()
                    this.$modal.msgSuccess('删除成功')
                })
                .catch(() => {})
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download(
                'mapi/activity/clothItem/export',
                {
                    ...this.queryParams
                },
                `clothItem_${new Date().getTime()}.xlsx`
            )
        },
        checkClose(done) {
            this.$confirm('是否关闭表单，关闭后数据将丢失？')
                .then(function () {
                    done()
                })
                .then(() => {})
                .catch(() => {})
        }
    }
}
</script>
