<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="INQUIRYCONTENT">
              <el-input
                v-model="searchForm.INQUIRYCONTENT"
                placeholder="请输入问询内容"
              />
            </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
        v-model:pagination="pagination"
        :data="list"
        @get-data="fnGetData"
      >
        <el-table-column label="序号" width="60">
          <template #default="{ $index }">
            {{ serialNumber(pagination, $index) }}
          </template>
        </el-table-column>
        <el-table-column prop="INQUIRYCONTENT" label="问询内容" />
        <el-table-column
          prop="INQUIRYCONCLUSION"
          label="问询结果"
          width="100"
        />
        <el-table-column prop="CREATTIME" label="创建时间" width="150" />
        <el-table-column prop="OPERATTIME" label="修改时间" width="150" />
        <el-table-column label="操作" width="100">
          <template #default="{ row }">
            <el-button
              type="primary"
              text
              link
              @click="fnAddOrEdit(row.DRIVINGCOMMITMENT_ID, 'edit')"
            >
              修改
            </el-button>
            <el-button
              type="danger"
              text
              link
              @click="deleteItem(row.DRIVINGCOMMITMENT_ID)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
        <template #button>
          <el-button type="primary" @click="fnAddOrEdit('', 'add')">
            新增
          </el-button>
        </template>
      </layout-table>
    </layout-card>
    <add
      v-model:visible="data.addOrEditDialog.visible"
      v-model:form="data.addOrEditDialog.form"
      :type="data.addOrEditDialog.type"
      @get-data="fnResetPagination"
    />
  </div>
</template>

<script setup>
import { serialNumber } from "@/assets/js/utils";
import useListData from "@/assets/js/useListData.js";
import { nextTick, reactive } from "vue";
import Add from "./components/add.vue";
import {
  getSafetyDrivingCommitmentList,
  getSafetyDrivingCommitmentView,
  setDrivingCommitmentDelete,
} from "@/request/traffic_driving_commitment.js";
import { ElMessage, ElMessageBox } from "element-plus";

const { list, pagination, searchForm, fnGetData, fnResetPagination } =
  useListData(getSafetyDrivingCommitmentList);
const data = reactive({
  addOrEditDialog: {
    visible: false,
    type: "",
    form: {
      INQUIRYCONTENT: "",
    },
  },
});

const fnAddOrEdit = async (DRIVINGCOMMITMENT_ID, type) => {
  data.addOrEditDialog.visible = true;
  await nextTick();
  data.addOrEditDialog.type = type;
  if (type === "edit") {
    const resData = await getSafetyDrivingCommitmentView({
      DRIVINGCOMMITMENT_ID,
    });
    data.addOrEditDialog.form = resData.pd;
  }
};

// 删除事件
const deleteItem = async (value) => {
  await ElMessageBox.confirm(`确定要删除吗？`, {
    type: "warning",
  });
  await setDrivingCommitmentDelete({ DRIVINGCOMMITMENT_ID: value });
  ElMessage.success("删除成功");
  fnGetData();
};
</script>

<style scoped></style>
