<template>
	<ciy-header title="我的工单" ref="header"></ciy-header>
	<view v-if="init.code != 1" class="px4 py4">
		<view style="height:4em;width:100%;" class="ciy-skeleton"></view>
		<view style="height:3em;width:60%;" class="ciy-skeleton"></view>
		<view style="height:8em;width:60%;margin-left:40%;" class="ciy-skeleton"></view>
		<view style="height:5em;width:60%;" class="ciy-skeleton"></view>
	</view>
	<view v-else class="flex flexcol" :style="'height:calc(100vh - ' + (header_statusbar_height+header_title_height) + 'px);'">
		<scroll-view @scrolltoupper="chgupper" :scroll-top="top" id="id_chats" class="flex1" style="overflow: auto;" scroll-y>
			<view class="ciy-list">
				<view class="l2" v-html="tobr(init.problem.content)"></view>
				<ciy-showimgs :src="init.problem.imgs" label="图片截图"></ciy-showimgs>
				<view class="rb">{{todatetime(init.problem.addtimes)}} 提交</view>
			</view>
			<view :class="item._od" class="vchat" v-for="(item,index) in init.list" :key="item.id">
				<view class="nick" v-if="item.nick">{{item.nick}}</view>
				<view class="chat" v-html="tobr(item.content)"></view>
				<view class="time">{{todatetime(item.addtimes,'st')}}</view>
			</view>
		</scroll-view>
		<form v-if="init.problem.closetimes==0">
			<view class="bg2 vchatsend">
				<!-- <view><button class="btn def" @tap="sendimg">图片</button></view> -->
				<view style="flex:1;"><ciy-textarea v-model="editdata.content"></ciy-textarea></view>
				<view><button @tap="submitsend" class="btn">发送</button></view>
			</view>
		</form>
		<view class="bg2 txt-center px4 py4" v-else>
			工单已关闭
		</view>
		<view class="bg2" :style="{height:footer_safe_height+'px'}"></view>
	</view>
</template>

<style>
	.vchatsend {
		display: flex;
		align-items: center;
		border-top: 2px solid var(--bg1);
		padding: 0 0.5em;
		gap: 0.5em;
	}

	.vchat {
		padding: 0.5em 1em;
	}

	.vchat .nick {
		font-size: 0.7em;
		color: var(--txt1);
		margin-left: 0.5em;
	}

	.vchat .time {
		font-size: 0.5em;
		color: var(--txt1);
		margin-left: 0.5em;
	}

	.vchat.me {
		text-align: right;
	}

	.vchat.me .chat {
		background: var(--succ4);
		color: var(--txt9);
		display: inline-block;
		clear: both;
		padding: 0.5em;
		border-radius: 0.5em;
		min-width: 5em;
		text-align: left;
	}

	.vchat.you .chat {
		background: var(--bg1);
		display: inline-block;
		clear: both;
		padding: 0.5em;
		border-radius: 0.5em;
		min-width: 5em;
	}
</style>

<script>
	export default {
		data() {
			return {
				top: 999999999,
			}
		},
		onLoad() {
			this.maxid = 0;
			this.minid = 2147483647;
			this.getlist();
			this._tout = setInterval(() => {
				this.getnew(true);
			}, 3000);
		},
		onUnload(opn) {
			if (this._tout)
				clearTimeout(this._tout);
		},
		watch: {},
		computed: {},
		methods: {
			async getlist() {
				if (this.minid == 0)
					return;
				if (this._loading)
					return;
				this._loading = true;
				var retjson = await this.callfunc({
					func: 'me.problem_chat_get',
					data: {
						mid: this.minid,
						op: 'min',
						id: this.opn.id,
						once: !this.init.once
					}
				});
				this._loading = false;
				this.callist(retjson.list);
				this.init = this.objdeepmerge(this.init, retjson, true);
				if (retjson.list.length < retjson.pagecount)
					this.minid = 0;
			},
			async getnew(blong) {
				var retjson = await this.callfunc({
					func: 'me.problem_chat_get',
					data: {
						mid: this.maxid,
						op: 'max',
						id: this.opn.id,
						once: !this.init.once
					}
				});
				if (retjson.list.length == 0)
					return;
				this.callist(retjson.list);
				this.init = this.objdeepmerge(this.init, retjson);
				setTimeout(() => {
					this.top = 999999999 + this.maxid;
				}, 300);
			},
			callist(list) {
				for (var i in list) {
					var id = parseInt(list[i].id);
					if (id > this.maxid)
						this.maxid = id;
					if (id < this.minid)
						this.minid = id;
					if (list[i].sendtype == 1) {
						list[i]._od = 'me';
					} else if (list[i].sendtype == 2) {
						list[i].nick = '工程师回复';
						list[i]._od = 'you';
					} else {
						list[i].nick = 'AI助手';
						list[i]._od = 'you';
					}
				}
			},
			chgupper(e) {
				this.getlist();
			},
			async submitsend() {
				if (!this.editdata.content)
					return this.toast('请输入内容');
				if (this._loading)
					return;
				this._loading = true;
				var retjson = await this.callfunc({
					func: 'me.problem_chat_send',
					data: this.editdata
				});
				this._loading = false;
				if (retjson.code != 1)
					return this.alert(retjson.errmsg);
				this.editdata.content = '';
				retjson.list = [retjson.data];
				this.callist(retjson.list);
				this.init = this.objdeepmerge(this.init, retjson);
				setTimeout(() => {
					this.top = 999999999 + this.maxid;
				}, 300);
			},
			sendimg() {}
		}
	}
</script>