<template>
  <div>
    <div class="titleBox">
      <div class="title">每日街面装备</div>
      <div class="btnBox">
        <el-button type="primary" @click="dialogFormVisible = true">
          <el-icon style="vertical-align: middle">
            <CirclePlus />
          </el-icon>
          <span style="vertical-align: middle">新增</span>
        </el-button>

        <el-button typeof="danger">
          <el-icon style="vertical-align: middle">
            <Delete />
          </el-icon>
          <span style="vertical-align: middle">批量注销</span>
        </el-button>
      </div>
    </div>
    <div class="searchBox" ref="searchBox">
      <el-form :model="form" :inline="true">
        <el-form-item label="所属部门">
          <el-select clearable v-model="form.ssbm" placeholder="请选择所属部门">
            <el-option label="警察局" value="1"></el-option>
            <el-option label="警务室" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="装备类型">
          <el-select clearable v-model="form.zblx" placeholder="请选择装备类型">
            <el-option label="枪支" value="1"></el-option>
            <el-option label="弹药" value="2"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="生产日期">
          <el-date-picker
            v-model="form.gzsj"
            type="date"
            placeholder="请选择日期"
          />
        </el-form-item>
        <el-form-item>
          <el-button @click="handleFilter"> 查询 </el-button>
          <el-button @click="reset()"> 重置 </el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="tabBox">
      <el-table
        :data="tableData"
        border
        row-key="id"
        :tree-props="{ children: 'itemList', hasChildren: true }"
        style="width: 100%"
        :height="tableHeight"
        :key="keyCount"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column
          prop="code"
          show-overflow-tooltip
          align="center"
          width="100px"
          label="序号"
        >
        </el-table-column>
        <el-table-column
          sortable
          prop="type"
          show-overflow-tooltip
          align="center"
          width="350px"
          label="装备类型"
        >
        </el-table-column>
        <el-table-column
          sortable
          prop="department"
          label="所属部门"
          align="center"
          width="250px"
        ></el-table-column>
        <el-table-column
          sortable
          prop="date"
          show-overflow-tooltip
          label="生产日期"
          align="center"
          width="200px"
        >
        </el-table-column>
        <el-table-column
          sortable
          prop="batch"
          align="center"
          show-overflow-tooltip
          label="录入批次"
        ></el-table-column>
        <el-table-column
          sortable
          prop="coding"
          show-overflow-tooltip
          align="center"
          label="唯一编码"
        ></el-table-column>
        <el-table-column
          sortable
          prop="status"
          show-overflow-tooltip
          label="状态"
          align="center"
        ></el-table-column>

        <el-table-column
          label="操作"
          align="center"
          fixed="right"
          width="150px"
        >
          <template #default="{ row }">
            <el-button @click="update(row)" size="small">修改</el-button>
            <el-popconfirm
              confirm-button-text="是"
              cancel-button-text="否"
              icon-color="red"
              title="确定要注销?"
              @confirm="delDictItem(row)"
            >
              <template #reference>
                <el-button type="danger" size="small">注销</el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <div class="fenye" :style="{ top: tableHeight + 'px' }">
        <el-pagination
          class="pagination"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="form.current"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="form.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>
    <div v-if="dialogFormVisible" class="dialog">
      <div class="head_box">
        <span class="title">新增装备</span>
        <div>
          <el-button type="primary" size="small" @click="submit"
            >保存</el-button
          >
          <el-button size="small" @click="dialogFormVisible = false"
            >关闭</el-button
          >
        </div>
      </div>
      <el-form
        ref="elform"
        :rules="rules"
        :model="addForm"
        :inline="true"
        label-position="top"
      >
        <el-form-item label="装备类型" prop="zblx">
          <el-select
            v-model="addForm.zblx"
            placeholder="请选择"
            style="width: 100%"
          >
            <el-option label="枪支" value="1" />
            <el-option label="警务移动端" value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="所属部门" prop="ssbm">
          <el-select
            v-model="addForm.ssbm"
            placeholder="请选择"
            style="width: 100%"
          >
            <el-option label="部门一" value="1" />
            <el-option label="部门二" value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="购置日期">
          <el-date-picker
            v-model="addForm.gzrq"
            type="date"
            placeholder="请选择日期"
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item label="录入批次">
          <el-select
            v-model="addForm.lrpc"
            placeholder="请选择"
            style="width: 100%"
          >
            <el-option label="第一批次" value="1" />
            <el-option label="第二批次" value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="唯一编码" prop="wybm">
          <el-input
            v-model="addForm.wybm"
            placeholder="请输入编码"
            clearable
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item label="使用年限" prop="synx">
          <el-date-picker
            v-model="addForm.synx"
            type="date"
            placeholder="请选择日期"
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item label="装备名称">
          <el-input
            v-model="addForm.zbmc"
            placeholder="请输入装备名称"
            clearable
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item label="设备型号">
          <el-input
            v-model="addForm.sbxh"
            placeholder="请输入设备型号"
            clearable
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item label="装备厂商" prop="zbcs">
          <el-input
            v-model="addForm.zbcs"
            placeholder="请输入装备厂商"
            clearable
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item label="备注" style="width: 100%">
          <el-input
            v-model="addForm.bz"
            placeholder="请输入关键字"
            show-word-limit
            type="textarea"
          />
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, getCurrentInstance, onUnmounted } from "vue";
const { proxy } = getCurrentInstance();
const searchBox = ref(null); // 搜索盒子
const keyCount = ref(0); //tabel组件刷新值
const tableHeight = ref(); // 表格高度

//查询参数
const form = ref({
  current: 1,
  size: 20,
  zblx: "",
  ssbm: "",
  gzsj: ""
});
//新增表单参数
const addForm = ref({
  zblx: "1",
  ssbm: "1",
  gzrq: "",
  lrpc: "1",
  wybm: "",
  synx: "",
  zbmc: "",
  sbxh: "",
  zbcs: "",
  bz: ""
});
const tableData = ref([]); //表单数据
const total = ref(0); //总数据
const dialogFormVisible = ref(false); //是否打开新增弹窗
const elform = ref({}); //表单对象
//表单验证
const rules = reactive({
  zblx: [
    {
      required: true
    }
  ],
  ssbm: [
    {
      required: true
    }
  ],
  wybm: [
    {
      required: true,
      message: "请输入唯一编码"
    }
  ],
  synx: [
    {
      required: true,
      message: "选择使用年限"
    }
  ],
  zbcs: [
    {
      required: true,
      message: "请输入装备厂商"
    }
  ]
});
//新增提交
function submit() {
  elform.value.validate((valid) => {

    if (valid) {

    }
  });
}
onMounted(() => {
  getListData();
  tabHeightFn();
  window.onresize = function () {
    tabHeightFn();
  };
  proxy.mittBus.on("mittFn", (data) => {
    keyCount.value = data;
  });
});
onUnmounted(() => {
  proxy.mittBus.off("mittFn");
});
//点击查询
const handleFilter = () => {
  form.value.current = 1;
  getListData();
};
//获取数据
const getListData = () => {
  const arr = [
    {
      code: 1,
      type: "移动警务终端",
      department: "xx部门",
      date: "2022-06-02",
      batch: "第二批次",
      coding: "NKJ89897",
      status: "正常"
    },
    {
      code: 2,
      type: "枪支",
      department: "xx部门",
      date: "2022-06-02",
      batch: "第二批次",
      coding: "NKJ89897",
      status: "正常"
    },
    {
      code: 3,
      type: "枪支",
      department: "xx部门",
      date: "2022-06-02",
      batch: "第二批次",
      coding: "NKJ89897",
      status: "正常"
    }
  ];
  tableData.value = arr;
  total.value = arr.length;
};
//点击重置
const reset = () => {
  form.value = {
    current: 1,
    size: 20,
    zblx: "",
    ssbm: "",
    gzsj: ""
  };
  getListData();
};
// 表格高度计算
const tabHeightFn = () => {
 tableHeight.value = window.innerHeight - searchBox.value.offsetHeight - 240;
};
</script>

<style lang="scss" scoped>
@import "~@/assets/css/layout.scss";
@import "~@/assets/css/element-plus.scss";
</style>
