<template>
	<div class="app-container">
		<search-bar
			:formConfigs="formConfig"
			ref="searchBarForm"
			@headBtnClick="buttonClick" />
		<base-table
			v-loading="dataListLoading"
			:table-props="tableProps"
			:page="listQuery.pageNo"
			:limit="listQuery.pageSize"
			:max-height="tableH"
			:table-data="tableData">
			<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="getDataList" />
		<base-dialog
			:dialogTitle="addOrEditTitle"
			:dialogVisible="addOrUpdateVisible"
			@cancel="handleCancel"
			@confirm="handleConfirm"
			:before-close="handleCancel"
			width="60%">
			<add-or-update
				ref="addOrUpdate"
				@refreshDataList="successSubmit"></add-or-update>
		</base-dialog>
	</div>
</template>

<script>
import AddOrUpdate from './add-or-updata';
import basicPage from '@/mixins/basic-page';
import { parseTime } from '../mixins/code-filter';
import {
	getMaterialLogPage,
	deleteMaterialLog
} from '@/api/base/materialUseLog';
import tableHeightMixin from '@/mixins/tableHeightMixin';

const tableProps = [
	{
		prop: 'materialName',
		label: '物料名称'
	},
	{
		prop: 'materialCode',
		label: '物料编码',
		width: 160,
	},
	{
		prop: 'materialDateName',
		label: '物料批次'
	},
	{
		prop: 'equipName',
		label: '使用设备'
	},
	{
		prop: 'num',
		label: '使用数量'
	},
	{
		prop: 'useTime',
		label: '使用时间',
		filter: parseTime,
		width: 160,
	},
	{
		prop: 'userName',
		label: '操作人',
		showOverflowtooltip: true,
	},
	{
		prop: 'source',
		label: '数据来源',
		filter: (val) => ['未知', '手动', '自动'][val],
		width: 80,
	},
	{
		prop: 'remark',
		label: '备注'
	}
];

export default {
	mixins: [basicPage, tableHeightMixin],
	data() {
		return {
			urlOptions: {
				getDataListURL: getMaterialLogPage,
				deleteURL: deleteMaterialLog,
				// exportURL: exportFactoryExcel,
			},
			tableProps,
			tableBtn: [
				this.$auth.hasPermi(`base:material-use-log:update`)
					? {
							type: 'edit',
							btnName: '编辑',
					  }
					: undefined,
          this.$auth.hasPermi(`base:material-use-log:delete`)
					? {
							type: 'delete',
							btnName: '删除',
					  }
					: undefined,
			].filter((v)=>v),
			tableData: [],
			listQuery: {
        pageSize: 20,
        pageNo: 1,
        total: 1,
				materialName: undefined,
				useTime: []
      },
			formConfig: [
				{
					type: 'input',
					label: '物料名称',
					placeholder: '物料名称',
					param: 'name',
				},
				{
          type: 'datePicker',
          label: '使用时间段',
          dateType: 'datetimerange',
          format: 'yyyy-MM-dd HH:mm:ss',
          valueFormat: 'yyyy-MM-dd HH:mm:ss',
          rangeSeparator: '-',
          startPlaceholder: '开始时间',
          endPlaceholder: '结束时间',
          width: 350,
          param: 'time',
        },
				{
					type: 'button',
					btnName: '查询',
					name: 'search',
					color: 'primary'
				},
				{
					type: 'separate',
				},
				{
					type: this.$auth.hasPermi('base:material-use-log:create') ? 'button' : '',
					btnName: '新增',
					name: 'add',
					color: 'success',
					plain: true
				},
			],
		};
	},
	components: {
		AddOrUpdate,
	},
	created() {},
	methods: {
		// 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      this.urlOptions.getDataListURL(this.listQuery).then(response => {
        this.tableData = response.data.records;
        this.listQuery.total = response.data.total;
        this.dataListLoading = false;
      });
    },
		buttonClick(val) {
			switch (val.btnName) {
				case 'search':
					this.listQuery.pageNo = 1;
					this.listQuery.pageSize = 20;
					this.listQuery.materialName = val.name ? val.name : undefined;
					if (val.time) {
						this.listQuery.useTime[0] = val.time[0]
						this.listQuery.useTime[1] = val.time[1]
					} else {
						this.listQuery.useTime = []
					}
					this.getDataList();
					break;
				case 'reset':
					this.$refs.searchBarForm.resetForm();
					this.listQuery = {
						pageSize: 20,
						pageNo: 1,
						total: 1,
					};
					this.getDataList();
					break;
				case 'add':
					this.addOrEditTitle = '新增';
					this.addOrUpdateVisible = true;
					this.addOrUpdateHandle();
					break;
				case 'export':
					this.handleExport();
					break;
				default:
					console.log(val);
			}
		},
    setDelete(data){
      this.deleteHandle(data.id,data.materialName)
    }
	},
};
</script>
