<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="220"
				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" />
	</div>
</template>

<script>
import AddOrUpdate from './add-or-updata';
import basicPage from '../../core/mixins/basic-page';
import { parseTime } from '../../core/mixins/code-filter';
import {
	getMaterialPage,
	deleteMaterial
} from '@/api/base/material';
import { listData } from "@/api/system/dict/data";
import { publicFormatter } from '@/utils/dict';

const tableProps = [
	{
		prop: 'createTime',
		label: '添加时间',
		filter: parseTime
	},
	{
		prop: 'code',
		label: '物料编码'
	},
	{
		prop: 'name',
		label: '物料名称'
	},
	{
		prop: 'engName',
		label: '英文名称'
	},
	{
		prop: 'type',
		label: '物料类型',
		filter: publicFormatter('material_type')
	},
	{
		prop: 'supplierName',
		label: '供应商'
	},
	{
		prop: 'remark',
		label: '备注'
	}
];

export default {
	mixins: [basicPage],
	data() {
		return {
			urlOptions: {
				getDataListURL: getMaterialPage,
				deleteURL: deleteMaterial
				// exportURL: exportFactoryExcel,
			},
			tableProps,
			tableBtn: [
				this.$auth.hasPermi(`base:material:update`)
					? {
							type: 'edit',
							btnName: '编辑',
					  }
					: undefined,
					this.$auth.hasPermi(`base:material:queryAttr`)
					? {
							type: 'search',
							btnName: '查看属性',
					  }
					: undefined,
					this.$auth.hasPermi(`base:material:editAttr`)
					? {
							type: 'editAttr',
							btnName: '编辑属性',
					  }
					: undefined,
          this.$auth.hasPermi(`base:material:delete`)
					? {
							type: 'delete',
							btnName: '删除',
					  }
					: undefined,
			].filter((v)=>v),
			tableData: [],
			typeList: [],
			formConfig: [
				{
					type: 'input',
					label: '物料名称',
					placeholder: '物料名称',
					param: 'name',
				},
				{
					type: 'input',
					label: '物料编码',
					placeholder: '物料编码',
					param: 'code',
				},
				{
					type: 'button',
					btnName: '查询',
					name: 'search',
					color: 'primary',
				},
				{
					type: 'separate',
				},
				{
					type: this.$auth.hasPermi('base:material:create') ? 'button' : '',
					btnName: '新增物料',
					name: 'add',
					color: 'success',
					plain: true
				},
			],
		};
	},
	components: {
		AddOrUpdate,
	},
	created() {
		this.getDict()
	},
	methods: {
		async getDict() {
			// 物料类型列表
			const res = await listData({
				pageNo: 1,
        pageSize: 99,
        dictType: 'material_type',
			});
			this.typeList = res.data.list.map(item => {
				return {
					label: item.label,
					value: Number(item.value)
				}
			});
		},
		otherMethods(val) {
			if (val.type === 'search') {
				this.addOrUpdateVisible = true;
				this.addOrEditTitle = '详情';
				this.$nextTick(() => {
					this.$refs.addOrUpdate.init(val.data.id, true);
				});
			} else {
				this.addOrEditTitle = '编辑';
				this.addOrUpdateVisible = true;
				this.$nextTick(() => {
					this.$refs.addOrUpdate.init(val.data.id);
				});
			}
		},
		// 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      this.urlOptions.getDataListURL(this.listQuery).then(response => {
        this.tableData = response.data.records.map(item => {
					this.typeList.filter(t => {
						if (item.type === t.value) {
							item.materialType = t.label
						}
					})
					return item
				});
        this.listQuery.total = response.data.total;
        this.dataListLoading = false;
      });
    },
		buttonClick(val) {
			switch (val.btnName) {
				case 'search':
					this.listQuery.pageNo = 1;
					this.listQuery.pageSize = 10;
					this.listQuery.name = val.name ? val.name : 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);
			}
		},
		// 删除
    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(() => { });
    }
	},
};
</script>
