<template>
  <div>
    <el-card>
      <el-form
        :model="searchForm"
        label-width="100px"
        @submit.prevent="fnResetPagination"
      >
        <el-row>
          <el-col :span="6">
            <el-form-item label="车辆车牌" prop="TRANSPORTVEHICLE">
              <el-input v-model="searchForm.TRANSPORTVEHICLE" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="运单状态" prop="WAYBILLSTATUS">
              <el-select v-model="searchForm.WAYBILLSTATUS">
                <el-option label="已出发" :value="1" />
                <el-option label="未出发" :value="0" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label-width="10px">
              <el-button type="primary" native-type="submit">搜索</el-button>
              <el-button native-type="reset" @click="fnResetPagination">
                重置
              </el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <layout-card>
      <layout-table
        ref="tableRef"
        v-model:pagination="pagination"
        :data="list"
        @get-data="fnGetData"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column label="序号" width="60">
          <template #default="{ $index }">
            {{ serialNumber(pagination, $index) }}
          </template>
        </el-table-column>
        <el-table-column prop="WAYBILLNUMBER" label="运单编号" width="150" />
        <el-table-column prop="WAYBILLSTATUS" label="运单状态" width="150">
          <template #default="{ row }">
            <el-tag v-if="row.WAYBILLSTATUS === '0'">未出发</el-tag>
            <el-tag v-else-if="row.WAYBILLSTATUS === '1'">已出发</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="TRANSPORTVEHICLE" label="运输车辆" width="150" />
        <el-table-column prop="PRACTITIONER" label="从业人员" width="150" />
        <el-table-column prop="CLIENT" label="委托方" width="150">
        </el-table-column>
        <el-table-column prop="CONFIRMER" label="确认方" width="150">
        </el-table-column>
        <el-table-column prop="ORIGIN" label="起运地" width="150">
        </el-table-column>
        <el-table-column prop="DESTINATION" label="目的地" width="150">
        </el-table-column>
        <el-table-column prop="SHIPPINGDATE" label="起止时间" width="180">
        </el-table-column>
        <el-table-column label="操作" width="120"> </el-table-column>
        <template #button>
          <el-button
            type="primary"
            @click="
              router.push({
                path: '/electronic_waybill_management/waybill_registration/add',
              })
            "
          >
            添加
          </el-button>
          <el-button
            v-if="buttonJurisdiction.del"
            type="danger"
            @click="fnBatchDelete"
          >
            批量删除
          </el-button>
        </template>
      </layout-table>
    </layout-card>
    <edit
      v-model:visible="data.analysisDialog.visible"
      :info="data.analysisDialog.info"
      @get-data="fnResetPagination"
    />
  </div>
</template>

<script setup>
import { serialNumber } from "@/assets/js/utils.js";
import useListData from "@/assets/js/useListData.js";
import { onMounted, reactive, ref } from "vue";

import {
  layoutFnGetNotificationsClassification,
  dateteSecurityAllNotice,
} from "@/request/safety_production_related.js";
import { getSecurityWaybillreList } from "@/request/waybill_registration.js";
import router from "@/router/index.js";
import { ElMessageBox, ElMessage } from "element-plus";
import { debounce } from "throttle-debounce";
import useButtonJurisdiction from "@/assets/js/useButtonJurisdiction.js";

const buttonJurisdiction = await useButtonJurisdiction("riskunit");

const tableRef = ref(null);

const { list, pagination, searchForm, fnGetData, fnResetPagination } =
  useListData(getSecurityWaybillreList);
const data = reactive({
  analysisDialog: {
    visible: false,
    info: {},
  },
});
const fnResetPaginationTransfer = () => {
  fnResetPagination({
    DEPTIDS: searchForm.value.DEPTIDS?.join(","),
  });
};
const fnBatchDelete = debounce(
  1000,
  async () => {
    // eslint-disable-next-line no-undef
    const selectionData = tableRef.value.getSelectionRows();
    if (selectionData.length === 0) {
      ElMessage.warning("请选中要删除的项");
      return;
    }
    await ElMessageBox.confirm("确定要删除选中的数据吗？", { type: "warning" });
    const DATA_IDS = selectionData
      .map((item) => item.NOTIFICATION_ID)
      .join(",");
    await dateteSecurityAllNotice({ DATA_IDS });
    ElMessage.success("删除成功");
    fnResetPaginationTransfer();
  },
  { atBegin: true }
);
const relatedClassificationList = ref(null);

onMounted(async () => {
  const data = await layoutFnGetNotificationsClassification();
  relatedClassificationList.value = JSON.parse(data.value.zTreeNodes);
});
</script>

<style scoped></style>
