<template>
    <div>
        <i-page-header :title="pageTitle"></i-page-header>
        <Card :bordered="false" dis-hover class="ivu-mt">
            <div style="padding: 20px 0;">
				<el-select v-model="detial.cate_id" placeholder="选择分类" style="margin:0px 10px 10px 0px;width: 200px;">
					<el-option label="选择分类" value=""></el-option>
				    <el-option :label="item.title" :value="item.id" v-for="(item,index) in cateData" :key="index"></el-option>
				</el-select>
				<el-select v-model="detial.is_hot" placeholder="是否热门" style="margin:0px 10px 10px 0px;width: 200px;">
					<el-option label="选择是否热门" value=""></el-option>
				    <el-option label="热门" value="1"></el-option>
					<el-option label="常规" value="0"></el-option>
				</el-select>
				<el-select v-model="detial.is_hot_sell" placeholder="是否热销" style="margin:0px 10px 10px 0px;width: 200px;">
					<el-option label="选择是否热销" value=""></el-option>
				    <el-option label="热销" value="1"></el-option>
					<el-option label="常规" value="0"></el-option>
				</el-select>
				<el-select v-model="detial.is_ip" placeholder="是否IP商品" style="margin:0px 10px 10px 0px;width: 200px;">
					<el-option label="选择是否IP商品" value=""></el-option>
				    <el-option label="IP商品" value="1"></el-option>
					<el-option label="常规" value="0"></el-option>
				</el-select>
				<div>
					<Input placeholder="请输入标题"  style="width:200px;" class="mr10" v-model="detial.title"/>
					<Input placeholder="请输入买断人推广码"  style="width:200px;" class="mr10" v-model="detial.uip"/>
					<DatePicker style="width: 250px" format="yyyy-MM-dd HH:mm:ss" v-model="detial.update_time" type="datetimerange" placement="bottom-start" placeholder="更新时间范围" :options="options"></DatePicker>
					<el-button type="primary" style="margin-left: 6px;" @click="tapSeach">查询</el-button>
				</div>
                <el-button type="danger" style="margin-top: 10px;" @click="tapAdd">添加商品</el-button>
            </div>
			
			<div class="muser-tab-continer">
			    <a class="muser-tab-a" :class="detial.status==''   ? 'active':''" @click="statusChange('')">全部</a>
			    <a class="muser-tab-a" :class="detial.status=='1'  ? 'active':''" @click="statusChange('1')">上线</a>
			    <a class="muser-tab-a" :class="detial.status=='0'  ? 'active':''" @click="statusChange('0')">下架</a>
			</div>
            <el-table :data="detial.data" v-loading="detial.loading" :stripe="true" :border="true" >
                <el-table-column
                        prop="id"
                        label="ID"
						width="80"
                        align="center">
                </el-table-column>
				<el-table-column
						width="200"
				        prop="title"
				        label="标题">
				</el-table-column>
				<el-table-column
						width="150"
				        prop="price"
				        label="价格">
						<template slot-scope="scope">
							<div>销售价:{{scope.row.price}}</div>
							<div>划线价:{{scope.row.old_price}}</div>
							<div>买断价:{{scope.row.ip_price}}</div>
						</template>
				</el-table-column>
				<el-table-column
						width="100"
				        prop="give_score"
				        label="赠送积分">
				</el-table-column>
				
				<el-table-column
						width="200"
				        prop="describe"
				        label="描述">
				</el-table-column>
				<el-table-column
				         prop="imgs"
				         label="主图"
						 width="160"
				         align="left">
				     <template slot-scope="scope">
						 <img v-for="(item,index) in scope.row.imgs" :key="index" v-lazy="item" :src="item" v-image-preview style="width: 60px;cursor: pointer;margin-right: 5px;"/>
				     </template>
				 </el-table-column>
				 <el-table-column
				          prop="descs"
				          label="详情图"
				 		 width="160"
				          align="left">
				      <template slot-scope="scope">
				 		 <img v-for="(item,index) in scope.row.descs" :key="index" v-lazy="item" :src="item" v-image-preview style="width: 60px;cursor: pointer;margin-right: 5px;"/>
				      </template>
				</el-table-column>

				<el-table-column
				         prop="total"
				         label="状态"
						 width="100"
				         align="left">
				     <template slot-scope="scope">
						<div>状态:
						<el-tag type="danger" size="mini" v-if="scope.row.status == 1">上架</el-tag>
						<el-tag type="info" size="mini" v-if="scope.row.status == 0">下架</el-tag>
						</div>
						<div>热门:
						<el-tag type="danger" size="mini" v-if="scope.row.is_hot == 1">热门</el-tag>
						<el-tag type="info" size="mini" v-if="scope.row.is_hot == 0">常规</el-tag>
						</div>
						<div>热销:
						<el-tag type="danger" size="mini" v-if="scope.row.is_hot_sell == 1">热销</el-tag>
						<el-tag type="info" size="mini" v-if="scope.row.is_hot_sell == 0">常规</el-tag>
						</div>
				     </template>
				 </el-table-column>
				 <el-table-column
				 		width="80"
				         prop="is_ip"
				         label="IP商品">
				 		<template slot-scope="scope">
				 			<el-tag type="danger" size="mini" v-if="scope.row.is_ip == 1">IP商品</el-tag>
				 		</template>
				 </el-table-column>
				 <el-table-column
				 		width="80"
				         prop="ip_title"
				         label="IP标题">
				 </el-table-column>
				 <el-table-column
				 		width="100"
				         prop="uip"
				         label="买断人">
				 </el-table-column>
				 <el-table-column
				 		width="150"
				         prop="update_time"
				         label="更新时间">
				 </el-table-column>
				<el-table-column  align="center" label="操作" prop="handle" width="150" fixed="right">
				    <template slot-scope="scope">
						<div><el-button plain size="mini" @click="tapEdit(scope.row)">编辑</el-button></div>
				    </template>
				</el-table-column>
            </el-table>
            <div class="acea-row row-right page">
                <Page :total="detial.pageCount" show-elevator show-total @on-change="tapPage" :page-size="detial.pageSize"/>
            </div>
        </Card>
		
	<Modal v-model="dialogVisible" :title="title" @on-cancel="editClose" width="800">
		<div>
			<el-form ref="form" :model="form" label-width="120px">
				<el-tabs v-model="formActiveName">
				    <el-tab-pane label="基础信息" name="tab_base">
						<el-form-item label="商品分类" prop="cate_id" :rules="[{ required: true, message: '请选择商品分类'}]">
							<el-select v-model="form.cate_id" placeholder="选择分类">
							    <el-option :label="item.title" :value="item.id" v-for="(item,index) in cateData" :key="index"></el-option>
							</el-select>
						</el-form-item>
						<el-form-item label="商品标题" prop="title" :rules="[{ required: true, message: '请输入商品标题'}]">
							<el-input placeholder="请输入商品标题" autocomplete="off" v-model="form.title"></el-input>
						</el-form-item>
						<el-form-item label="商品描述" prop="describe">
							<el-input placeholder="请输入商品描述" autocomplete="off" v-model="form.describe" type="textarea"></el-input>
						</el-form-item>
						<el-form-item label="商品售价" prop="price" :rules="[{ required: true, message: '请输入商品售价'}]">
							<el-input placeholder="请输入商品售价" autocomplete="off" v-model="form.price"></el-input>
						</el-form-item>
						<el-form-item label="商品划线价" prop="old_price" :rules="[{ required: true, message: '请输入商品划线价'}]">
							<el-input placeholder="请输入商品划线价" autocomplete="off" v-model="form.old_price"></el-input>
						</el-form-item>
						
						<el-form-item label="主图" prop="imgs" :rules="[{ required: true, message: '请上传主图'}]">
							<div class="clearfix" style="width: 100%;">
								<div class="upimg-item fx-r fx-bc fx-ac" style="" v-for="(item,index) in form.imgs" :key="index">
									<img v-lazy="item" :src="item" v-image-preview/>
									<i class="el-icon-error img-remove-btn" @click="removeImgs(index)"></i>
								</div>
								<ui-upload style="float: left;" :upUrl="upUrl"  fileName="上传图片" :headers="upHeaders" :updata="{isz:0,code:'goods'}" @onUpload="onUpload" v-if="form.imgs.length<5"></ui-upload>
							</div>
						</el-form-item>
						<el-form-item label="详情图" prop="descs" :rules="[{ required: true, message: '请上传详情图'}]">
							<div class="clearfix" style="width: 100%;">
								<div class="upimg-item fx-r fx-bc fx-ac" style="" v-for="(item,index) in form.descs" :key="index">
									<img v-lazy="item" :src="item" v-image-preview/>
									<i class="el-icon-error img-remove-btn" @click="removeDescs(index)"></i>
								</div>
								<ui-upload style="float: left;" :upUrl="upUrl"  fileName="上传图片" :headers="upHeaders" :updata="{isz:0,code:'goods'}" @onUpload="onUploadDescs" v-if="form.descs.length<5"></ui-upload>
							</div>
						</el-form-item>
						<el-form-item label="赠送积分" prop="give_score">
							<el-input placeholder="请输入赠送积分数" autocomplete="off" v-model="form.give_score" type="number"></el-input>
						</el-form-item>
						<el-form-item label="是否热门" style="margin-bottom: 0;">
							<el-radio-group v-model="form.is_hot">
								<el-radio :label="1">热门</el-radio>
								<el-radio :label="0">常规</el-radio>
							</el-radio-group>
						</el-form-item>
						<el-form-item label="是否热销" style="margin-bottom: 0;">
							<el-radio-group v-model="form.is_hot_sell">
								<el-radio :label="1">热销</el-radio>
								<el-radio :label="0">常规</el-radio>
							</el-radio-group>
						</el-form-item>
						<el-form-item label="是否上架" style="margin-bottom: 0;">
							<el-radio-group v-model="form.status">
								<el-radio :label="1">上架</el-radio>
								<el-radio :label="0">下架</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-tab-pane>
					<el-tab-pane label="IP设置" name="tab_ip">
						<el-form-item label="是否IP商品" style="margin-bottom: 0;">
							<el-radio-group v-model="form.is_ip">
								<el-radio :label="1">IP商品</el-radio>
								<el-radio :label="0">常规</el-radio>
							</el-radio-group>
						</el-form-item>
						<el-form-item label="商品买断价" prop="ip_price"  v-if="form.is_ip==1" :rules="[{ required: form.is_ip==1?true:false, message: '请输入商品买断价'}]">
							<el-input placeholder="请输入商品买断价" autocomplete="off" v-model="form.ip_price"></el-input>
						</el-form-item>
						<el-form-item label="IP标题" prop="ip_title" v-if="form.is_ip==1">
							<el-input placeholder="请输入IP标题" autocomplete="off" v-model="form.ip_title"></el-input>
						</el-form-item>
						<el-form-item label="IP收益百分比" prop="ip_title" v-if="form.is_ip==1">
							<el-input v-model="form.ip_income_per" style="width: 200px;"><template slot="append">%</template></el-input>
						</el-form-item>
						
					</el-tab-pane>
				    <el-tab-pane label="参数设置" name="tab_parms">
						<el-form-item v-for="(domain, index2) in form.data" :label="domain.title" :key="index2" :prop="'parms_value_' + index2">
						    <el-input v-model="form.data[index2].content" :placeholder="'请输入'+domain.title"></el-input>
							<!-- <el-button @click.prevent="removeParms(index2)">移除</el-button> -->
						</el-form-item>
						<el-form-item label="添加新参数" prop="add_parms_input">
							<el-input placeholder="请输入参数名称" autocomplete="off" v-model="add_parms_name" style="width: 200px;"></el-input>
							<el-button @click.prevent="addParms()">添加</el-button>
						</el-form-item>
					</el-tab-pane>
				</el-tabs>
			</el-form>
			
		</div>
		<span slot="footer" class="dialog-footer">
			<el-button @click="dialogVisible = false">取 消</el-button>
			<el-button type="primary" @click="subAdmin">确 定</el-button>
        </span>
    </Modal>
		
    </div>
</template>
<script>
    import {SystemGoodsList,SystemGoodsAdd,SystemGoodsCate} from "../../../api/system/goods";
	
    import IPageHeader from "../../../layouts/system/page-header/index";
	import UiUpload from "@/ui/upload/index";
	import Setting from '@/setting';
    export default {
        name: "SystemGoodsList",
        components: {UiUpload,IPageHeader},
        computed: {},
        data() {
            return {
				pageTitle:'商品列表',
				upHeaders:{},
				upUrl:"",
				cateData:[],
                detial: {
					data        : [],
					
					cate_id     : "",
					is_hot      : "",
					is_hot_sell : "",
					is_ip       : "",
					title       : "",
					uip			: "",
					time        : "",
					status      : "",
					
                    page        : 1,
					loading     : true,
                    pageSize    : 0,
                    pageCount   : 0,
                },
				
				options: {
				    shortcuts: [
				        {
				            text: '今天',
				            value() {
				                const end = new Date();
				                const start = new Date();
				                start.setTime(new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate()));
				                return [start, end];
				            }
				        },
				        {
				            text: '昨天',
				            value() {
				                const end = new Date();
				                const start = new Date();
				                start.setTime(start.setTime(new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate() - 1)));
				                end.setTime(end.setTime(new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate())));
				                return [start, end];
				            }
				        },
				        {
				            text: '最近7天',
				            value() {
				                const end = new Date();
				                const start = new Date();
				                start.setTime(start.setTime(new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate() - 6)));
				                return [start, end];
				            }
				        },
				        {
				            text: '最近30天',
				            value() {
				                const end = new Date();
				                const start = new Date();
				                start.setTime(start.setTime(new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate() - 29)));
				                return [start, end];
				            }
				        },
				        {
				            text: '本月',
				            value() {
				                const end = new Date();
				                const start = new Date();
				                start.setTime(start.setTime(new Date(new Date().getFullYear(), new Date().getMonth(), 1)));
				                return [start, end];
				            }
				        },
				        {
				            text: '本年',
				            value() {
				                const end = new Date();
				                const start = new Date();
				                start.setTime(start.setTime(new Date(new Date().getFullYear(), 0, 1)));
				                return [start, end];
				            }
				        }
				    ]
				},
				dialogVisible:false,
				title:"",
				add_parms_name:"",
				formActiveName:"tab_base",
				form:{
				    imgs           : [],
					descs          : [],
				    status         : 1,
					is_hot         : 0,
					is_hot_sell    : 0,
					is_ip          : 0,
					cate_id        : "",
					give_score     : 0,
					data           : [],
					ip_income_per  : 0,
				},
				defaultParms:[],
            }
        },

        created() {
			
            this.initView();
        },
        methods: {
			/**
			 * 添加
			 */
			tapAdd:function(sassid){
				var that = this;
			    this.dialogVisible = true;
				that.add_parms_name="",
				that.formActiveName="tab_base",
			    this.$refs['form'].resetFields();
			    this.form = this.$utils.resetFields(this.form, {
				    imgs           : [],
					descs          : [],
				    status         : 1,
					is_hot         : 0,
					is_hot_sell    : 0,
					is_ip          : 0,
					cate_id        : "",
					give_score     : 0,
					data           : JSON.parse(JSON.stringify(that.defaultParms)),
					ip_income_per  : 0,
				});
				
			},
            initView:function(){
				var upHeaders = {};
				this.upUrl = Setting.apiBaseURL + "/systemv1/upload/index";
				const token = this.$utils.util.cookies.get('system_token');
				if (token) {
				    upHeaders['SYSTEM-ACC-TOKEN'] = token;
				}
				this.upHeaders = upHeaders;
                this.getData();
				this.getCateData();
            },
			removeParms:function(index){
				this.$delete(this.form.data,index);
			},
			addParms:function(){
				var name = this.add_parms_name;
				if(this.add_parms_name){
					this.form.data.push({
						title:name,
						content:"",
					})
					this.add_parms_name="";
				}
			},
			statusChange:function(status){
				this.detial.status = status;
				this.detial.page = 1
				this.getData()
			},
			/**
			 * 上传文件
			 * @param res
			 */
			onUpload:function (res) {
			    if(res.code == -1) {
			        Notice.error({ title : "系统提示", content:res.msg});
			    } else {
			        this.form.imgs.push(res.data.img);
			    }
			},
			onUploadDescs:function(res){
				if(res.code == -1) {
				    Notice.error({ title : "系统提示", content:res.msg});
				} else {
				    this.form.descs.push(res.data.img);
				}
			},
			tapEdit:function(row){
				this.formActiveName="tab_base";
				this.form = JSON.parse(JSON.stringify(row));
				this.dialogVisible = true;
				this.title = "编辑商品";
			},
			//关闭
			editClose:function(){
			    this.dialogVisible = false;
			},
			removeImgs:function(index){
				this.$delete(this.form.imgs,index);
			},
			removeDescs:function(index){
				this.$delete(this.form.descs,index);
			},
			/**
			 * 修改数据
			 */
			subAdmin:function(e){
			    this.$refs['form'].validate((valid) => {
			        if (valid) {
			            let loading = this.$loading("提交数据中...");
			            var data = {};
			            for(var i in this.form) {
			                data[i] = this.form[i];
			            }
			            SystemGoodsAdd(data)
			                .then(res => {
			                    loading.close();
			                    if (res.code == 200) {
			                        this.dialogVisible = false;
			                        this.$Message.success("操作成功");
			                        this.getData();
			                    } else {
			                        this.$alert(res.msg, '系统提示');
			                    }
			                })
			                .catch(err => {
			                    loading.close();
			                    this.$alert("网络繁忙，加载失败，请稍等片刻在尝试!", '系统提示');
			                });
			        }
			    });
			},
			
			
            //搜索栏目
            tapSeach:function () {
                this.detial.page = 1;
                this.getData();
            },
            //分页
            tapPage:function (page) {
                this.detial.page = page;
                this.getData();
            },
            getData: function () {
                var data = {};
				var that = this;
                this.detial.loading = true;
				data.page        = this.detial.page;
				data.status      = this.detial.status;
				data.cate_id     = this.detial.cate_id;
				data.is_hot      = this.detial.is_hot;
				data.is_sell_hot = this.detial.is_sell_hot;
				data.is_ip       = this.detial.is_ip;
				data.title       = this.detial.title;
				data.uip         = this.detial.uip;
				data.time        = this.detial.time;
                SystemGoodsList(data)
                    .then(res => {
                        this.detial.loading   = false;
                        this.detial.data      = res.data.list;
                        this.detial.pageSize  = res.data.pageSize;
                        this.detial.pageCount = res.data.pageCount;
						if(that.defaultParms.length<=0 && res.data.defaultParms){
							that.defaultParms = res.data.defaultParms;
						}
                    })
                    .catch(err => {
                        this.detial.loading = false;
                        this.$alert('网络繁忙，加载失败，请稍等片刻在尝试!', '系统提示');
                    });
            },
			getCateData: function () {
			    var that = this,data = {};
				data.pageSize = 100;
			    SystemGoodsCate(data)
			        .then(res => {
						if(res.code == 200 && res.data && res.data.list.length>0) {
						    that.cateData = res.data.list;
						}
			        })
			        .catch(err => {
			           
			        });
			},
        }
    }
</script>
<style scoped>
iframe.videoIframe{border: 0px;padding: 0px;}
.el-dialog__body{padding: 0px;}
.el-dialog__body iframe body{padding: 0px;}

	.muser-tab-continer{border:1px solid #EEEEEE}
    a.muser-tab-a{display: inline-block;padding: 0px 28px;height:42px;line-height: 42px;border-bottom:3px solid transparent;font-size: 17px;color:#595959;}
    a.muser-tab-a.active{border-bottom:3px solid #EA312B;color:#EA312B}
	
	.upimg-item{width: 82px;height: 82px;margin-right: 5px;overflow: hidden;float: left;margin-bottom: 5px;position: relative;}
	.upimg-item img{width: 82px;cursor: pointer;}
	.img-remove-btn{position: absolute;top: 0px;right: 0px;font-size: 24px;}
</style>
