﻿<template>
	<div class="NCC-common-layout">
		<div class="NCC-common-layout-center">
			<el-row class="NCC-common-search-box" :gutter="16">
				<el-form @submit.native.prevent>
					<el-col :span="6">
						<el-form-item label="产品/套餐">
							<el-input v-model="query.title" placeholder="产品/套餐" clearable />	
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item label="金额/单价">
							<el-input v-model="query.price" placeholder="金额/单价" clearable />	
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item label="原价">
							<el-input v-model="query.originalPrice" placeholder="原价" clearable />	
						</el-form-item>
					</el-col>
					<template v-if="showAll">
					<el-col :span="6">
						<el-form-item label="标签">
							<el-input v-model="query.tags" placeholder="标签" clearable />	
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item label="有效期 (开始)">
							<el-date-picker v-model="query.validStart" type="daterange" value-format="timestamp" format="yyyy-MM-dd" start-placeholder="开始日期" end-placeholder="结束日期">
							</el-date-picker>
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item label="有效期 (结束)">
							<el-date-picker v-model="query.validEnd" type="daterange" value-format="timestamp" format="yyyy-MM-dd" start-placeholder="开始日期" end-placeholder="结束日期">
							</el-date-picker>
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item label="产品类型">
							<el-select v-model="query.type" placeholder="产品类型" >
								<el-option v-for="(item, index) in typeOptions" :key="index" :label="item.fullName" :value="item.id"  />
							</el-select>
						</el-form-item>
					</el-col>

                    <el-col :span="6">
						<el-form-item label="允许分享类型">
							<el-select v-model="query.allowShareUserType" placeholder="允许分享类型" clearable >
								<el-option v-for="(item, index) in allowShareUserTypeOptions" :key="index" :label="item.fullName" :value="item.enCode"  />
							</el-select>
						</el-form-item>
					</el-col>
					</template>
					<el-col :span="6">
						<el-form-item>
							<el-button type="primary" icon="el-icon-search" @click="search()">查询</el-button>
							<el-button icon="el-icon-refresh-right" @click="reset()">重置</el-button>
							<el-button type="text" icon="el-icon-arrow-down" @click="showAll=true" v-if="!showAll">展开</el-button>
							<el-button type="text" icon="el-icon-arrow-up" @click="showAll=false" v-else>收起</el-button>
						</el-form-item>
					</el-col>
				</el-form>
			</el-row>
			<div class="NCC-common-layout-main NCC-flex-main">
				<div class="NCC-common-head">
					<div>
						<el-button type="primary" icon="el-icon-plus" @click="addOrUpdateHandle()">新增</el-button>
						<el-button type="text" icon="el-icon-download" @click="exportData()">导出</el-button>
					</div>
					<div class="NCC-common-head-right">
						<el-tooltip effect="dark" content="刷新" placement="top">
							<el-link icon="icon-ym icon-ym-Refresh NCC-common-head-icon" :underline="false" @click="reset()" />
						</el-tooltip>
						<screenfull isContainer />
					</div>
				</div>
                <NCC-table v-loading="listLoading" :data="list">
					<el-table-column prop="title" label="产品/套餐" align="left" />
					<el-table-column prop="price" label="金额/单价" align="left" />
					<el-table-column prop="originalPrice" label="原价" align="left" />
					<el-table-column prop="discount" label="折扣" align="left" />
					<el-table-column prop="tags" label="标签" align="left" />
					<el-table-column prop="validStart" label="有效期 (开始)"  :formatter="ncc.tableDateFormat" align="left" />
					<el-table-column prop="validEnd" label="有效期 (结束)"  :formatter="ncc.tableDateFormat" align="left" />
					<!-- <el-table-column prop="creatorUserId" label="创建用户" align="left" /> -->
					<el-table-column prop="creatorTime"  :formatter="ncc.tableDateFormat"  label="创建时间" align="left" />
					<!-- <el-table-column prop="lastModifyUserId" label="修改用户" align="left" /> -->
					<el-table-column prop="lastModifyTime"  :formatter="ncc.tableDateFormat"  label="修改时间" align="left" />
                    <el-table-column label="产品类型" prop="type" align="left">
                        <template slot-scope="scope">{{ scope.row.type | dynamicText(typeOptions) }}</template>
                </el-table-column>
                <el-table-column label="会员等级" prop="level" align="left">
                        <template slot-scope="scope">{{ scope.row.level | dynamicText(levelOptions) }}</template>
                </el-table-column>
                <!-- <el-table-column prop="limitUser" label="限制人数" align="left" /> -->
                <!-- <el-table-column prop="limitUserMax" label="最大限制人数" align="left" /> -->
                <el-table-column prop="limitUser" label="限制人数" align="left" >
                    <template slot-scope="scope">
                    {{ !scope.row.isLimitUser?'不限制': scope.row.limitUser }} {{(scope.row.limitUserMax  && scope.row.limitUserMax>0)?('- '+scope.row.limitUserMax): '' }}
                    </template>

                    </el-table-column>
                    <el-table-column prop="free" label="是否免费" align="left" >
                        <template slot-scope="scope">
                        {{ scope.row.free?'是':'否' }}
                        </template>
    
                        </el-table-column>
                <el-table-column label="允许分享类型" prop="allowShareUserType" align="left">
                        <template slot-scope="scope">{{ scope.row.allowShareUserType | dynamicTextCode(allowShareUserTypeOptions) }}</template>
                </el-table-column>
                <!-- <el-table-column prop="isLimitUser" label="限制使用人数" align="left" /> -->
					<el-table-column prop="isUseCoupo" label="可用优惠券" align="left" >
                        <template slot-scope="scope">
                            {{ scope.row.isUseCoupo?'是':'否' }}
                            </template>
                        </el-table-column>
					<el-table-column label="操作" fixed="right" width="100">
							<template slot-scope="scope">
							<el-button type="text" @click="addOrUpdateHandle(scope.row.id)" >编辑</el-button>
							<el-button type="text" @click="handleDel(scope.row.id)" class="NCC-table-delBtn" >删除</el-button>
						</template>
					</el-table-column>
				</NCC-table>
                <pagination :total="total" :page.sync="listQuery.currentPage" :limit.sync="listQuery.pageSize" @pagination="initData" />
            </div>
        </div>
		<NCC-Form v-if="formVisible" ref="NCCForm" @refresh="refresh" />
		<ExportBox v-if="exportBoxVisible" ref="ExportBox" @download="download" />
	</div>
</template>
<script>
    import request from '@/utils/request'
    import { getDictionaryDataSelector } from '@/api/systemData/dictionary'
    import NCCForm from './Form'
    import ExportBox from './ExportBox'
    import { previewDataInterface } from '@/api/systemData/dataInterface'
	export default {
        components: { NCCForm, ExportBox },
        data() {
            return {
                showAll: false,
				query: {
                    title:undefined,
                    price:undefined,
                    originalPrice:undefined,
                    tags:undefined,
                    validStart:undefined,
                    validEnd:undefined,
                    type:undefined,
                    allowShareUserType:undefined,
				},
                list: [],
                listLoading: true,
                multipleSelection: [], total: 0,
                listQuery: {
                    currentPage: 1,
                    pageSize: 20,
					sort: "desc", 
                    sidx: "",
                },
                formVisible: false,
                exportBoxVisible: false,
                columnList: [
                { prop: 'title', label: '产品/套餐' },
                    { prop: 'price', label: '金额/单价' },
                    { prop: 'originalPrice', label: '原价' },
                    { prop: 'discount', label: '折扣' },
                    { prop: 'tags', label: '标签' },
                    { prop: 'validStart', label: '有效期 (开始)' },
                    { prop: 'validEnd', label: '有效期 (结束)' },
                    { prop: 'creatorUserId', label: '创建用户' },
                    { prop: 'creatorTime', label: '创建时间' },
                    { prop: 'lastModifyUserId', label: '修改用户' },
                    { prop: 'lastModifyTime', label: '修改时间' },
                    { prop: 'type', label: '产品类型' },
                    { prop: 'level', label: '会员等级' },
                    { prop: 'limitUser', label: '限制人数' },
                    { prop: 'free', label: '是否免费' },
                    { prop: 'allowShareUserType', label: '允许分享类型' },
                    { prop: 'isLimitUser', label: '限制使用人数' },
                    { prop: 'isUseCoupo', label: '允许使用优惠券' },
                    
				],
				typeOptions:[{"fullName":"商品","id":"0"},{"fullName":"会员套餐","id":"1"}],
				levelOptions : [],
				allowShareUserTypeOptions : [],
			}
        },
		computed: {},
		created() {
			this.initData()
            this.getlevelOptions();
			this.getallowShareUserTypeOptions();
		},
		methods: {
            getlevelOptions(){
				previewDataInterface('303521699263939845').then(res => {
					this.levelOptions = res.data
				});
			},
            getallowShareUserTypeOptions(){
				getDictionaryDataSelector('303681362995447045').then(res => {
					this.allowShareUserTypeOptions = res.data.list
				});
			},
			initData() {
                this.listLoading = true;
                let _query = {
                    ...this.listQuery,
                    ...this.query
                };
                let query = {}
                for (let key in _query) {
                    if (Array.isArray(_query[key])) {
                        query[key] = _query[key].join()
                    } else {
                        query[key] = _query[key]
                    }
                }
                request({
                    url: `/api/Order/BaseProduct`,
                    method: 'GET',
                    data: query
                }).then(res => {
                    this.list = res.data.list
                    this.total = res.data.pagination.total
                    this.listLoading = false
                })
            },
			handleDel(id) {
                this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                    type: 'warning'
                }).then(() => {
                    request({
                        url: `/api/Order/BaseProduct/${id}`,
                        method: 'DELETE'
                    }).then(res => {
                        this.$message({
                            type: 'success',
                            message: res.msg,
                            onClose: () => {
                                this.initData()
                            }
                        });
                    })
                }).catch(() => {
                });
            },
			addOrUpdateHandle(id, isDetail) {
                this.formVisible = true
                this.$nextTick(() => {
                    this.$refs.NCCForm.init(id, isDetail)
                })
            },
			exportData() {
                this.exportBoxVisible = true
                this.$nextTick(() => {
                    this.$refs.ExportBox.init(this.columnList)
                })
            },
            download(data) {
                let query = { ...data, ...this.listQuery, ...this.query }
                request({
                    url: `/api/Order/BaseProduct/Actions/Export`,
                    method: 'GET',
                    data: query
                }).then(res => {
                    if (!res.data.url) return
                    window.location.href = this.define.comUrl + res.data.url
                    this.$refs.ExportBox.visible = false
                    this.exportBoxVisible = false
                })
            },
			search() {
                this.listQuery = {
                    currentPage: 1,
                    pageSize: 20,
                    sort: "desc",
                    sidx: "lastModifyTime",
                }
                this.initData()
            },
            refresh(isrRefresh) {
                this.formVisible = false
                if (isrRefresh) this.reset()
            },
            reset() {
                for (let key in this.query) {
                    this.query[key] = undefined
                }
                this.listQuery = {
                    currentPage: 1,
                    pageSize: 20,
                    sort: "desc",
                    sidx: "lastModifyTime",
                }
                this.initData()
            }
		}
    }
</script>