<template>
  <div class="app-container">
    <!--    搜索栏-->
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch"
             :label-width=" locale ? '100px':'140px'">
      <el-form-item :label="t('tireManagement.tireFactoryCode')" prop="tyreFacCode">
        <el-input
            v-model="queryParams.tyreFacCode"
            :placeholder=" t('common.pleaseEnter') + t('tireManagement.tireFactoryCode')"
            style="width: 200px"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="t('tireManagement.enterpriseCode')" prop="companyCode">
        <el-input
            v-model="queryParams.companyCode"
            :placeholder="t('common.pleaseEnter') + t('tireManagement.enterpriseCode')"
            style="width: 200px"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="t('tireManagement.plateNumber')" prop="carLicense">
        <el-input
            v-model="queryParams.carLicense"
            :placeholder="t('common.pleaseEnter') + t('tireManagement.plateNumber')"
            style="width: 200px"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="t('tireManagement.tireNo')" prop="tureCode">
        <el-input
            v-model="queryParams.tureCode"
            :placeholder="t('common.pleaseEnter') + t('tireManagement.tireNo')"
            style="width: 200px"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="RFID" prop="tyreRfid">
        <el-input
            v-model="queryParams.tyreRfid"
            :placeholder="t('common.pleaseEnter') +'RFID'"
            style="width: 200px"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="t('tireManagement.wheelPosition')" prop="tyrePosition">
        <el-input
            v-model="queryParams.tyrePosition"
            :placeholder="t('common.pleaseEnter') + t('tireManagement.wheelPosition')"
            style="width: 200px"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="t('tireManagement.tireSensorID')" prop="sensorId">
        <el-input
            v-model="queryParams.sensorId"
            :placeholder="t('common.pleaseEnter') + t('tireManagement.tireSensorID')"
            style="width: 200px"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="t('tireManagement.treadDepth')" prop="currentTextureDepth">
        <el-input
            v-model="queryParams.currentTextureDepth"
            :placeholder="t('common.pleaseEnter') + t('tireManagement.treadDepth')"
            style="width: 200px"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="t('common.state')" prop="state">
        <el-input
            v-model="queryParams.state"
            :placeholder="t('common.pleaseEnter') + t('common.state')"
            style="width: 200px"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="t('common.isRemove')" prop="isDelete">
        <el-input
            v-model="queryParams.isDelete"
            :placeholder="t('common.pleaseEnter') + t('common.isRemove')"
            style="width: 200px"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="t('common.creatorName')" prop="createName">
        <el-input
            v-model="queryParams.createName"
            :placeholder="t('common.pleaseEnter') + t('common.creatorName')"
            style="width: 200px"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="t('common.editTime')" prop="modifyTime">
        <el-date-picker
            clearable
            style="width: 200px"
            v-model="queryParams.modifyTime"
            type="date"
            value-format="yyyy-MM-dd"
            :placeholder="t('common.PleaseSelect') + t('common.editTime')"
        />
      </el-form-item>
      <el-form-item :label="t('common.editAccount')" prop="modifyBy">
        <el-input
            v-model="queryParams.modifyBy"
            :placeholder="t('common.pleaseEnter') + t('common.editAccount')"
            style="width: 200px"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="t('common.editName')" prop="modifyName">
        <el-input
            v-model="queryParams.modifyName"
            :placeholder="t('common.pleaseEnter') + t('common.editName')"
            style="width: 200px"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label=" ">
        <el-button type="primary" icon="Search" @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
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['basetyre:basetyre:add']"
        >{{ t('option.add') }}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['basetyre:basetyre:edit']"
        >{{ t('option.edit') }}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['basetyre:basetyre:remove']"
        >{{ t('option.remove') }}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['basetyre:basetyre:export']"
        >{{ 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 type="selection" width="55" align="center" fixed/>
      <el-table-column :label="t('tireManagement.tireFactoryCode')" align="center" prop="tyreFacCode" width="150"/>
      <el-table-column :label="t('tireManagement.enterpriseCode')" align="center" prop="companyCode" width="150"/>
      <el-table-column :label="t('tireManagement.carId')" align="center" prop="carId" width="150"/>
      <el-table-column :label="t('tireManagement.plateNumber')" align="center" prop="carLicense" width="150"/>
      <el-table-column :label="t('tireManagement.tireNo')" align="center" prop="tureCode" width="150"/>
      <el-table-column :label="t('tireManagement.tireModel')" align="center" prop="tureModel" width="150"/>
      <el-table-column :label="t('tireManagement.tireModelId')" align="center" prop="tureModelId" width="150"/>
      <el-table-column :label="t('tireManagement.brandId')" align="center" prop="tureBrandId" width="150"/>
      <el-table-column label="RFID" align="center" prop="tyreRfid" width="150"/>
      <el-table-column :label="t('tireManagement.wheelPosition')" align="center" prop="tyrePosition" width="150"/>
      <el-table-column :label="t('tireManagement.tireSensorID')" align="center" prop="sensorId" width="150"/>
      <el-table-column :label="t('tireManagement.treadDepth')" align="center" prop="currentTextureDepth" width="150"/>
      <el-table-column :label="t('common.state')" align="center" prop="state" width="150"/>
      <el-table-column :label="t('common.isRemove')" align="center" prop="isDelete" width="150"/>
      <el-table-column :label="t('common.creatorId')" align="center" prop="createId" width="150"/>
      <el-table-column :label="t('common.creatorName')" align="center" prop="createName" width="150"/>
      <el-table-column :label="t('common.editTime')" align="center" prop="modifyTime" width="150">
        <template #default="scope">
          <span>{{ parseTime(scope.row.modifyTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="t('common.editID')" align="center" prop="modifyId" width="150"/>
      <el-table-column :label="t('common.editAccount')" align="center" prop="modifyBy" width="150"/>
      <el-table-column :label="t('common.creatorName')" align="center" prop="modifyName" 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="Edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['basetyre:basetyre:edit']"
          >{{ t('option.edit') }}
          </el-button>
          <el-button
              type="text"
              icon="Delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['basetyre:basetyre:remove']"
          >{{ t('option.remove') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

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

    <!-- 添加或修改岗位对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="postRef" :model="form" :rules="rules"  :label-width=" locale ? '100px':'140px'">
        <el-form-item :label="t('tireManagement.tireFactoryCode')" prop="tyreFacCode">
          <el-input v-model="form.tyreFacCode"
                    :placeholder=" t('common.pleaseEnter') + t('tireManagement.tireFactoryCode')"/>
        </el-form-item>
        <el-form-item :label="t('tireManagement.enterpriseCode')" prop="companyCode">
          <el-input v-model="form.companyCode"
                    :placeholder="t('common.pleaseEnter') + t('tireManagement.enterpriseCode')"/>
        </el-form-item>
        <el-form-item :label="t('tireManagement.plateNumber')" prop="carLicense">
          <el-input v-model="form.carLicense" :placeholder="t('common.pleaseEnter') + t('tireManagement.plateNumber')"/>
        </el-form-item>
        <el-form-item :label="t('tireManagement.tireNo')" prop="tureCode">
          <el-input v-model="form.tureCode" :placeholder="t('common.pleaseEnter') + t('tireManagement.tireNo')"/>
        </el-form-item>
        <el-form-item label="RFID" prop="tyreRfid">
          <el-input v-model="form.tyreRfid" :placeholder="t('common.pleaseEnter') +'RFID'"/>
        </el-form-item>
        <el-form-item :label="t('tireManagement.wheelPosition')" prop="tyrePosition">
          <el-input v-model="form.tyrePosition"
                    :placeholder="t('common.pleaseEnter') + t('tireManagement.wheelPosition')"/>
        </el-form-item>
        <el-form-item :label="t('tireManagement.tireSensorID')" prop="sensorId">
          <el-input v-model="form.sensorId" :placeholder="t('common.pleaseEnter') + t('tireManagement.tireSensorID')"/>
        </el-form-item>
        <el-form-item :label="t('tireManagement.treadDepth')" prop="currentTextureDepth">
          <el-input v-model="form.currentTextureDepth"
                    :placeholder="t('common.pleaseEnter') + t('tireManagement.treadDepth')"/>
        </el-form-item>
        <el-form-item :label="t('common.state')" prop="state">
          <el-input v-model="form.state" :placeholder="t('common.pleaseEnter') + t('common.state')"/>
        </el-form-item>
        <el-form-item :label="t('common.isRemove')" prop="isDelete">
          <el-input v-model="form.isDelete" :placeholder="t('common.pleaseEnter') + t('common.isRemove')"/>
        </el-form-item>
        <el-form-item :label="t('common.creatorName')" prop="createName">
          <el-input v-model="form.createName" :placeholder="t('common.pleaseEnter') + t('common.creatorName')"/>
        </el-form-item>
        <el-form-item :label="t('common.editTime')" prop="modifyTime">
          <el-date-picker
              clearable
              v-model="form.modifyTime"
              type="date"
              format="YYYY/MM/DD"
              value-format="YYYY-MM-DD"
              :placeholder="t('common.PleaseSelect') + t('common.editTime')">
          </el-date-picker>
        </el-form-item>
        <el-form-item :label="t('common.editAccount')" prop="modifyBy">
          <el-input v-model="form.modifyBy" :placeholder="t('common.pleaseEnter') + t('common.editAccount')"/>
        </el-form-item>
        <el-form-item :label="t('common.editName')" prop="modifyName">
          <el-input v-model="form.modifyName" :placeholder="t('common.pleaseEnter') + t('common.editName')"/>
        </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>

<script setup name="basetyre">
import {addBasetyre, delBasetyre, getBasetyre, listBasetyre, updateBasetyre} from "@/api/basetyre/basetyre";

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

const {t} = useI18n();

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

/**
 * postList 表格数据
 * open 模态框开关标识
 * loading 表格加载状态开关标识
 * showSearch 搜索区域开关标识
 * ids 选择行id
 * single 修改按钮是否可用标识
 * multiple 删除按钮是否可用标识
 * total 总条数
 * title 模态框标题
 * */

const postList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
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,
  tyreFacCode: null,
  companyCode: null,
  carId: null,
  carLicense: null,
  tureCode: null,
  tureModel: null,
  tureModelId: null,
  tureBrandId: null,
  tyreRfid: null,
  tyrePosition: null,
  sensorId: null,
  currentTextureDepth: null,
  state: null,
  isDelete: null,
  createId: null,
  createName: null,
  modifyTime: null,
  modifyId: null,
  modifyBy: null,
  modifyName: null
})

const rules = {
  brand: [{required: true, message: "轮胎品牌不能为空", trigger: "blur"}],
  type: [{required: true, message: "轮胎型号不能为空", trigger: "blur"}],
  serialNumber: [{required: true, message: "轮胎编号不能为空", trigger: "blur"}],
  depth: [{required: true, message: "花纹深度不能为空", trigger: "blur"}],
}

const form = ref({});

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

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

/** 表单重置 */
function reset() {
  form.value = {
    id: null,
    tyreFacCode: null,
    companyCode: null,
    carId: null,
    carLicense: null,
    tureCode: null,
    tureModel: null,
    tureModelId: null,
    tureBrandId: null,
    tyreRfid: null,
    tyrePosition: null,
    sensorId: null,
    currentTextureDepth: null,
    state: null,
    isDelete: null,
    createTime: null,
    createId: null,
    createBy: null,
    createName: null,
    modifyTime: null,
    modifyId: null,
    modifyBy: null,
    modifyName: 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.postId);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加轮胎基础信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const postId = row.postId || ids.value;
  getBasetyre(postId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改轮胎基础信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["postRef"].validate(valid => {
    if (valid) {
      if (form.value.postId != undefined) {
        updateBasetyre(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addBasetyre(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const postIds = row.postId || ids.value;
  proxy.$modal.confirm('是否确认删除岗位编号为"' + postIds + '"的数据项？').then(function () {
    return delBasetyre(postIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

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

getList();
</script>
