<template>
	<div class="container" style="padding: 10px;">
		<el-form :inline="true" :model="model" class="demo-form-inline">
			<el-form-item label="关键字">
				<el-input v-model="model.KeyWord" placeholder="关键字"></el-input>
			</el-form-item>

			<el-form-item>
				<el-button type="primary" @click="onSubmit">查询</el-button>
			</el-form-item>
			<el-form-item>
				<el-button type="success" @click="addorupdpartner(null)">新增</el-button>
			</el-form-item>
		</el-form>
		<el-table :data="tableData" style="width: 100%">
			<el-table-column prop="date" label="ID" v-if="false">
				<template slot-scope="scope">
					{{scope.row.id}}
				</template>
			</el-table-column>
			<el-table-column prop="partnerName" label="合伙人姓名">
				<template slot-scope="scope">
					{{scope.row.partnerName}}
				</template>
			</el-table-column>
			<el-table-column prop="remark" label="合伙人备注">
				<template slot-scope="scope">
					{{scope.row.remark}}
				</template>
			</el-table-column>
			<el-table-column prop="status" label="状态">
				<template slot-scope="scope">
					<el-tag :type="scope.row.status==0?'success':'warn'">
						{{scope.row.status==0?'使用中':'禁用中'}}
					</el-tag>
				</template>
			</el-table-column>
			<el-table-column prop="add_time" label="添加时间">
				<template slot-scope="scope">
					{{scope.row.add_time}}
				</template>
			</el-table-column>
			<el-table-column label="屏蔽">
				<template slot-scope="scope">
					<el-button type="primary" @click="ShieldPerSon(scope.row.id)">屏蔽
					</el-button>
				</template>
			</el-table-column>
			<el-table-column label="查看推广">
				<template slot-scope="scope">
					<el-button type="primary" @click="RedirectOrder(scope.row.id)">查看推广</el-button>
				</template>
			</el-table-column>
			<el-table-column label="编辑">
				<template slot-scope="scope">
					<el-button type="success" @click="addorupdpartner(scope.row)">编辑</el-button>
				</template>
			</el-table-column>
			<el-table-column label="删除">
				<template slot-scope="scope">
					<el-button type="warning" @click="DelPartner(scope.row.id)">删除</el-button>
				</template>
			</el-table-column>
			<el-table-column label="添加推广(经理)">
				<template slot-scope="scope">
					<el-button type="primary" @click="Addsubordinate(scope.row.id)">推广用户</el-button>
				</template>
			</el-table-column>
		</el-table>
		<el-pagination background layout="prev, pager, next" :total="model.TotalCount" @current-change="Pages"
			style="text-align: right;margin-top: 10px;">
		</el-pagination>
		<el-dialog title="提示" :visible.sync="dialogVisible" width="50%">
			<el-form ref="form" :model="form" label-width="80px" :rules="rulesform">
				<el-form-item label="合伙人姓名" prop="partnerName">
					<el-input v-model="form.partnerName" placeholder="合伙人姓名" maxlength="12"></el-input>
				</el-form-item>
				<el-form-item label="合伙人备注" prop="remark">
					<el-input v-model="form.remark" placeholder="合伙人备注" max="20"></el-input>
				</el-form-item>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button @click="dialogVisible = false">取 消</el-button>
				<el-button type="primary" @click="SubmitPartner">确 定</el-button>
			</span>
		</el-dialog>
		<el-dialog title="新增推广人员" :visible.sync="dialogVisibleX" width="30%">
			<el-form ref="formpartner" :model="formpartner" label-width="80px" :rules="partneruserForm">
				<el-form-item label="用户ID" prop="User_id">
					<el-input type="text" placeholder="请输入用户ID(编号)" v-model="formpartner.User_id"></el-input>
				</el-form-item>
				<el-form-item label="合伙人ID" prop="Partner_id" v-if="false">
					<el-input type="text" placeholder="请输入合伙人ID" v-model="formpartner.Partner_id" disabled></el-input>
				</el-form-item>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button @click="dialogVisibleX = false">取 消</el-button>
				<el-button type="primary" @click="SubmitPartnerUser">确 定</el-button>
			</span>
		</el-dialog>
		<el-dialog title="外部链接" :visible.sync="dialogVisibleR" width="50%">
			<span>外部链接:{{linkR}}</span>
			<span slot="footer" class="dialog-footer">
				<el-button @click="dialogVisibleR = false">取 消</el-button>
				<el-button type="primary" @click="dialogVisibleR = false">确 定</el-button>
			</span>
		</el-dialog>
	</div>
</template>

<script>
	import {
		GetListPartner,
		AddOrUpdatePartner,
		DelPartner,
		UpdatePartner,
	} from '../../api/Partner.js'
	import {
		AddOrUpdatePartnerUser
	} from '../../api/partneruser.js'
	import utils from '../../utils/utils.js'
	export default {
		data() {
			return {
				dialogVisibleX: false,
				dialogVisible: false,
				dialogVisibleR: false,
				ShieldTxt: true,
				linkR: '',
				tableData: [],
				model: {
					"KeyWord": "",
					"TotalCount": 0,
					"PageIndex": 1,
					"PageSize": 20,
					"Sort": [{
						"Field": "",
						"Type": 0
					}]
				},
				form: {
					"id": 0,
					"partnerName": "",
					"remark": "",
					"status": 0,
					"add_time": "2022-01-26T08:46:59.928Z",
					"update_time": "2022-01-26T08:46:59.928Z",
				},
				rulesform: {
					partnerName: [{
						required: true,
						message: '请输入合伙人姓名',
						trigger: 'blur'
					}]
				},
				// 合伙人绑定销售经理
				partneruserForm: {
					User_id: [{
						required: true,
						message: '请输入用户ID(编号)',
						trigger: 'blur'
					}],
					Partner_id: [{
						required: true,
						message: '请输入合伙人ID',
						trigger: 'blur'
					}]
				},
				// 合伙人绑定销售经理
				formpartner: {
					"id": 0,
					"User_id": 0,
					"Partner_id": 0,
					"status": 0
				},
				//修改合伙人状态
				statusModel: {
					"id": 0,
					"status": 0
				}
			}
		},
		created() {
			this.ShowPartner()
		},
		methods: {
			//屏蔽合伙人
			ShieldPerSon(id) {
				this.statusModel.id=id
				this.ShieldTxt = !this.ShieldTxt
				if(this.ShieldTxt){
					this.statusModel.status=0
				}else{
					this.statusModel.status=-99
				}
				UpdatePartner(this.statusModel).then(res => {
					if (res.code == 200) {
						this.$message.success('操作成功')
						this.ShowPartner()
					} else {
						this.$message.error('屏蔽失败')
					}
				})
			},
			// 新增销售经理
			Addsubordinate(id) {
				console.log('id', id)
				this.dialogVisibleX = true
				this.formpartner.Partner_id = id

			},
			// 合伙人绑定销售经理提交
			SubmitPartnerUser() {
				this.$refs.formpartner.validate((valid) => {
					if (valid) {
						AddOrUpdatePartnerUser(this.formpartner).then(res => {
							console.log('数', res)
							if (res.data.code == 200) {
								this.$message.success('绑定销售经理成功')
								this.dialogVisibleX = false
							} else {
								this.$message.error(res.data.message)
							}
						})
					}
				})
			},

			// 查看订单
			RedirectOrder(id) {
				let links = 'https://shangten.yiqituancity.com/#/externalInfo?id=' + utils.Encrypt(id)
				this.linkR = links
				this.dialogVisibleR = true
				// this.$router.push({
				// 	path: '/externalInfo?id=' +id
				// })

			},
			Pages(e) {
				this.model.PageIndex = e
				this.ShowPartner()
			},
			// 查询所有合伙人信息
			ShowPartner() {
				GetListPartner(this.model).then(res => {
					console.log('Shuju', res)
					res.data.data.rows.forEach((item, index) => {
						item.add_time = utils.formatTime(item.add_time, "yyyy-MM-dd HH:mm:ss")
					})
					this.tableData = res.data.data.rows
					this.model.TotalCount = res.data.data.total
				})
			},
			// 提交编辑修改
			SubmitPartner() {
				this.$refs.form.validate((valid) => {
					if (valid) {
						AddOrUpdatePartner(this.form).then(res => {
							console.log('添加修改数据', res)
							if (res.data.code == 200) {
								this.$message.success('操作成功')
								this.dialogVisible = false
								this.ShowPartner()
							} else {
								this.$message.error('操作失败')
							}
						})
					}
				})
			},
			addorupdpartner(obj) {
				this.dialogVisible = true
				if (obj.id > 0) {
					// 修改
					this.form.id = obj.id
					this.form.partnerName = obj.partnerName
					this.form.remark = obj.remark
					this.form.status = obj.status
				} else {
					// 新增
					this.form.id = 0
					this.form.partnerName = ''
					this.form.remark = ''
					this.form.status = 0
				}
			},
			DelPartner(id) {
				DelPartner(id).then(res => {
					if (res.code == 200) {
						this.$message.success('删除成功')
						this.ShowPartner()
					} else {
						this.$message.error('删除失败')
					}
				})
			},
			// 提交查询
			onSubmit() {

			}
		}
	}
</script>

<style>
</style>
