<template>
  <div class="app-container">
    <!--    搜索栏-->
    <el-form v-show="showSearch" ref="queryRef" :inline="true" :label-width=" locale ? '110px':'170px'"
             :model="queryParams">
      <el-form-item :label="t('baseCar.car.carLicense')" prop="carLicense">
        <el-input
            v-model="queryParams.carLicense"
            :placeholder="t('common.pleaseEnter') + t('baseCar.car.carLicense')"
            clearable
            style="width: 200px"
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label=" ">
        <el-button icon="Search" type="primary" @click="handleQuery">{{ t('option.search') }}</el-button>
        <el-button icon="Refresh" @click="resetQuery">{{ t('option.reset') }}</el-button>
      </el-form-item>
    </el-form>

    <!--    顶部操作按钮-->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            icon="Plus"
            plain
            type="primary"
            @click="handleAdd"
            v-hasPermi="['basecar:car:add']"
        >{{ t('option.add') }}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            :disabled="single"
            icon="Edit"
            plain
            type="success"
            @click="handleUpdate"
            v-hasPermi="['basecar:car:edit']"
        >{{ t('option.edit') }}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            v-hasPermi="['basecar:car:remove']"
            :disabled="multiple"
            icon="Delete"
            plain
            type="danger"
            @click="handleDelete"
        >{{ t('option.remove') }}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            v-hasPermi="['basecar:car:export']"
            icon="Download"
            plain
            type="warning"
            @click="handleExport"
        >{{ t('option.export') }}
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!--    表格主体-->
    <el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange">
      <el-table-column align="center" fixed type="selection" width="55"/>
      <el-table-column :label="t('baseCar.car.carLicense')" align="center" prop="carLicense" width="150"/>
      <el-table-column align="center" label="所属车队" prop="carQueueId" />
      <el-table-column align="center" label="所属车型" prop="carTypeId" />
      <el-table-column :label="t('baseCar.car.deviceId')" align="center" prop="deviceId" width="150"/>
      <el-table-column :label="t('baseCar.car.deviceBindTime')" align="center" prop="deviceBindTime" width="150"/>
      <el-table-column :label="t('common.createTime')" align="center" prop="createTime" width="160"/>
      <el-table-column :label="t('common.createBy')" align="center" prop="createBy" width="150"/>
      <el-table-column :label="t('option.option')" align="center" class-name="small-padding fixed-width" fixed="right"
                       width="160">
        <template #default="scope">
          <el-button
              type="text"
              icon="Finished"
              @click="handleBang(scope.row)"
              >
              绑定接收器
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--    分页-->
    <pagination
        v-show="total > 0"
        v-model:limit="queryParams.pageSize"
        v-model:page="queryParams.pageNum"
        :total="total"
        @pagination="getList"
    />

      <el-dialog title="绑定接收器" v-model="bangOpen" width="500px" append-to-body>

      </el-dialog>
    <!-- 添加或修改岗位对话框 -->
    <el-dialog v-model="open" :title="title" append-to-body width="500px">
      <el-form ref="postRef" :label-width=" locale ? '120px':'140px'" :model="form" :rules="rules">
        <el-form-item :label="t('baseCar.car.carLicense')" prop="carLicense">
          <el-input
              v-model="form.carLicense"
              :placeholder="t('common.pleaseEnter') + t('baseCar.car.carLicense')"/>
        </el-form-item>
        <el-form-item label="所属车队" prop="carQueueId">
          <el-select
              v-model="form.carQueueId"
              :placeholder="t('common.pleaseEnter') + t('baseCar.car.carQueueId')"
              clearable
              style="width: 240px"
          >
            <el-option
                v-for="dict in carQueue"
                :key="dict.id"
                :label="dict.title"
                :value="dict.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="所属车型" prop="carTypeId">
          <el-select
              v-model="form.carTypeId"
              :placeholder="t('common.pleaseEnter') + t('baseCar.car.carQueueId')"
              clearable
              style="width: 240px"
          >
            <el-option
                v-for="dict in carType"
                :key="dict.id"
                :label="dict.title"
                :value="dict.id"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">{{ t('option.ok') }}</el-button>
          <el-button @click="cancel">{{ t('option.cancel') }}</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="basetyre">
import {addBaseCar, delBaseCar, getBaseCar, listBaseCar, updateBaseCar,getCarQueueList} from "@/api/basecar/car";

import {useI18n} from 'vue-i18n';
import Cookies from "js-cookie";

const {t} = useI18n();

const locale = (Cookies.get('language') || 'zhCn') === 'zhCn'
const {proxy} = getCurrentInstance();
const {sys_normal_disable} = proxy.useDict("sys_normal_disable");


// 表格数据
const postList = ref([]);
//车队和车型信息
const carQueue = ref([]);
const carType = ref([]);
// 模态框开关标识
const bangOpen = ref(false);
const open = ref(false);
// 表格加载状态开关标识
const loading = ref(true);
// 搜索区域开关标识
const showSearch = ref(true);
// 选择行id
const ids = ref([]);
// 修改按钮是否可用标识
const single = ref(true);
// 删除按钮是否可用标识
const multiple = ref(true);
// 总条数
const total = ref(0);
// 模态框标题
const title = ref("");

// 搜索参数
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  carLicense: undefined,

})

// 表单验证规则
const rules = {
  carLicense: [{required: true, message: "车牌号不能为空", trigger: "blur"}],
  carTypeId: [{required: true, message: "所属车型不能为空", trigger: "blur"}],
}

// 表单数据
const form = ref({
  id: null,
  carLicense: null,
  carQueueId: null,
  carTypeId: null,
});

/** 查询岗位列表 */
function getList() {
  loading.value = true;
  listBaseCar(queryParams.value).then(response => {
    postList.value = response.rows;
    total.value = response.total;
  }).finally(() => {
    loading.value = false;
  });
}


/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

/** 表单重置 */
function reset() {
  form.value = {
    id: null,
    carLicense: null,
    carQueueId: null,
    carTypeId: null,
  };
  proxy.resetForm("postRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  proxy.resetForm("queryRef");
  handleQuery();
}

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

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = t('baseCar.car.addTitle');
  getCarQueueList().then(response => {
    console.log(response);
    carQueue.value = response.CarQueueList;
    carType.value = response.CarTypeList;
  }).finally(() => {
    loading.value = false;
  });
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const postId = row.id || ids.value;
  getBaseCar(postId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = t('baseCar.car.editTitle');
  });
}
function handleBang(row){
    // console.log(row.id)
    bangOpen.value = true;
}

/** 提交按钮 */
function submitForm() {
  console.log(form)
  proxy.$refs["postRef"].validate(valid => {
    if (valid) {
      if (form.value.id != undefined) {
        updateBaseCar(form.value).then(response => {
          proxy.$modal.msgSuccess(t('option.modificationSuccessful'));
          open.value = false;
          getList();
        });
      } else {
        addBaseCar(form.value).then(response => {
          proxy.$modal.msgSuccess(t('option.addedSuccessfully'));
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const postIds = row.id || ids.value;
  proxy.$modal.confirm(t('option.confirmDeletion', {
    key: t('baseCar.car.companyCode'), value: postIds
  })).then(function () {
    return delBaseCar(postIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess(t('option.successfullyDeleted'));
  }).catch(() => {
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download("system/post/export", {
    ...queryParams.value
  }, `post_${new Date().getTime()}.xlsx`);
}

getList();
</script>
