<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="70px"
    >
      <el-form-item label="申请人" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入申请人"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="使用时间" prop="startUseTime">
        <el-date-picker
          @change="changeDate"
          v-model="value1"
          type="daterange"
          range-separator="至"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          value-format="YYYY-MM-DD"
          :size="size"
        />
      </el-form-item>
      <el-form-item label="目的地" prop="addres">
        <el-input
          v-model="queryParams.addres"
          placeholder="请输入目的地"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['offcial:car:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="carList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column label="申请人" align="center" prop="createBy" />
      <el-table-column label="联系方式" align="center" prop="phone" />
      <el-table-column label="目的地" align="center" prop="addres" />
      <el-table-column
        label="用车时间"
        align="center"
        prop="startUseTime"
        width="190"
      >
        <template #default="scope">
          <span>{{
            parseTime(scope.row.startUseTime, "{y}-{m}-{d}") +
            " 至 " +
            parseTime(scope.row.endUseTime, "{y}-{m}-{d}")
          }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column
        label="结束使用时间"
        align="center"
        prop="endUseTime"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.endUseTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="天数" align="center" prop="dayNum" />
      <el-table-column label="用途" align="center" prop="useMessage" />
      <el-table-column label="乘车人数" align="center" prop="carNum" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button @click="viewDetails(scope.row)" plain type="primary"
            >查看</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"
    />
    <carApproval ref="carRef" @guanbi="handleQuery"></carApproval>
  </div>
</template>

<script setup name="Car">
import { listCar, getCar, delCar, addCar, updateCar } from "@/api/car";
import carApproval from "@/views/liucheng/todowork/formView/carApproval.vue";

const { proxy } = getCurrentInstance();

const carList = 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 value1 = ref(null);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    applyUserId: null,
    startUseTime: null,
    endUseTime: null,
    dayNum: null,
    addres: null,
    useMessage: null,
    carNum: null,
    isAsc: "Desc",
    orderByColumn: "createTime",
  },
  rules: {},
});

const { queryParams, form, rules } = toRefs(data);
function viewDetails(row) {
  console.log(row);
  row.WorkID = row.workId;
  proxy.$refs["carRef"].closeSendNext();

  proxy.$refs["carRef"].showDetail1(row);
}
/** 查询申请用车列表 */
function getList() {
  loading.value = true;
  listCar(queryParams.value).then((response) => {
    carList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
function changeDate(val) {
  console.log(val);
  queryParams.value.startUseTime = val[0];
  queryParams.value.endUseTime = val[1];
}

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

// 表单重置
function reset() {
  form.value = {
    id: null,
    applyUserId: null,
    startUseTime: null,
    endUseTime: null,
    dayNum: null,
    addres: null,
    useMessage: null,
    carNum: null,
    createBy: null,
    createTime: null,
    remark: null,
  };
  proxy.resetForm("carRef");
}

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

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  value1.value = null;
  queryParams.value.startUseTime = null;
  queryParams.value.endUseTime = null;
  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 = "添加申请用车";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  console.log("row", row);
  reset();
  const _id = row.id || ids.value;
  getCar(_id).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改申请用车";
  });
}

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

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

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

getList();
</script>
