<template>
	<ciy-header title="我的工单" ref="header"></ciy-header>
	<view v-if="init.code != 1" class="px4 py4">
		<view style="height:2em;width:100%;" class="ciy-skeleton"></view>
		<view style="height:6em;width:100%;" class="ciy-skeleton"></view>
		<view style="height:6em;width:100%;" class="ciy-skeleton"></view>
	</view>
	<view v-else>
		<view class="sti" :style="{top:(header_statusbar_height+header_title_height)+'px'}">
			<ciy-segment lis=":等待中,3:有回复,9:已关闭" v-model="liid" @change="segment_change"></ciy-segment>
		</view>
		<view class="ciy-list" v-for="(item,index) in init.list" :key="item.id" @tap="showmenu(index)">
			<view class="l2" v-html="tobr(item.content)"></view>
			<ciy-showimgs :src="item.imgs" label="图片截图"></ciy-showimgs>
			<view class="rt" v-if="item.closetimes>0">已关闭</view>
			<view class="rt" style="color:var(--succ5)" v-else-if="item.replytimes>0">有回复</view>
			<view class="rt" style="color:var(--warn5)" v-else>等待中</view>
			<view class="lb" v-if="item.contract">{{item.contract}}</view>
			<view class="rb">{{todatetime(item.addtimes)}}</view>
		</view>
		<ciy-listend :page="pageno" :listlen="init.list.length" :nodataimg="file_stor('/img/mbnone.png')"></ciy-listend>
		<view class="ciy-add" @tap="openadd()"></view>
		<ciy-dialog ref="dialog">
			<view class="ciy-form v">
				<label class="imp">问题描述</label>
				<view>
					<ciy-textarea bb v-model="editdata.content" ciystyle="min-height:4em"></ciy-textarea>
				</view>
			</view>
			<view class="ciy-form">
				<label>图片截图</label>
				<view>
					<ciy-upload left num="8" path="problem" v-model="editdata.imgs" imgwidth="1600" sourcetype="camera,album,message"></ciy-upload>
				</view>
			</view>
			<view class="ciy-form">
				<label>联系方式</label>
				<view>
					<ciy-input bb v-model="editdata.contract"></ciy-input>
				</view>
			</view>
			<view class="ciy-form-bottom">
				<button class="btn lg cc" @tap="edited">建立工单</button>
			</view>
		</ciy-dialog>
	</view>
</template>

<style>
</style>

<script>
	export default {
		data() {
			return {}
		},
		onLoad() {
			this.getlist();
		},
		onReachBottom(b) {
			if (this.init.code != 1)
				return;
			this.getlist();
		},
		onPullDownRefresh(b) {
			this.pageno = 0;
			this._stopPullDown = true;
			this.getlist();
		},
		watch: {},
		computed: {},
		methods: {
			async getlist() {
				if (this.pageno < 0)
					return;
				if (this._loading)
					return;
				this.pagepost.pageno = this.pageno + 1;
				this._loading = true;
				var retjson = await this.callfunc({
					func: 'me.problem_pub_list',
					data: this.pagepost
				});
				this._loading = false;
				if (retjson.code != 1)
					return this.alert(retjson.errmsg);
				this.pageno++;
				if (this.pageno == 1)
					this.init.list = [];
				this.init = this.objdeepmerge(this.init, retjson);
				if (retjson.list.length < retjson.pagecount)
					this.pageno = -1;
			},
			openadd() {
				this.editdata.contract = this.me.mobile;
				this.getrefsSync('dialog').Open({
					title: '新建工单'
				});
			},
			async edited() {
				if (!this.editdata.content)
					return this.toast('请输入问题描述');
				if (this._loading)
					return;
				this._loading = true;
				var retjson = await this.callfunc({
					func: 'me.problem_pub_add',
					data: this.editdata
				});
				this._loading = false;
				if (retjson.code != 1)
					return this.alert(retjson.errmsg);
				this.getrefsSync('dialog').Close();
				this.init.list.unshift(retjson.data);
			},
			segment_change(e) {
				this.pagepost.liid = e.value.id;
				this.pageno = 0;
				this.getlist();
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 800
				});
			},
			async close(item) {
				if (await this.askmsg('是否关闭工单？', '关闭工单') != 'ok')
					return;
				var retjson = await this.callfunc({
					func: 'me.problem_pub_close',
					data: {
						id: item.data.id
					}
				});
				if (retjson.code != 1)
					return this.alert(retjson.errmsg);
				this.init.list[item.data._idx].closetimes = this.tostamp();
			},
			showmenu(idx) {
				var dat = this.init.list[idx];
				dat._idx = idx;
				var items = [];
				items.push({
					url: 'problem_chat?id=' + dat.id,
					name: '详情',
				});
				if (dat.closetimes == 0) {
					items.push({
						func: 'close',
						name: '关闭工单',
						data: dat,
						style: 'color:var(--warn6)',
					});
				}
				this.popmenu({
					one: true,
					items
				});
			},
		}
	}
</script>