<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="MANUALNAME">
              <el-input v-model="searchForm.MANUALNAME" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="完成状态" prop="COMPLETIONSTATUS">
              <el-select v-model="searchForm.COMPLETIONSTATUS">
                <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="有效状态" prop="VALIDSTATUS">
              <el-select v-model="searchForm.VALIDSTATUS">
                <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"
        row-key="MANUAL_ID"
        @get-data="fnGetData"
      >
        <el-table-column label="序号" width="60">
          <template #default="{ $index }">
            {{ serialNumber(pagination, $index) }}
          </template>
        </el-table-column>
        <el-table-column prop="MANUALNAME" label="名称" width="250" />
        <el-table-column prop="COMPLETIONSTATUS" label="完成状态" width="250">
          <template #default="{ row }">
            <el-tag v-if="row.COMPLETIONSTATUS === '1'">已上传</el-tag>
            <el-tag v-else-if="row.COMPLETIONSTATUS === '0'">未上传</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="有效状态" prop="VALIDSTATUS">
          <template #default="{ row }">
            <el-tag v-if="row.VALIDSTATUS === '1'">正常</el-tag>
            <el-tag v-else-if="row.VALIDSTATUS === '0'">未生效</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="CORP_NAME" label="经营企业" width="150" />
        <el-table-column prop="EXPIRYDATE" label="到期时间" width="150" />
        <el-table-column prop="CREATETIME" label="创建时间" width="150" />
        <el-table-column prop="OPERATTIME" label="修改时间" width="150" />
        <el-table-column label="操作" width="180">
          <template #default="{ row }">
            <el-button type="primary" text link @click="fnView(row.MANUAL_ID)">
              查看
            </el-button>
            <el-button
              type="primary"
              text
              link
              @click="fnAddOrEdit(row.MANUAL_ID, 'edit')"
            >
              编辑
            </el-button>
            <el-button
              text
              link
              type="primary"
              @click="deleteItem(row.MANUAL_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"
      :options="[]"
      :loading="false"
      @get-data="fnResetPagination"
    />
    <view-info
      v-model:visible="data.viewDialog.visible"
      :info="data.viewDialog.info"
      label-name="详情"
    />
  </div>
</template>

<script setup>
import { serialNumber } from "@/assets/js/utils";
import useListData from "@/assets/js/useListData.js";
import Add from "./components/add.vue";
import { nextTick, reactive } from "vue";
import ViewInfo from "./components/view.vue";
import { ElMessage, ElMessageBox } from "element-plus";
import {
  deleteManualView,
  getSafetyManualList,
  infoManualView,
} from "@/request/traffic_safety_manual.js";

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

const fnAddOrEdit = async (MANUAL_ID = "", type = "add") => {
  data.addOrEditDialog.visible = true;
  data.addOrEditDialog.type = type;
  if (type === "edit" && MANUAL_ID) {
    const resData = await infoManualView({ MANUAL_ID });
    if (resData && resData.pd) {
      data.addOrEditDialog.form = {
        ...resData.pd,
        OPERATINGCOMPANY: resData.pd.CORP_NAME,
        EXPIRYDATE: resData.pd.EXPIRYDATE,
      };
    }
  } else {
    data.addOrEditDialog.form = {};
  }
  await nextTick();
};

const fnView = async (MANUAL_ID) => {
  const resData = await infoManualView({
    MANUAL_ID,
  });
  data.viewDialog.info = resData.pd;
  data.viewDialog.visible = true;
};
// 删除事件
const deleteItem = async (value) => {
  await ElMessageBox.confirm(`确定要删除吗？`, {
    type: "warning",
  });
  await deleteManualView({ MANUAL_ID: value });
  ElMessage.success("删除成功");
  fnGetData();
};
</script>

<style scoped></style>
