<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="120" 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" />
    <!-- <detail-or-update v-if="detailOrUpdateVisible" ref="detailOrUpdate" @refreshDataList="successSubmit" /> -->
  </div>
</template>

<script>
import AddOrUpdate from './add-or-updata';
// import DetailOrUpdate from './detail-or-updata';

import basicPage from '../../core/mixins/basic-page';
import StatusBtn from './statusBtn.vue'
import {
  processEquValueBomPage,
  deleteProcessEquValueBom,
  getEquipmentList
} from '@/api/extend/processEquValueBom';

const tableProps = [
	{
    prop: 'equipmentCode',
		label: '设备编码',
  },
  {
    prop: 'equipmentName',
    label: '设备名称'
  },
  {
    prop: 'code',
    label: '参数配方编码'
  },
	{
    prop: 'name',
    label: '参数配方名称'
	},
	{
    prop: 'enabled',
    label: '当前激活状态',
    subcomponent: StatusBtn
	},
	{
    prop: 'remark',
		label: '备注'
	},
];

export default {
	mixins: [basicPage],
	data() {
		return {
			urlOptions: {
        getDataListURL: processEquValueBomPage,
        deleteURL: deleteProcessEquValueBom,
				// exportURL: exportFactoryExcel,
			},
			tableProps,
			tableBtn: [
        this.$auth.hasPermi(`extend:process-equ-value-bom:update`)
					? {
							type: 'edit',
							btnName: '编辑',
					  }
          : undefined,
        this.$auth.hasPermi(`extend:process-equ-value-bom:detail`)
          ? {
            type: 'detail',
            btnName: '详情',
          }
          : undefined,
        this.$auth.hasPermi(`extend:process-equ-value-bom:delete`)
					? {
							type: 'delete',
							btnName: '删除',
					  }
					: undefined,
			].filter((v)=>v),
      tableData: [],
      detailOrUpdateVisible:false,
			formConfig: [
        {
          type: 'select',
          label: '设备名称',
          selectOptions: [],
          labelField: 'name',
          valueField: 'id',
          param: 'equipmentId',
          filterable: true
          // filter
        },
        {
          type: 'input',
          label: '参数配方名称',
          // selectOptions: [],
          // labelField: 'name',
          // valueField: 'id',
          param: 'name',
        },
        {
          type: 'input',
          label: '参数配方编码',
          // selectOptions: [],
          // labelField: 'name',
          // valueField: 'id',
          param: 'code',
        },
				{
					type: 'button',
					btnName: '查询',
					name: 'search',
					color: 'primary',
				},
				{
					type: 'separate',
				},
				{
          type: this.$auth.hasPermi('extend:process-equ-value-bom:create') ? 'button' : '',
					btnName: '新增',
					name: 'add',
					color: 'success',
					plain: true
				},
			],
		};
	},
	components: {
    AddOrUpdate,
    // DetailOrUpdate
	},
  created() {
    this.getDict()
  },
  methods: {
    otherMethods(val) {
      if (val.type === 'detail') {
        this.addOrUpdateVisible = true;
        this.addOrEditTitle = "详情";
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(val.data.id, true);
        });
      }
    },
    getDict() {
      getEquipmentList().then((res) => {
        // console.log(res);
        // console.log(response);
        this.formConfig[0].selectOptions = res.data.map((item) => {
          return {
            name: item.name,
            id: item.id
          }
        })
      })
    },
    deleteHandle(id, name, index) {
      this.$confirm(`是否确认删除设备参数bom${ name}`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.urlOptions.deleteURL(id).then(({ data }) => {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.getDataList();
              },
            });
          });
        })
        .catch(() => { });
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      this.urlOptions.getDataListURL(this.listQuery).then(response => {
        this.tableData = response.data.list;
        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.equipmentId = val.equipmentId ? val.equipmentId : undefined;
          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);
			}
		},
	},
};
</script>
