<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="50%">
			<add-or-update
				ref="addOrUpdate"
				:name-arr="formConfig[0].selectOptions"
				@refreshDataList="successSubmit"></add-or-update>
		</base-dialog>
	</div>
</template>

<script>
import AddOrUpdate from './add-or-updata';
import basicPage from '@/mixins/basic-page';
import tableHeightMixin from '@/mixins/lb/tableHeightMixin';
import { parseTime } from '@/filter/code-filter';
import {
	deleteRawOthercostLog,
	getRawOthercostLogPage,
	exportRawOthercostLogExcel,
} from '@/api/cost/costOthercostLogDeep';
import { getRawOthercostRulePage } from '@/api/cost/deepOthercostRule';

const tableProps = [
	{
		prop: 'otherCostName',
		label: '成本名称',
	},
	{
		prop: 'recTime',
		label: '日期',
		filter: (val) => parseTime(val, '{y}年{m}月{d}日'),
	},
	{
		prop: 'price',
		label: '成本金额',
		align: 'right',
    filter: (val) => (val != null ? Number(val).toFixed(2) : '-'),
	},
	{
		prop: 'remark',
		label: '备注',
	},
];

export default {
	mixins: [basicPage, tableHeightMixin],
	data() {
		return {
			urlOptions: {
				getDataListURL: getRawOthercostLogPage,
				deleteURL: deleteRawOthercostLog,
				exportURL: exportRawOthercostLogExcel,
			},
			tableProps,
			tableBtn: [{
							type: 'edit',
							btnName: '编辑',
					  },{
							type: 'delete',
							btnName: '删除',
					  }
			].filter((v) => v),
			tableData: [],
			formConfig: [
				{
					type: 'select',
					label: '成本名称',
					selectOptions: [],
					param: 'name',
					labelField: 'label',
					valueField: 'name',
					filterable: true,
				},
				{
					type: 'datePicker',
					label: '时间范围',
					dateType: 'daterange',
					format: 'yyyy-MM-dd',
					valueFormat: 'yyyy-MM-dd HH:mm:ss',
					rangeSeparator: '-',
					startPlaceholder: '开始时间',
					endPlaceholder: '结束时间',
					param: 'searchTime',
				},
				{
					type: 'button',
					btnName: '搜索',
					name: 'search',
					color: 'primary',
				},
				{
					type: this.$auth.hasPermi('monitoring:cost-deep-othercost-log:create')
						? 'separate'
						: '',
				},
				{
					type: this.$auth.hasPermi('monitoring:cost-deep-othercost-log:create')
						? 'button'
						: '',
					btnName: '新增',
					name: 'add',
					color: 'success',
					plain: true,
				},
				// {
				// 	type: this.$auth.hasPermi('monitoring:cost-othercost-log:create') ? 'separate' : '',
				// },
				{
					type: 'button',
					btnName: '导出',
					name: 'export',
					color: 'warning',
				},
			],
		};
	},
	components: {
		AddOrUpdate,
	},
	created() {
		const params = {
			pageNo: 1,
			pageSize: 100,
		};
		getRawOthercostRulePage(params).then((response) => {
			this.formConfig[0].selectOptions = response.data.list;
		});
	},
	methods: {
		buttonClick(val) {
			switch (val.btnName) {
				case 'search':
					this.listQuery.pageNo = 1;
					this.listQuery.pageSize = 20;
					this.listQuery.name = val.name||null;
					this.listQuery.startTime = val.searchTime ? val.searchTime[0] : null;
					this.listQuery.endTime = val.searchTime
						? val.searchTime[1].substr(0, 10) + ' 23:59:59'
						: null;
					this.getDataList();
					break;
				case 'add':
					this.addOrUpdateHandle();
					break;
				case 'export':
					this.listQuery.pageNo = 1;
					this.listQuery.pageSize = 20;
					this.listQuery.name = val.name||null;
					this.listQuery.startTime = val.searchTime ? val.searchTime[0] : null;
					this.listQuery.endTime = val.searchTime
						? val.searchTime[1].substr(0, 10) + ' 23:59:59'
						: null;
					this.handleExport();
					break;
				default:
					console.log(val);
			}
		},
    //tableBtn点击
    handleClick(val) {
      if (val.type === "edit") {
        this.addOrUpdateVisible = true;
        this.addOrEditTitle = "编辑";
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(val.data.id);
        });
      } else if (val.type === "delete") {
        this.deleteHandle(val.data.id, val.data.otherCostName, val.data._pageIndex)
      } else {
        this.otherMethods(val)
      }
    },
		/** 导出按钮操作 */
		handleExport() {
			let title;
      title = '深加工其它成本-成本记录';
			// 处理查询参数
			let params = { ...this.listQuery };
			params.pageNo = undefined;
			params.pageSize = undefined;
			this.$modal
				.confirm('是否确认导出所有数据项?')
				.then(() => {
					return this.urlOptions.exportURL(params);
				})
				.then((response) => {
					this.$download.excel(response, title + '报表.xls');
				})
				.catch(() => {});
		},
	},
};
</script>
