<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="activityName">
                <el-input v-model="queryParams.activityName" 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>

        <el-table v-loading="loading" fit highlight-current-row border stripe :data="couponItemList">
            <!--      <el-table-column type="selection" width="55" align="center" fixed="left"/>-->
            <!--      <el-table-column label="编号" align="center" prop="id" fixed="left"/>-->
            <!--      <el-table-column label="订单编码" align="center" prop="orderNo" />-->
            <el-table-column label="活动名称" align="center" prop="activityTitle" :show-overflow-tooltip="true" />
            <el-table-column label="活动有效期" align="center" prop="startDate" width="220">
                <template slot-scope="scope"> {{ scope.row.activityStartDate }}至{{ scope.row.activityEndDate }} </template>
            </el-table-column>
            <el-table-column label="优惠券名称" align="center" prop="couponTypeTitle" :show-overflow-tooltip="true" />
            <el-table-column label="优惠券类型" align="center" prop="discountWay" width="120" :show-overflow-tooltip="true">
                <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" width="220">
                <template slot-scope="scope"> {{ scope.row.couponStartDate }} 至 {{ scope.row.couponEndDate }} </template>
            </el-table-column>
            <!--      <el-table-column label="活动ID" align="center" prop="activityInfoId" />-->
            <!--      <el-table-column label="发放门店/工厂" align="center" prop="orgName" width="120" :show-overflow-tooltip="true" />-->
            <!--      <el-table-column label="客户姓名" align="center" prop="appUserName" width="100" />-->
            <!--      <el-table-column label="客户手机号" align="center" prop="appUserPhone" width="120" />-->
            <!--      <el-table-column label="是否核销" align="center" prop="isUsed" width="80">-->
            <!--        <template slot-scope="scope">-->
            <!--          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.isUsed"/>-->
            <!--        </template>-->
            <!--      </el-table-column>-->
            <!--      <el-table-column label="核销门店/工厂" align="center" prop="usedOrgName"  width="120" :show-overflow-tooltip="true"/>-->
            <!--      <el-table-column label="核销时间" align="center" prop="usedTime" width="180">-->
            <!--      </el-table-column>-->
            <!--      <el-table-column label="有效期开始时间" align="center" prop="startDate" width="180">-->
            <!--      </el-table-column>-->
            <!--      <el-table-column label="有效期结束时间" align="center" prop="endDate" width="180">-->
            <!--      </el-table-column>-->
            <el-table-column label="是否单件抵扣券" align="center" prop="isUnique" width="180">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.isUnique" />
                </template>
            </el-table-column>
            <el-table-column label="抵用金额（元）" align="center" prop="deductAmount" width="120" />
            <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>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

        <!-- 发送优惠券对话框 -->
        <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: {
                pageNum: 1,
                pageSize: 10,
                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.loading = true
            listByStore(this.queryParams).then((response) => {
                this.couponItemList = response.data.records
                this.total = response.data.total
                this.loading = false
            })
        },
        // 取消按钮
        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.couponTypeId
            this.sendCouponForm.activityId = row.activityInfoId
            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>
