<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item :label="tLang('device', '设备名称')" prop="name">
        <el-input v-model="queryParams.name" :placeholder="tLang('common', '请输入') + tLang('device', '设备名称')" clearable
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item :label="tLang('device', '设备编码')" prop="code">
        <el-input v-model="queryParams.code" :placeholder="tLang('common', '请输入') + tLang('device', '设备编码')" clearable
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item :label="tLang('device', '车间名称')" prop="workShop">
        <el-input v-model="queryParams.workShop" :placeholder="tLang('common', '请输入') + tLang('device', '车间名称')" clearable
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item :label="tLang('device', '产线名称')" prop="lineName">
        <el-input v-model="queryParams.lineName" :placeholder="tLang('common', '请输入') + tLang('device', '产线名称')" clearable
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item :label="tLang('device', '保养周期')" prop="maintenance">
        <el-input v-model="queryParams.maintenance" :placeholder="tLang('common', '请输入') + tLang('device', '保养周期')"
          clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item :label="tLang('device', '供货商名称')" prop="supplier">
        <el-input v-model="queryParams.supplier" :placeholder="tLang('common', '请输入') + tLang('device', '供货商名称')"
          clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item :label="tLang('device', '供货商联络人')" prop="supplierContactPerson">
        <el-input v-model="queryParams.supplierContactPerson"
          :placeholder="tLang('common', '请输入') + tLang('device', '供货商联络人')" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item :label="tLang('device', '生产厂家')" prop="manufacturer">
        <el-input v-model="queryParams.manufacturer" :placeholder="tLang('common', '请输入') + tLang('device', '生产厂家')"
          clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item :label="tLang('device', '联系电话')" prop="contactPhone">
        <el-input v-model="queryParams.contactPhone" :placeholder="tLang('common', '请输入') + tLang('device', '联系电话')"
          clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item :label="tLang('device', '品牌')" prop="brand">
        <el-input v-model="queryParams.brand" :placeholder="tLang('common', '请输入') + tLang('device', '品牌')" clearable
          @keyup.enter="handleQuery" />
      </el-form-item>
      <!-- <el-form-item :label="针数" prop="needleNum">
        <el-input v-model="queryParams.needleNum" :placeholder="tLang('common','请输入') + 针数" clearable @keyup.enter="handleQuery" />
      </el-form-item> -->
      <el-form-item :label="tLang('device', '设备型号')" prop="model">
        <el-input v-model="queryParams.model" :placeholder="tLang('common', '请输入') + tLang('device', '设备型号')" clearable
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="search" size="mini" @click="handleQuery">{{ tLang('common', '搜索') }}</el-button>
        <el-button icon="refresh" size="mini" @click="resetQuery">{{ tLang('common', '重置') }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="plus" size="mini" @click="handleAdd" v-hasPermi="['device:device:add']">{{
      $t('common.新增') }}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['device:device:edit']">{{ $t('common.修改') }}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['device:device:remove']">{{ $t('common.删除') }}</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="warning" plain icon="download" size="mini" @click="handleExport"
          v-hasPermi="['device:device:export']">导出</el-button>
      </el-col> -->
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="deviceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" :label="$t('common.序号')" width="60" align="center" />
      <el-table-column :label="tLang('device', '设备名称')" align="center" v-if="columns[0].visible" prop="name" />
      <el-table-column :label="tLang('device', '设备编码')" align="center" v-if="columns[1].visible" prop="code" />
      <el-table-column :label="tLang('device', '设备类型')" align="center" v-if="columns[2].visible" prop="type" />
      <el-table-column :label="tLang('device', '车间名称')" align="center" v-if="columns[3].visible" prop="workShop" />
      <el-table-column :label="tLang('device', '产线名称')" align="center" v-if="columns[4].visible" prop="lineName" />
      <el-table-column :label="tLang('device', '保养周期')" align="center" v-if="columns[5].visible" prop="maintenance" />
      <el-table-column :label="tLang('device', '供货商名称')" align="center" v-if="columns[6].visible" prop="supplier" />
      <el-table-column :label="tLang('device', '供货商联络人')" align="center" v-if="columns[7].visible"
        prop="supplierContactPerson" />
      <el-table-column :label="tLang('device', '生产厂家')" align="center" v-if="columns[8].visible" prop="manufacturer" />
      <el-table-column :label="tLang('device', '联系电话')" align="center" v-if="columns[9].visible" prop="contactPhone" />
      <el-table-column :label="tLang('device', '品牌')" align="center" v-if="columns[10].visible" prop="brand" />
      <el-table-column :label="tLang('device', '设备型号')" align="center" v-if="columns[11].visible" prop="model" />
      <el-table-column :label="tLang('device', '备注')" align="center" v-if="columns[12].visible" prop="remark" />
      <el-table-column :label="tLang('common', '操作')" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-tooltip :content="tLang('common', '详情')" placement="top" v-if="scope.row.userId !== 1">
            <el-button link type="primary" icon="Tickets" @click="handleInfo(scope.row)"
              v-hasPermi="['device:device:info']"></el-button>
          </el-tooltip>
          <el-tooltip :content="tLang('common', '修改')" placement="top" v-if="scope.row.userId !== 1">
            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
              v-hasPermi="['device:device:edit']"></el-button>
          </el-tooltip>
          <el-tooltip :content="tLang('common', '删除')" placement="top" v-if="scope.row.userId !== 1">
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
              v-hasPermi="['device:device:remove']"></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination">
      <el-pagination :hide-on-single-page="true" v-model:current-page="queryParams.pageNum"
        v-model:page-size="queryParams.pageSize" :page-sizes="[25, 50, 100, 150]"
        layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="getList"
        @current-change="getList" />
    </div>

    <!-- 添加或修改设备信息对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body>
      <el-form ref="formRef" :inline="true" :model="form" :rules="rules" class="demo-form-inline" label-width="100px">
        <el-form-item :label="tLang('device', '设备名称')" prop="name">
          <el-input v-model="form.name" :placeholder="tLang('common', '请输入') + tLang('device', '设备名称')" />
        </el-form-item>
        <el-form-item :label="tLang('device', '设备编码')" prop="code">
          <el-input v-model="form.code" :placeholder="tLang('common', '请输入') + tLang('device', '设备编码')" />
        </el-form-item>
        <el-form-item :label="tLang('device', '设备类型')" prop="code">
          <el-input v-model="form.type" :placeholder="tLang('common', '请输入') + tLang('device', '设备编码')" />
        </el-form-item>
        <el-form-item :label="tLang('device', '车间名称')" prop="workShop">
          <el-input v-model="form.workShop" :placeholder="tLang('common', '请输入') + tLang('device', '车间名称')" />
        </el-form-item>
        <el-form-item :label="tLang('device', '产线名称')" prop="lineName">
          <el-input v-model="form.lineName" :placeholder="tLang('common', '请输入') + tLang('device', '产线名称')" />
        </el-form-item>
        <el-form-item :label="tLang('device', '保养周期')" prop="maintenance">
          <el-input v-model="form.maintenance" :placeholder="tLang('common', '请输入') + tLang('device', '保养周期')" />
        </el-form-item>
        <el-form-item :label="tLang('device', '供货商名称')" prop="supplier">
          <el-input v-model="form.supplier" :placeholder="tLang('common', '请输入') + tLang('device', '供货商名称')" />
        </el-form-item>
        <el-form-item :label="tLang('device', '供货商联络人')" prop="supplierContactPerson">
          <el-input v-model="form.supplierContactPerson"
            :placeholder="tLang('common', '请输入') + tLang('device', '供货商联络人')" />
        </el-form-item>
        <el-form-item :label="tLang('device', '生产厂家')" prop="manufacturer">
          <el-input v-model="form.manufacturer" :placeholder="tLang('common', '请输入') + tLang('device', '生产厂家')" />
        </el-form-item>
        <el-form-item :label="tLang('device', '联系电话')" prop="contactPhone">
          <el-input v-model="form.contactPhone" :placeholder="tLang('common', '请输入') + tLang('device', '联系电话')" />
        </el-form-item>
        <el-form-item :label="tLang('device', '品牌')" prop="brand">
          <el-input v-model="form.brand" :placeholder="tLang('common', '请输入') + tLang('device', '品牌')" />
        </el-form-item>
        <!-- <el-form-item :label="针数" prop="needleNum">
          <el-input-number v-model="form.needleNum" :min="1" />
        </el-form-item> -->
        <el-form-item :label="tLang('device', '设备型号')" prop="model">
          <el-input v-model="form.model" :placeholder="tLang('common', '请输入') + tLang('device', '设备型号')" />
        </el-form-item>
        <el-form-item :label="tLang('device', '图片')" prop="file">
          <image-upload v-model="form.file" />
        </el-form-item>
        <el-form-item :label="tLang('device', '备注')" prop="remark">
          <el-input v-model="form.remark" :placeholder="tLang('common', '请输入') + tLang('device', '备注')" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">{{ tLang('common', '确 定') }}</el-button>
          <el-button @click="cancel">{{ tLang('common', '取 消') }}</el-button>
        </div>
      </template>
    </el-dialog>
    <InfoVue v-model="infoDialog" :info="deviceinfo" />
  </div>
</template>

<script setup>
import { ref, reactive, watch, onMounted } from 'vue'
import { listDevice, getDevice, delDevice, addDevice, updateDevice } from "@/api/casm/device.js";
import InfoVue from "./info.vue";
const { proxy } = getCurrentInstance();
// 遮罩层
let loading = ref(true)
// 选中数组
let ids = ref([])
// 非单个禁用
let single = ref(true)
// 非多个禁用
let multiple = ref(true)
// 显示搜索条件
let showSearch = ref(false)
// 总条数
let total = ref(0)
// 设备信息表格数据
let deviceList = ref([])
// 弹出层标题
let title = ref("")
// 是否显示弹出层
let open = ref(false)
let infoDialog = ref(false)
let deviceinfo = ref({})
// 查询参数
let queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  name: null,
  code: null,
  type: null,
  workShop: null,
  lineName: null,
  maintenance: null,
  supplier: null,
  supplierContactPerson: null,
  manufacturer: null,
  contactPhone: null,
  brand: null,
  // needleNum: null,
  model: null,
  file: null,
})
// 表单参数
let form = ref({})
const formRef = ref(null)
// 表单校验
let rules = ref({
  needleNum: [
    {
      type: 'number',
      required: true,
      message: '针数只能为数字',
      trigger: 'change',
    },
  ],
})
// 列显隐信息
const columns = ref([
  { key: 0, label: proxy.tLang('device', `设备名称`), visible: true },
  { key: 1, label: proxy.tLang('device', `设备编码`), visible: true },
  { key: 2, label: proxy.tLang('device', `设备类型`), visible: true },
  { key: 3, label: proxy.tLang('device', `车间名称`), visible: true },
  { key: 4, label: proxy.tLang('device', `产线名称`), visible: false },
  { key: 5, label: proxy.tLang('device', `保养周期`), visible: false },
  { key: 5, label: proxy.tLang('device', `供货商名称`), visible: false },
  { key: 5, label: proxy.tLang('device', `供货商联络人`), visible: false },
  { key: 5, label: proxy.tLang('device', `生产厂家`), visible: false },
  { key: 5, label: proxy.tLang('device', `联系电话`), visible: false },
  { key: 5, label: proxy.tLang('device', `品牌`), visible: false },
  { key: 5, label: proxy.tLang('device', `设备型号`), visible: false },
  { key: 5, label: proxy.tLang('device', `备注`), visible: false },
]);
/** 查询设备信息列表 */
function getList() {
  loading.value = true;
  listDevice(queryParams).then(response => {
    deviceList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}
// 表单重置
function reset() {
  form.value = {
    id: null,
    name: null,
    code: null,
    workShop: null,
    lineName: null,
    maintenance: null,
    supplier: null,
    supplierContactPerson: null,
    manufacturer: null,
    contactPhone: null,
    brand: null,
    // needleNum: 1,
    model: null,
    file: null,
    remark: null
  };
  //resetForm("form");
}

//** 搜索按钮操作 */
function handleQuery() {
  queryParams.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  queryParams.pageNum = 1
  queryParams.pageSize = 10
  queryParams.name = null
  queryParams.code = null
  queryParams.type = null
  queryParams.workShop = null
  queryParams.lineName = null
  queryParams.maintenance = null
  queryParams.supplier = null
  queryParams.supplierContactPerson = null
  queryParams.manufacturer = null
  queryParams.contactPhone = null
  queryParams.brand = null
  // queryParams.needleNum = null
  queryParams.model = null
  queryParams.file = null

  handleQuery();
}
// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length !== 1
  multiple.value = !selection.length
}

/**详情按钮操作 */
function handleInfo(row) {
  deviceinfo.value = row
  infoDialog.value = true
}
/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = proxy.tLang('common', '添加') + ' ' + proxy.tLang('device', '设备信息');
}
/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const id = row.id || ids.value
  getDevice(id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = proxy.tLang('common', '修改') + ' ' + proxy.tLang('device', '设备信息');
  });
}
/** 提交按钮 */
function submitForm() {
  formRef.value.validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateDevice(form.value).then(response => {
          proxy.$modal.msgSuccess(proxy.tLang('common', '修改成功'));
          open.value = false;
          getList();
        });
      } else {
        addDevice(form.value).then(response => {
          proxy.$modal.msgSuccess(proxy.tLang('common', '新增成功'));
          open.value = false;
          getList();
        });
      }
    }
  });
}
/** 删除按钮操作 */
function handleDelete(row) {
  const id = row.id || ids.value;
  proxy.$modal.confirm(proxy.tLang('tip', '确定删除选中记录？') + ` [${id}]`).then(function () {
    return delDevice(id);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess(proxy.tLang('common', '删除成功'));
  }).catch(() => { });
}
/** 导出按钮操作 */
function handleExport() {
  this.download('casm/device/export', {
    ...queryParams
  }, `device_${new Date().getTime()}.xlsx`)
}

onMounted(() => {
  getList();
})
</script>

<style lang="scss" scoped>
.demo-form-inline :deep(.el-input) {
  --el-input-width: 220px;
}

.demo-form-inlinee :deep(.el-select) {
  --el-select-width: 220px;
}

.pagination {
  width: 100%;
  margin: 10px;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
