<template>
	<div class="container" id="app">
		<div class="row">
			<nav class="navbar navbar-expand-lg navbar-light">
				<a class="navbar-brand" href="#">首页</a>
			</nav>
		</div>
		<div class="row align-items-start mx-auto">
			<form class="form-horizontal" role="form">
				<div class="alert alert-success">输入4张扑克牌的点数：如2 3 5 8</div>
				<div class="form-group row">
					<label class="col-5">4张扑克牌</label>
					<div class="col">
						<input
							type="text"
							v-model="cards"
							class="form-control"
							id="cards"
							value=""
						/>
					</div>
				</div>
				<div class="form-group row">
					<label class="col-5">结果</label>
					<div class="col">
						<input type="text" v-model="result" class="form-control" />
					</div>
				</div>
			</form>
		</div>
	</div>
</template>

        <script>
	export default {
		data() {
			return {
				show: "get",
				cards: "",
				result: "",
			};
		},

		watch: {
			cards(newVal, oldVal) {
				var stripped = newVal.trim();
				var array = stripped.split(" ");
				var args = [];
				if (array.length == 6) {
					for (var i = 0; i < 6; i++) {
						var j = parseInt(array[i]);
						if (isNaN(j)) {
							return;
						}
						args[i] = j;
					}
					var ret = this.find(args, 24);
					if (ret.length > 0) {
						this.result = ret + "=24";
					} else {
						this.result = "没有找到";
					}
				} else {
					console.log("len: " + array.length);
				}
			},
		},
		methods: {
			calc() {
				console.log("start calculate");
				console.log(find([8, 2, 3, 5], 24));
			},
            find3(a, b, target) {
				if (a + b === target) {
					return a + "+" + b;
				}  else if (a - b == target) {
					return a + "-" + b;
				} else if (b - a == target) {
					return b + "-" + a;
				}
				return "";
			},
			find2(a, b, target) {
				if (a * b === target) {
					return a + "*" + b;
				} else if (a + b === target) {
					return a + "+" + b;
				} else if (a % b === 0 || b % a === 0) {
					if (a / b === target) {
						return a + "/" + b;
					}
					if (b / a === target) {
						return b + "/" + a;
					}
				} else if (a - b == target) {
					return a + "-" + b;
				} else if (b - a == target) {
					return b + "-" + a;
				}
				return "";
			},

			remove(array, index) {
				var out = [];
				var j = 0;
				for (var i = 0; i < array.length; i++) {
					if (i == index) {
						continue;
					}
					out[j] = array[i];
					j++;
				}
				return out;
			},

			find(array, target) {
                	// js 中必须用var声明，限制作用域。
				var out = "";
				var ret = "";
				var i = 0;
				var left;

				if (array.length == 2) {
					out = this.find3(array[0], array[1], target);
					if (out.length > 0) {
						return "(" + out + ")";
					}
					return "";
				}

			

				var x;
				for (i = 0; i < array.length; i++) {
					x = array[i];
					left = this.remove(array, i);
					console.log(left);

					if (target != 0 && (target % x == 0 || x % target == 0)) {
						if (target > x) {
							ret = find(left, target / x);
							if (ret.length > 0) {
								out = x + "*" + ret;
								break;
							}
						} else {
							ret = find(left, x / target);
							if (ret.length > 0) {
								out = x + "/" + ret;
								break;
							}
						}
					}

					if (target > x) {
						ret = this.find(left, target - x);
						if (ret.length > 0) {
							out = x + "+" + ret;
							break;
						}
					} else {
						ret = this.find(left, x - target);
						if (ret.length > 0) {
							out = x + "-" + ret;
							break;
						}
					}

					ret = this.find(left, x * target);
					if (ret.length > 0) {
						out = ret + "/" + x;
						break;
					}
					ret = this.find(left, target + x);
					if (ret.length > 0) {
						out = ret + "-" + x;
						break;
					}
				}

				if (out.length > 0) {
					if (array.length == 4) {
						return out;
					} else {
						return "(" + out + ")";
					}
				}
				return "";
			},
		},
	};
</script>
