<template>
  <div class="JNPF-common-layout machine_data">
    <div class="JNPF-common-layout-center">
      <el-row class="JNPF-common-search-box" :gutter="14">
        <el-form @submit.native.prevent>
<!--          <el-col :span="6">-->
<!--            <el-form-item label="机台编码">-->
<!--              <el-input v-model="query.machineCd" placeholder="请输入" clearable></el-input>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--          <el-col :span="6">-->
<!--            <el-form-item label="机台名称">-->
<!--              <el-input v-model="query.machineName" placeholder="请输入" clearable></el-input>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--          <el-col :span="6">-->
<!--            <el-form-item label="所属产线">-->
<!--              <el-select v-model="query.belgLineId" placeholder="请选择" clearable>-->
<!--                <el-option-->
<!--                  v-for="item in proLineList"-->
<!--                  :key="item.id"-->
<!--                  :label="item.proLineName"-->
<!--                  :value="item.id">-->
<!--                </el-option>-->
<!--              </el-select>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--          <el-col :span="6">-->
<!--            <el-form-item label="所属工序">-->
<!--              <el-select v-model="query.belgProcId" placeholder="请选择" clearable>-->
<!--                <el-option-->
<!--                  v-for="item in procList"-->
<!--                  :key="item.id"-->
<!--                  :label="item.procName"-->
<!--                  :value="item.id">-->
<!--                </el-option>-->
<!--              </el-select>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
          <el-col :span="6">
            <el-form-item label="状态">
              <JnpfSelect v-model="query.enabledStatus" placeholder="请选择" clearable :options="enabledStatusOptions"
                          :props="enabledStatusProps">
              </JnpfSelect>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item class="btn">
              <el-button type="primary" icon="el-icon-search" @click="search()">查询</el-button>
              <el-button icon="el-icon-refresh-right" @click="reset()">重置</el-button>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div class="JNPF-common-layout-main JNPF-flex-main">
        <div class="JNPF-common-head">
          <div>
            <el-button type="primary" icon="icon-ym icon-ym-btn-add" @click="addOrUpdateHandle()">新建</el-button>
          </div>
          <div class="JNPF-common-head-right"></div>
        </div>
        <JNPF-table v-loading="listLoading" :data="list">
          <el-table-column prop="machineCd" label="机台编码" align="center"/>
          <el-table-column prop="machineName" label="机台名称" align="center"/>
          <el-table-column prop="belgLineName" label="所属产线" align="center"/>
          <el-table-column prop="belgProcName" label="所属工序" align="center"/>
          <el-table-column prop="enabledStatus" label="状态" align="center">
            <template slot-scope="scope">
              <el-tag :type="scope.row.enabledStatus == 1 ? 'success' : 'info'" size="small">
                {{ scope.row.enabledStatus == 1 ? '启用' : scope.row.enabledStatus == 2 ? '未启用' : scope.row.enabledStatus }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="remark" label="备注" align="center"/>
          <el-table-column prop="creatorTime" label="创建时间" align="center" :formatter="jnpf.tableDateFormat1"/>
          <el-table-column label="操作" fixed="right" align="center" width="100">
            <template slot-scope="scope">
              <el-button type="text" @click="addOrUpdateHandle(scope.row)">编辑</el-button>
            </template>
          </el-table-column>
        </JNPF-table>
        <pagination :total="total" :page.sync="listQuery.currentPage" :limit.sync="listQuery.pageSize"
          @pagination="initData" />
      </div>
    </div>
    <JNPF-Form v-if="formVisible" ref="JNPFForm" @refresh="refresh" :proLineList="proLineList" :procList="procList" />
  </div>
</template>

<script>
import request from "@/utils/request";
import { mapGetters } from "vuex";
import JNPFForm from "./form";
import jnpf from "@/utils/jnpf";

export default {
  name: "machine_list",
  components: {
    JNPFForm,
  },
  data() {
    return {
      keyword: "",
      query: {
        machineCd: undefined,
        machineName: undefined,
        belgLineId: undefined,
        belgProcId: undefined,
        enabledStatus: undefined,
      },
      defListQuery: {
        sort: "desc",
        sidx: "",
      },
      list: [],
      listLoading: false,
      multipleSelection: [],
      total: 0,
      listQuery: {
        currentPage: 1,
        pageSize: 20,
        sort: "",
        sidx: "",
      },
      formVisible: false,
      proLineList: [],
      procList: [],
      enabledStatusOptions: [
        { fullName: "启用", id: 1 },
        { fullName: "未启用", id: 2 },
      ],
      enabledStatusProps: { label: "fullName", value: "id" },
    };
  },
  computed: {
    jnpf() {
      return jnpf
    },
    ...mapGetters(["userInfo"]),
    menuId() {
      return this.$route.meta.modelId || "";
    },
  },
  created() {
    this.initData();
    this.getProLineList();
    this.getProcList();
  },
  methods: {
    initData() {
      this.listLoading = true;
      let _query = {
        ...this.listQuery,
        ...this.query,
        dataType: 0,
      };
      // 清理空值，确保传递正确的数据格式
      Object.keys(_query).forEach(key => {
        if (_query[key] === undefined || _query[key] === null || _query[key] === '') {
          delete _query[key];
        }
      });

      console.log('查询参数:', _query); // 调试日志

      request({
        url: `/api/example/machine/getList`,
        method: "post",
        data: _query,
      }).then((res) => {
        var _list = [];
        for (let i = 0; i < res.data.list.length; i++) {
          let _data = res.data.list[i];
          _list.push(_data);
        }
        this.list = _list;
        this.total = res.data.pagination.total;
        this.listLoading = false;
      });
    },
    // 获取产线下拉列表
    getProLineList() {
      request({
        url: `/api/example/machine/getProLineList`,
        method: 'get'
      }).then(res => {
        this.proLineList = res.data || [];
      });
    },
    // 获取工序列表
    getProcList() {
      request({
        url: `/api/example/machine/getProcList`,
        method: 'get'
      }).then(res => {
        this.procList = res.data || [];
      });
    },
    addOrUpdateHandle(row) {
      let id = row ? row.id : "";
      this.formVisible = true;
      this.$nextTick(() => {
        this.$refs.JNPFForm.init(id);
      });
    },
    search() {
      this.listQuery.currentPage = 1;
      this.listQuery.pageSize = 20;
      this.initData();
    },
    refresh(isrRefresh) {
      this.formVisible = false;
      if (isrRefresh) this.search();
    },
    reset() {
      this.query = {
        machineCd: undefined,
        machineName: undefined,
        belgLineId: undefined,
        belgProcId: undefined,
        enabledStatus: undefined,
      };
      this.search();
    },
  },
};
</script>
