<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="90px">
            <el-form-item prop="discountWay">
                <el-select v-model="queryParams.discountWay" placeholder="请选择优惠券类型" clearable>
                    <el-option v-for="dict in dict.type.coupon_type" :key="dict.value" :label="dict.label" :value="dict.value" />
                </el-select>
            </el-form-item>
            <el-form-item prop="title">
                <el-input v-model="queryParams.title" placeholder="请输入优惠券名称" clearable @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="['app:couponItem:add']"-->
            <!--        >手动发送优惠券</el-button>-->
            <!--      </el-col>-->
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <Page uri="/mapi/activity/couponInfo/listByStore" :request-params="queryParams" ref="pagination">
            <el-table-column label="编号" align="center" prop="id" width="80" />
            <el-table-column label="名称" align="center" prop="title" />
            <el-table-column label="优惠券类型" align="center" prop="discountWay" width="120">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.coupon_type" :value="scope.row.discountWay" />
                </template>
            </el-table-column>
            <el-table-column label="券值" align="center" prop="deductAmount" width="150">
                <template slot-scope="scope">
                    <span v-if="scope.row.discountWay == 0">满{{ scope.row.minAmount }}元减{{ scope.row.deductAmount }}元</span>
                    <span v-if="scope.row.discountWay == 1">满{{ scope.row.minAmount }}件减{{ scope.row.deductAmount }}件</span>
                    <span v-if="scope.row.discountWay == 2">折扣比例：{{ scope.row.discountRate }}%</span>
                </template>
            </el-table-column>
            <el-table-column label="有效期" align="center" prop="startDate" width="210">
                <template slot-scope="scope"> {{ scope.row.startDate }} ~ {{ scope.row.endDate }} </template>
            </el-table-column>
            <el-table-column label="适用范围" align="center" prop="useScope" width="180">
                <template slot-scope="scope">
                    <span v-if="scope.row.useScope == 1">全部衣服</span>
                    <span v-if="scope.row.useScope == 2">指定衣服</span>
                </template>
            </el-table-column>
            <el-table-column label="优惠券库存" align="center" prop="couponStock" width="120" />
            <el-table-column label="备注" align="center" prop="remark">
                <template slot-scope="scope">
                    <span>{{ scope.row.remark ? scope.row.remark : '--' }}</span>
                </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" class-name="small-padding fixed-width" fixed="right" width="150" v-if="isUsed != 'Y'">
                <template slot-scope="scope">
                    <el-button type="text" icon="el-icon-plus" @click="handleAdd(scope.row)" v-hasPermi="['app:couponItem:add']">发放优惠券</el-button>
                </template>
            </el-table-column>
        </Page>

        <!-- 发送优惠券对话框 -->
        <el-dialog :title="sendCouponTitle" :visible.sync="sendCouponOpen" size="30%" append-to-body>
            <el-row :gutter="15">
                <el-form ref="form" :model="sendCouponForm" :rules="rules" label-width="140px">
                    <el-col :span="24">
                        <el-form-item label="用户手机号" prop="appUserPhoneNumber">
                            <el-input style="width: 200px" v-model.number="sendCouponForm.appUserPhoneNumber" placeholder="请输入手机号" clearable maxlength="11" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="一次性发送数量" prop="sendNum">
                            <el-input-number v-model.number="sendCouponForm.sendNum" placeholder="请输入发送数量" :controls="false" :min="1" />
                        </el-form-item>
                    </el-col>
                </el-form>
            </el-row>

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

<script>
import { listCouponItem, getCouponItem, delCouponItem, sendCoupon, updateCouponItem, updateCouponItemStatus } from '@/api/coupon/item'
import { allOrg } from '@/api/system/store'
import { allValidList } from '@/api/coupon/type'
import { listByStore } from '@/api/activity/couponInfo'

export default {
    name: 'CouponItem',
    dicts: ['sys_source_type', 'sys_yes_no', 'coupon_type'],
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 优惠券表格数据
            couponItemList: [],
            // 弹出层标题
            title: '',
            sendCouponTitle: '',
            // 是否显示弹出层
            open: false,
            sendCouponOpen: false,
            // 查询参数
            queryParams: {
                orgIndex: null,
                dateRange: []
            },
            // 表单参数
            form: {},
            // 表单参数
            sendCouponForm: {},
            // 表单校验
            rules: {
                duringDate: [{ required: true, message: '有效期不能为空', trigger: 'blur' }],
                orgId: [{ required: true, message: '门店不能为空', trigger: 'blur' }],
                couponTypeId: [{ required: true, message: '优惠券不能为空', trigger: 'blur' }],
                appUserPhoneNumber: [{ required: true, message: '用户手机号不能为空', trigger: 'blur' }],
                sendNum: [{ required: true, message: '发放数量不能为空', trigger: 'blur' }]
            },
            orgList: [],
            isUsed: null,
            storeList: [],
            factoryList: [],
            dateRange: []
        }
    },
    created() {
        this.handleUser()
        this.isUsed = this.getUrlParam('isUsed')
        if (this.isUsed == 'Y') {
            this.queryParams.isUsed = this.isUsed
        }
        this.getList()
        allOrg().then((res) => {
            this.orgList = res.data
            this.orgList.forEach((org) => {
                if (org.sourceType == '01') {
                    this.factoryList.push(org)
                } else {
                    this.storeList.push(org)
                }
            })
        })
    },
    methods: {
        handleUser() {
            this.userInfoVO = this.getUserInfo()
        },
        /** 查询优惠券列表 */
        getList() {
            this.$nextTick(() => {
                this.$refs.pagination.handleSearch(true)
            })
        },
        // 取消按钮
        cancel() {
            this.open = false
            this.reset()
        },
        // 取消按钮
        sendCouponCancel() {
            this.sendCouponOpen = false
            this.reset()
        },
        // 表单重置
        reset() {
            this.sendCouponForm = {
                couponTypeId: null,
                activityId: null,
                orgId: null,
                sourceType: null,
                appUserPhoneNumber: null,
                sendNum: null
            }
            this.resetForm('form')
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1
            this.getList()
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm('queryForm')
            this.handleQuery()
        },
        /** 发送优惠券按钮操作 */
        handleAdd(row) {
            this.reset()
            this.sendCouponForm.couponTypeId = row.id
            this.sendCouponOpen = true
            this.sendCouponTitle = '发送优惠券'
        },
        /** 提交发送优惠券 */
        submitSendCouponForm() {
            this.$refs['form'].validate((valid) => {
                if (valid) {
                    this.sendCouponForm.sourceType = this.userInfoVO.userType
                    this.sendCouponForm.orgId = this.userInfoVO.storeId
                    sendCoupon(this.sendCouponForm).then((response) => {
                        this.$modal.msgSuccess('发放成功')
                        this.sendCouponOpen = false
                        this.getList()
                    })
                }
            })
        },
        checkClose(done) {
            this.$confirm('是否关闭表单，关闭后数据将丢失？')
                .then(function () {
                    done()
                })
                .then(() => {})
                .catch(() => {})
        }
    }
}
</script>
