<template>
	<div>
		<div v-for="obj in dataArray" :class="['soft-inline', (obj.Type == 'light' ? 'soft-light glossy' : ''), comFnGetClass(obj)]" :style="[comFnChkInput(obj.Keyin) ? { 'cursor' : 'pointer' } : '']" @click="comFnChkInput(obj.Keyin) && !['button', 'button2', 'image2'].includes(obj.Type) ? comFnKeyinData(obj) : ''">
			<div v-if="obj.Soft" :title="obj.Desc">
				<i v-if="icon || obj.Icon" class="soft-icon material-icons">{{icon || obj.Icon}}</i>
				<span v-if="obj.Type == 'data'" class="soft-text" :data-type="obj.Type" :data-desc="obj.Desc" :data-value="softValues[obj.Soft].value">{{softValues[obj.Soft].value}}</span>
				<span v-else-if="obj.Type == 'text'" class="soft-text" :data-type="obj.Type" :data-desc="obj.Desc" :data-value="obj.Icon[softValues[obj.Soft].value]">{{obj.Icon[softValues[obj.Soft].value]}}</span>
				<span v-if="obj.Unit" class="soft-unit">{{obj.Unit}}</span>
			</div>
		</div>
	</div>
</template>

<script>
module.exports = {
	props: {
		"dataArray": {
			default: [],
			required: true
		},
		"softValues": {
			default: {},
			required: true
		},
		"icon": {
			default: "",
		},
		"dataModified": {
			default: true
		}
	},
	data: () => {
		return {
			onoffObj: { "on": "開", "off": "關" },
			onoffArr: ["off", "on"]
		}
	},
	methods: {
		comFnChkInput (keyin) {
			return keyin == "Yes" && this.dataModified == true ? true : false
		},
		comFnGetClass (obj) {
			if (typeof obj.Class == "string" && obj.Class.length > 0) {
				return obj.Class;
			} else if (typeof obj.Class == "object") {
				if (obj.Class.length > 1) {
					return obj.Class[this.softValues[obj.Soft].value]
				}
				return obj.Class[0]
			}
			return "";
		},
		comFnGifferPlay (_soft, _softs) {
			if (typeof _softs != "string") {
				_softs = ""
			}
			var self = this
			var _play = false
			Array.union([_soft], _softs.split(";")).forEach(function(_point){
				if (self.softValues[_point].value == 1) {
					_play = true
				}
			})
			return _play;
		},
		async comFnKeyinData (obj, _soft="") {
			// 檢查是否能輸入
			if (!this.comFnChkInput(obj.Keyin)) {
				return false
			}
			try {
				var pt = obj.Soft.substr(0, 1)
				var upd_val = false
				var updObj = {}
				updObj[(_soft.length > 2 ? _soft : obj.Soft)] = { decimal: obj.Decimal }
				
				if (obj.Type == "button") {
					var pv = Number(this.softValues[obj.Soft].value)
					var pn = (pv + 1) % 2
					
					await this.$q.dialog({
						title: this.onoffObj[obj.Icon[pv]] + " 切換至 " + this.onoffObj[obj.Icon[pn]],
						message: obj.Soft + "：" + obj.Icon[pv] + " => " + obj.Icon[pn],
						ok: "確定",
						cancel: "取消",
						preventClose: true
					}).then(function(){ 
						upd_val = pn
					})
				} else if (obj.Type == "text") {
					var pv = Number(this.softValues[obj.Soft].value)
					var ra = obj.Icon.map(function(txt, val){ 
						return { label: txt, value: val }
					})
					
					await this.$q.dialog({
						title: "請選擇 (" + (obj.Desc.length ? obj.Desc : (_soft.length > 2 ? _soft : obj.Soft) ) + ")",
						options: {
							type: "radio",
							model: pv,
							items: ra
						},
						ok: "確定",
						cancel: "取消",
						preventClose: true
					}).then(function(pn){
						upd_val = pn
					})
				} else if (obj.Type == "data") {
					var err = ""
					await this.$q.dialog({
						title: "請輸入 (" + (obj.Desc.length ? obj.Desc : (_soft.length > 2 ? _soft : obj.Soft) ) + ")",
						message: (pt == "D" ? "限定數字" : (pt == "R" ? "限定 0 or 1" : "")),
						prompt: {
							model: "",
							type: "text"
						},
						ok: "確定",
						cancel: "取消",
						preventClose: true
					}).then(function(pn){
						if (pt == "D" && isNaN(Number(pn))) {
							err = "只接受數字"
						} else if (pt == "R" && ![0, 1].includes(Number(pn))) {
							err = "只接受 0 和 1"
						} else {
							upd_val = pn
						}
					});
					
					if (err != "") {
						await this.$q.dialog({
							title: "輸入型態錯誤 (" + (obj.Desc.length ? obj.Desc : (_soft.length > 2 ? _soft : obj.Soft) ) + ")",
							message: err,
							ok: "確定",
							preventClose: true
						});
						return false
					}
				} else if (pt == "R") {
					var pv = Number(this.softValues[obj.Soft].value)
					var ra = [{ label: "OFF", value: 0 }, { label: "ON", value: 1 }]
					
					await this.$q.dialog({
						title: "請選擇 (" + (obj.Desc.length ? obj.Desc : (_soft.length > 2 ? _soft : obj.Soft) ) + ")",
						options: {
							type: "radio",
							model: pv,
							items: ra
						},
						ok: "確定",
						cancel: "取消",
						preventClose: true
					}).then(function(pn){
						upd_val = pn
					})
				}
				
				if (upd_val !== false) {
					updObj[(_soft.length > 2 ? _soft : obj.Soft)]["value"] = (pt == "R" ? this.onoffArr[upd_val] : upd_val)
					if (obj.Source == "MQTT") {
						this.$emit("mqtt_modify", updObj);
						return;
					}
					this.$emit("soft_modify", updObj);
				}
			} catch (err) {
				return false
			}
		}
	}
}
</script>