<template>
	<div class="app-container">
		<search-bar
			:formConfigs="formConfig"
			ref="searchBarForm"
			@headBtnClick="buttonClick" />
		<base-table
			:table-props="tableProps"
			:page="listQuery.pageNo"
			:limit="listQuery.pageSize"
			:table-data="list"
			:max-height="tableH">
			<method-btn
				v-if="tableBtn.length"
				slot="handleBtn"
				:width="120"
				label="操作"
				:method-list="tableBtn"
				@clickBtn="handleClick" />
		</base-table>
		<pagination
			:limit.sync="listQuery.pageSize"
			:page.sync="listQuery.pageNo"
			:total="listQuery.total"
			@pagination="getList" />
	</div>
</template>

<script>
import { parseTime } from '../mixins/code-filter';
import {
	getProcessTraceabilityPage,
	getWorkOrderList,
	getCoreProduct,
} from '@/api/quality/processTraceability';
import { publicFormatter } from '@/utils/dict';
import tableHeightMixin from '@/mixins/tableHeightMixin';
const tableProps = [
	{
		prop: 'name',
		label: '工单名称',
		showOverflowtooltip: true,
		minWidth: 120,
	},
	{
		prop: 'code',
		label: '工单编码',
		showOverflowtooltip: true,
		minWidth: 120,
	},
	{
		prop: 'planProductName',
		label: '产品名',
		showOverflowtooltip: true,
	},
	{
		prop: 'status',
		label: '状态',
		filter: (val) =>
			val == 1
				? '等待'
				: val == 2
				? '激活'
				: val == 3
				? '暂停'
				: val == 4
				? '完成'
				: '作废',
	},
	{
		prop: 'startProduceTime',
		label: '开始生产时间',
		filter: parseTime,
		width: 160,
	},
	{
		prop: 'finishProduceTime',
		label: '结束生产时间',
		filter: parseTime,
		width: 160,
	},
	{
		prop: 'actualQuantity',
		label: '生产数量',
	},
	{
		prop: 'planProductUnit',
		label: '单位',
		filter: publicFormatter('unit_dict'),
	},
	{
		prop: 'yield',
		label: '合格率',
		filter: (val) => (val ? val.toFixed(2) + '%' : '-'),
		align: 'center',
	},
	{
		prop: 'processFlowName',
		label: '工艺名称',
		showOverflowtooltip: true,
	},
];

export default {
	mixins: [tableHeightMixin],
	data() {
		return {
			tableProps,
			tableBtn: [
				{
					type: 'processDetail',
					btnName: '查看工艺详情',
				},
			].filter((v) => v),
			list: [],
			listQuery: {
				pageSize: 10,
				pageNo: 1,
			},
			formConfig: [
				{
					type: 'select',
					label: '工单名称',
					selectOptions: [],
					labelField: 'name',
					valueField: 'id',
					param: 'orderName',
					filterable: true,
				},
				{
					type: 'datePicker',
					label: '工单开始时间',
					dateType: 'daterange',
					format: 'yyyy-MM-dd',
					valueFormat: 'yyyy-MM-dd',
					rangeSeparator: '-',
					startPlaceholder: '开始时间',
					endPlaceholder: '结束时间',
					param: 'timeVal',
					defaultSelect: [],
					width: 250,
				},
				{
					type: 'button',
					btnName: '查询',
					name: 'search',
					color: 'primary',
				},
			],
		};
	},
	created() {
		this.getList();
		this.getDict();
	},
	methods: {
		getList() {
			getProcessTraceabilityPage(this.listQuery).then((res) => {
				this.list = res.data.list || [];
				this.listQuery.total = res.data.total || 0;
			});
		},
		handleClick(val) {
			if (val.type === 'processDetail') {
				console.log(val);
				let specificationsData = '';
				getCoreProduct(val.data.planProductId).then((res) => {
					console.log(res);

					this.$router.push({
						path: 'process-traceability-detail',
						query: {
							id: val.data.processFlowId,
							orderId: val.data.id,
							name: val.data.name,
							specifications: res.data.specifications,
							productName: val.data.planProductName,
							processFlowName: val.data.processFlowName,
						},
					});
				});
			}
		},
		getDict() {
			getWorkOrderList().then((response) => {
				this.formConfig[0].selectOptions = response.data.map((item) => {
					return {
						name: item.name,
						id: item.name,
					};
				});
				console.log(this.formConfig[0].selectOptions);
			});
		},
		buttonClick(val) {
			console.log(val);
			if (val.btnName === 'search') {
				this.listQuery.orderName = val.orderName ? val.orderName : undefined;
				if (val.timeVal && val.timeVal.length != 0) {
					this.listQuery.startTime = val.timeVal[0] + ' 00:00:00';
					this.listQuery.endTime = val.timeVal[1] + ' 23:59:59';
				} else {
					this.listQuery.startTime = undefined;
					this.listQuery.endTime = undefined;
				}
				this.getList();
			}
		},
		otherMethods(val) {
			this.addOrUpdateVisible = true;
			this.addOrEditTitle = '详情';
			this.$nextTick(() => {
				this.$refs.addOrUpdate.init(val.data.id, true);
			});
		},
	},
};
</script>
