<template>
  <el-dialog
    title="选择警用车辆"
    width="1400px"
    :model-value="modelValue"
    @close="closed"
  >
    <div v-if="modelValue">
      <el-form :model="listQuery" class="mosty-from-wrap" :inline="true">
        <el-form-item label="所属部门">
          <MOSTY.Department
            width="100%"
            clearable
            v-model="listQuery.ssbmdm"
          />
        </el-form-item>
        <el-form-item label="车牌号">
          <el-input
            v-model="listQuery.cph"
            placeholder="请输入车牌号"
          ></el-input>
        </el-form-item>
        <el-form-item label="车辆品牌">
          <el-select
            filterable
            v-model="listQuery.clpp"
            class="m-2"
            placeholder="请选择"
          >
            <el-option
              v-for="item in D_BZ_CLPP"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="success" @click="handleFilter">查询</el-button>
          <el-button type="info" @click="reset()"> 重置 </el-button>
        </el-form-item>
      </el-form>
      <div
        class="tabBox"
        :class="props.Single ? 'tabBoxRadio' : ''"
        style="margin-top: 0px"
      >
        <el-table
          ref="multipleUserRef"
          @selection-change="handleSelectionChange"
          :data="tableData"
          border
          :row-key="keyid"
          style="width: 100%"
          height="450"
        >
          <el-table-column type="selection" width="55" :reserve-selection="true" />
          <el-table-column
            type="index"
            show-overflow-tooltip
            align="center"
            width="60px"
            label="序号"
          >
          </el-table-column>
          <el-table-column
            sortable
            prop="ssbm"
            show-overflow-tooltip
            align="center"
            width="250px"
            label="所属部门"
          >
          </el-table-column>
          <el-table-column
            sortable
            prop="cplx"
            show-overflow-tooltip
            align="center"
            width="150px"
            label="号牌种类"
          >
            <template #default="{ row }">
              <dict-tag :options="D_BZ_HPZL" :value="row.cplx" :tag="false" />
            </template>
          </el-table-column>
          <el-table-column
            sortable
            prop="cph"
            label="车牌号"
            width="250px"
            align="center"
          ></el-table-column>
          <el-table-column
            sortable
            prop="clpp"
            show-overflow-tooltip
            label="车辆品牌"
            align="center"
            width="250px"
          >
            <template #default="{ row }">
              <dict-tag :options="D_BZ_CLPP" :value="row.clpp" :tag="false" />
            </template>
          </el-table-column>
          <el-table-column
            sortable
            prop="clnk"
            show-overflow-tooltip
            label="车辆年款"
            align="center"
          ></el-table-column>
        </el-table>
      </div>
      <div class="fenye">
        <el-pagination
          class="pagination"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="listQuery.pageCurrent"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="listQuery.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </div>
    </div>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="closed">取消</el-button>
        <el-button type="primary" @click="onComfirm">确认</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import * as rule from "@/utils/rules.js";
import * as MOSTY from "@/components/MyComponents/index";
import { ElMessage } from "element-plus";
import {
  defineProps,
  watch,
  ref,
  onMounted,
  nextTick,
  getCurrentInstance
} from "vue";
import { getXfcl } from "@/api/basicsmanage/watchmanCar";
const { proxy } = getCurrentInstance();
const { D_BZ_HPZL, D_BZ_CLYS, D_BZ_CLPP } = proxy.$dict(
  "D_BZ_HPZL",
  "D_BZ_CLYS",
  "D_BZ_CLPP"
);
const props = defineProps({
  modelValue: {
    type: Boolean,
    required: true
  },
  roleIds: {
    type: Array,
    default: []
  },
  //是否单选
  Single: {
    type: Boolean,
    default: false
  }
});
const total = ref(0);
const listQuery = ref({
  pageCurrent: 1,
  pageSize: 20
});
const form = ref({});
const tableData = ref([]);
const multipleUserRef = ref(null);
const multipleSelectionUser = ref([]);
const emits = defineEmits(["update:modelValue", "choosedUsers"]);
onMounted(() => {
  handleFilter();
});
const closed = () => {
  emits("update:modelValue", false);
};
const keyid = (row) => {
  return row.cid;
};
const reset = () => {
  listQuery.value = {
    pageCurrent: 1,
    pageSize: 20
  };
  getListData();
};

// 为用户分配角色
const onComfirm = () => {
  const userList = multipleSelectionUser.value;
  let list = []
  let listId = []
  userList.forEach(val=>{
    if(listId.indexOf(val.cid) == -1) {
      list.push(val);
      listId.push(val.cid);
    }
  })
  emits("choosedUsers", list);
  closed();
};
/**
 * pageSize 改变触发
 */
const handleSizeChange = (currentSize) => {
  listQuery.value.pageSize = currentSize;
  getListData();
};

/**
 * 页码改变触发
 */
const handleCurrentChange = (currentPage) => {
  listQuery.value.pageCurrent = currentPage;
  getListData();
};
const getListData = async () => {
  const params = listQuery.value;
  const res = await getXfcl(params);
  tableData.value = res?.records;
  total.value = Number(res.total);
  multipleUser()
};

const handleFilter = () => {
  listQuery.value.pageCurrent = 1;
  getListData();
};
const handleSelectionChange = (val) => {
  if (props.Single) {
    if (val.length > 1) {
      let del_row = val.shift();
      multipleUserRef.value.toggleRowSelection(del_row, false);
    }
    multipleSelectionUser.value = val;
  } else {
    multipleSelectionUser.value = val;
  }
};
//列表回显
function multipleUser() {
  tableData.value.forEach(item=>{
    if(props.roleIds.some(id=>id == item.cid)){
      multipleUserRef.value.toggleRowSelection(item, true);
    }
  })
}
</script>

<style lang="scss" scoped>
@import "@/assets/css/layout.scss";
@import "@/assets/css/element-plus.scss";
</style>
<style>
.tabBoxRadio .el-checkbox__inner {
  border-radius: 50% !important;
}
.tabBoxRadio .el-table__header-wrapper .el-checkbox {
  display: none;
}
</style>
