<script name="supplierDevice" setup>
import {
  getSupplierFindPageList,
  getDeviceID,
  addDevice,
  updateDevice,
  deleteDevice
} from '@/api/device/supplierDevice.js'

import { formatDate } from '@/utils/index'

//路由
// const router = useRouter()
//this
const { proxy } = getCurrentInstance()
const { device_status } = proxy.useDict('device_status')
const { net_status } = proxy.useDict('net_status')

const deviceList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
//表格多选
const ids = ref([])
//不等于-1  为true
const single = ref(true)
//大于1   为true
const multiple = ref(true)
const total = ref(0)
const title = ref('')
//时间范围
const size = (ref < 'default') | 'large' | ('small' > 'default')
const value1 = ref('')

// const shortcuts = [
//   {
//     text: 'Today',
//     value: new Date()
//   },
//   {
//     text: 'Yesterday',
//     value: () => {
//       const date = new Date()
//       date.setTime(date.getTime() - 3600 * 1000 * 24)
//       return date
//     }
//   }
// ]

const data = reactive({
  form: {},
  queryParams: {
    pageNo: 1,
    pageSize: 10
  },
  rules: {
    deviceName: [
      {
        required: true,
        message: '设备名称不能为空',
        trigger: 'blur'
      }
    ],
    imei: [
      {
        required: true,
        message: 'IMEI号不能为空',
        trigger: 'blur'
      }
    ]
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询设备列表 */
function getList(id) {
  loading.value = true
  if (id) {
    id.createTime = formatDate(id.createTime)
    getDeviceID(id).then((response) => {
      if (response.result[0]) {
        deviceList.value = response.result
        total.value = 1
      } else {
        deviceList.value = []
        total.value = 0
      }
      loading.value = false
      proxy.$modal.msgSuccess('数据查询成功')
    })
  } else {
    getSupplierFindPageList(queryParams.value).then((response) => {
      deviceList.value = response.result
      total.value = response.totalSize
    })
    loading.value = false
  }
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.currentPage = 1
  getList(queryParams.value)
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm('queryRef')
  handleQuery()
}
/** 删除按钮操作 */
function handleDelete(row) {
  const deviceIds = row.id ? [row.id] : ids.value
  proxy.$modal
    .confirm('是否确认删除设备编号为"' + row.projectName + '"的数据项?')
    .then(() => {
      return deleteDevice(deviceIds)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess('删除成功')
    })
    .catch(() => {})
}

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

/** 重置新增的表单以及其他数据  */
function reset() {
  form.value = {
    projectName: '',
    supplierName: '',
    deviceName: '',
    deviceNo: '',
    supplyProductsType: '',
    supplyProductsType: '',
    brand: '',
    price: 0,
    purchaseQuantity: 0,
    sumPrice: 0,
    purchaseDate: '',
    contacts: '',
    contactsPhone: '',
    productGrade: '',
    createTime: '',
    createBy: '',
    remarks: ''
  }
  //表单重置
  proxy.resetForm('deviceRef')
}
/** 新增按钮点击事件 */
function handleAdd() {
  //重置表单
  reset()
  open.value = true
  title.value = '添加'
}
/** 修改设备 */
function handleUpdate(row) {
  reset()
  console.log(row.id)
  getDeviceID(row.id).then((res) => {
    console.log(res)
    form.value = res.data
  })
  title.value = '修改'
  open.value = true

  // })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs['deviceRef'].validate((valid) => {
    if (valid) {
      form.value.purchaseDate = formatDate(form.value.purchaseDate)
      try {
        if (form.value.id) {
          updateDevice(form.value).then((res) => {
            if (res) {
              proxy.$modal.msgSuccess('设备修改成功')
              getList()
              open.value = false
            }
          })
        } else {
          addDevice(form.value).then((res) => {
            if (res) {
              proxy.$modal.msgSuccess('设备新增成功')
              getList()
              open.value = false
            }
          })
        }
      } catch {
        proxy.$modal.msgError('失败')
      }
    }
  })
}
/** 取消按钮 */
function cancel() {
  open.value = false
  reset()
}

getList()
</script>

<template>
  <div class="app-container">
    <div style="display: flex">
      <!-- 搜索功能 -->
      <el-form
        :model="queryParams"
        ref="queryRef"
        v-show="showSearch"
        v-hasPermi="['device:deviceInfo:query']"
        :inline="true"
      >
        <el-form-item label="设备名称" prop="deviceName">
          <el-input
            v-model.number="queryParams.deviceName"
            placeholder="请输入设备名称"
            clearable
            style="width: 240px"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="设备编号" prop="deviceNo">
          <el-input
            v-model.number="queryParams.deviceNo"
            placeholder="请输入设备编号"
            clearable
            style="width: 240px"
            @keyup.enter="handleQuery"
          />
        </el-form-item>

        <el-form-item label="项目名称" prop="projectName">
          <el-input
            v-model.number="queryParams.projectName"
            placeholder="请输入设备编号"
            clearable
            style="width: 240px"
            @keyup.enter="handleQuery"
          />
        </el-form-item>

        <el-form-item label="供应商单位名称" prop="supplierName">
          <el-input
            v-model.number="queryParams.supplierName"
            placeholder="请输入供应商"
            clearable
            style="width: 240px"
            @keyup.enter="handleQuery"
          />
        </el-form-item>

        <el-form-item label="录入时间" prop="createTime">
          <el-date-picker
            v-model="queryParams.createTime"
            type="date"
            placeholder="请选择时间"
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 新增,修改 -->
      <el-row :gutter="10" class="mb8" style="height: 32px">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['device:deviceInfo:add']"
            >新增</el-button
          >
        </el-col>
        <!-- <el-col :span="1.5">
					<el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
						v-hasPermi="['device:deviceInfo:edit']">修改</el-button>
				</el-col> -->
        <!-- <el-col :span="1.5">
					<el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete({})"
						v-hasPermi="['device:deviceInfo:remove']">删除</el-button>
				</el-col> -->
        <!-- <right-toolbar v-model:showSearch="showSearch" v-hasPermi="['device:deviceInfo:query']"
					@queryTable="getList"></right-toolbar> -->
      </el-row>
    </div>
    <!-- 表格数据 -->
    <el-table v-loading="loading" :data="deviceList" stripe>
      <!-- @selection-change="handleSelectionChange" -->
      <!-- 多选框 -->
      <!-- <el-table-column
        type="selection"
        width="40"
        align="center"
        :fixed="true"
      /> -->
      <el-table-column
        label="项目名称"
        prop="projectName"
        align="center"
        :fixed="true"
      />
      <el-table-column
        label="供货商单位名称"
        width="130"
        prop="supplierName"
        align="center"
        :fixed="true"
      />
      <el-table-column
        label="设备名称"
        prop="deviceName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="设备编号"
        prop="deviceNo"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="供应产品种类"
        prop="supplyProductsType"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="供应产品型号规格"
        prop="supplyProductsType"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="品牌"
        prop="brand"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="单价"
        prop="price"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="购买数量"
        prop="purchaseQuantity"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="合计金额 "
        prop="sumPrice"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="采购日期"
        prop="purchaseDate"
        align="center"
        width="160"
      />
      <el-table-column
        label="联系人 "
        prop="contacts"
        :show-overflow-tooltip="true"
      />

      <el-table-column
        label="联系方式 "
        prop="contactsPhone"
        :show-overflow-tooltip="true"
      />

      <el-table-column
        label="等级"
        prop="productGrade"
        :show-overflow-tooltip="true"
      />

      <el-table-column
        label="创建时间"
        prop="createTime"
        width="160"
        align="center"
      />

      <el-table-column
        label="添加用户"
        prop="createBy"
        :show-overflow-tooltip="true"
      />

      <el-table-column
        label="备注"
        prop="remarks"
        :show-overflow-tooltip="true"
      />

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="{ row }">
          <el-tooltip content="修改" placement="top" v-if="row.roleId !== 1">
            <el-button
              type="text"
              icon="Edit"
              @click="handleUpdate(row)"
              v-hasPermi="['device:deviceInfo:edit']"
            ></el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top" v-if="row.roleId !== 1">
            <el-button
              type="text"
              icon="Delete"
              @click="handleDelete(row)"
              v-hasPermi="['device:deviceInfo:remove']"
            ></el-button>
          </el-tooltip>
          <!-- <el-tooltip content="查看" placement="top" v-if="row.roleId !== 1">
            <el-button
              type="text"
              icon="View"
              @click="handleDelete(row)"
              v-hasPermi="['device:deviceInfo:remove']"
            ></el-button>
          </el-tooltip> -->
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination
      v-show="total"
      :total="total"
      v-model:page="queryParams.currentPage"
      v-model:limit="queryParams.size"
      @pagination="getList()"
    />

    <!-- 添加或修改设备配置对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body>
      <!-- :rules="rules" -->
      <el-form ref="deviceRef" :model="form" label-width="130px" :inline="true">
        <el-form-item label="项目名称" prop="projectName">
          <el-input v-model="form.projectName" placeholder="请输入项目名称" />
        </el-form-item>
        <el-form-item label="供货商单位名称" prop="supplierName">
          <el-input
            v-model="form.supplierName"
            placeholder="请输入供货商单位名称"
          />
        </el-form-item>
        <el-form-item label="设备名称" prop="deviceName">
          <el-input v-model="form.deviceName" placeholder="请输入设备名称" />
        </el-form-item>
        <el-form-item label="设备编号" prop="deviceNo">
          <el-input v-model="form.deviceNo" placeholder="请输入设备编号" />
        </el-form-item>
        <el-form-item label="供应产品种类" prop="supplyProductsType">
          <el-input
            v-model="form.supplyProductsType"
            placeholder="请输入供应产品种类"
          />
        </el-form-item>
        <el-form-item label="供应产品型号规格" prop="supplyProductsType">
          <el-input
            v-model="form.supplyProductsType"
            placeholder="请输入供应产品型号规格"
          />
        </el-form-item>
        <el-form-item label="品牌" prop="brand">
          <el-input v-model="form.brand" placeholder="请输入品牌" />
        </el-form-item>
        <el-form-item label="单价" prop="price">
          <el-input v-model.number="form.price" placeholder="请输入单价" />
        </el-form-item>
        <el-form-item label="购买数量" prop="purchaseQuantity">
          <el-input
            v-model.number="form.purchaseQuantity"
            placeholder="请输入购买数量"
          />
        </el-form-item>
        <el-form-item label="合计金额" prop="sumPrice">
          <el-input
            v-model.number="form.sumPrice"
            placeholder="请输入合计金额"
          />
        </el-form-item>
        <el-form-item label="采购日期" prop="purchaseDate">
          <el-date-picker
            v-model="form.purchaseDate"
            type="datetime"
            editable
            placeholder="请选择采购日期"
          />
        </el-form-item>
        <el-form-item label="联系人" prop="contacts">
          <el-input v-model="form.contacts" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="联系方式" prop="contactsPhone">
          <el-input v-model="form.contactsPhone" placeholder="请输入联系方式" />
        </el-form-item>
        <el-form-item label="等级" prop="productGrade">
          <el-input v-model="form.productGrade" placeholder="请输入等级" />
        </el-form-item>
        <!-- <el-form-item label="创建时间" prop="createTime">
          <el-input v-model="form.createTime" placeholder="请输入项目名称" />
        </el-form-item>
        <el-form-item label="添加用户" prop="createBy">
          <el-input v-model="form.createBy" placeholder="请输入项目名称" />
        </el-form-item> -->
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
