<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"
			:table-data="tableData">
			<method-btn
				v-if="tableBtn.length"
				slot="handleBtn"
				:width="150"
				label="操作"
				:method-list="tableBtn"
				@clickBtn="handleClick" />
		</base-table>
		<pagination
			:limit.sync="listQuery.pageSize"
			:page.sync="listQuery.pageNo"
			:total="listQuery.total"
			@pagination="getDataList" />
		<!-- <add-or-update
			v-if="addOrUpdateVisible"
			ref="addOrUpdate"
			@refreshDataList="getDataList" /> -->
    <base-dialog
			:dialogTitle="addOrEditTitle"
			:dialogVisible="addOrUpdateVisible"
			@cancel="handleCancel"
			@confirm="handleConfirm"
			:before-close="handleCancel"
			width="55%">
			<add-or-update
				ref="addOrUpdate"
				@refreshDataList="successSubmit"></add-or-update>
		</base-dialog>
		<add-content
			v-if="addContent"
			ref="addContent"
			@refreshDataList="addContent = false" />
	</div>
</template>

<script>
import AddOrUpdate from './add-or-updata';
import AddContent from './addContent';
import basicPage from '../../../../core/mixins/basic-page';
import { parseTime, toDay } from '../../../../core/mixins/code-filter';
import { publicFormatter } from '@/utils/dict';
import { getPlanPage } from '@/api/equipment/base/maintain/planconfig';
import { deleteEqMaintainPlan } from '@/api/equipment/base/maintain/record';

const tableProps = [
	// {
	// 	prop: 'createTime',
	// 	label: '添加时间',
	// 	filter: parseTime
	// },
	{
		prop: 'name',
		label: '保养计划名称'
	},
	{
		prop: 'departmentName',
		label: '部门'
	},
  {
		prop: 'lineName',
		label: '产线名'
	},
  {
		prop: 'maintenancePeriod',
		label: '保养频率(天/次)'
	},
  {
		prop: 'firstMaintenanceTime',
		label: '首次保养时间',
		filter: parseTime
	},
  {
		prop: 'maintainDuration',
		label: '保养时长'
	},
  {
		prop: 'maintainer',
		label: '计划保养人员'
	},
  {
		prop: 'confirmTimeLimit',
		label: '确认时限',
		filter: toDay
	},
	{
		prop: 'remark',
		label: '备注'
	}
];

export default {
	mixins: [basicPage],
	data() {
		return {
			urlOptions: {
				getDataListURL: getPlanPage,
				deleteURL: deleteEqMaintainPlan
			},
			tableProps,
			addContent: false,
			listQuery: {
        pageSize: 10,
        pageNo: 1,
        total: 0,
				special: false,
				planName: undefined
      },
			tableBtn: [
				this.$auth.hasPermi(`equipment:plan-config:add`)
					? {
							type: 'add',
							btnName: '添加内容',
					  }
					: undefined,
				this.$auth.hasPermi(`equipment:plan-config:update`)
					? {
							type: 'edit',
							btnName: '编辑',
					  }
					: undefined,
          this.$auth.hasPermi(`equipment:plan-config:delete`)
					? {
							type: 'delete',
							btnName: '删除',
					  }
					: undefined,
			].filter((v)=>v),
			tableData: [],
			formConfig: [
				{
					type: 'input',
					label: '保养计划名称',
					placeholder: '保养计划名称',
					param: 'planName'
				},
				{
					type: 'button',
					btnName: '查询',
					name: 'search',
					color: 'primary',
				},
				{
					type: 'separate',
				},
				{
					type: this.$auth.hasPermi('equipment:plan-config:create') ? 'button' : '',
					btnName: '新增',
					name: 'add',
					color: 'success',
					plain: true
				},
			],
		};
	},
	components: {
		AddOrUpdate,
		AddContent
	},
	created() {},
	methods: {
		// 删除
    deleteHandle(id, name, index) {
      this.$confirm(`是否确认删除产品名称为"${name}"的数据项`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.urlOptions.deleteURL(id).then(({ data }) => {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.getDataList();
              },
            });
          });
        })
        .catch(() => { });
    },
		// 查看详情
		otherMethods(val) {
			if (val.type === 'add') {
				this.addContent = true;
				// this.addOrEditTitle = '详情';
				this.$nextTick(() => {
					this.$refs.addContent.init(val.data.id);
				});
			}
		},
		buttonClick(val) {
			switch (val.btnName) {
				case 'search':
					// this.listQuery.pageNo = 1;
					// this.listQuery.pageSize = 10;
					this.listQuery.planName = val.planName ? val.planName : undefined;
					this.listQuery.code = val.code ? val.code : undefined;
					this.getDataList();
					break;
				case 'reset':
					this.$refs.searchBarForm.resetForm();
					this.listQuery = {
						pageSize: 10,
						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);
			}
		},
	},
};
</script>
