<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <!-- <el-form-item label="用章类型" prop="sealType">
        <el-select
          v-model="queryParams.sealType"
          placeholder="请选择用章类型"
          clearable
        >
          <el-option
            v-for="dict in seal_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item> -->
      <el-form-item label="用章人" prop="useName">
        <el-input
          style="width: 240px"
          v-model="queryParams.useName"
          placeholder="请输入用章人"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用章处室" prop="useDept">
        <el-input
          style="width: 240px"
          v-model="queryParams.useDept"
          placeholder="请输入用章处室"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="印章类型" prop="signetType">
        <el-select
          style="width: 240px"
          v-model="queryParams.signetType"
          placeholder="请选择印章类型"
          clearable
        >
          <el-option
            v-for="dict in signet_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!--      <el-form-item label="盖章份数" prop="signetCopies">
        <el-input
          style="width: 240px"
          v-model="queryParams.signetCopies"
          placeholder="请输入盖章份数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="创建时间" prop="doneTime">
        <el-date-picker
          style="width: 255px"
          v-model="dateRange"
          type="daterange"
          value-format="YYYY-MM-DD"
          range-separator="-"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
        />
      </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="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['offcial:seal:add']"
          >新增</el-button
        >
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['offcial:seal:remove']"
          >删除</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['offcial:seal:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="sealList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="序号"
        type="index"
        width="55"
        align="center"
        prop="id"
      />
      <!-- <el-table-column
        label="用章日期"
        align="center"
        prop="useTime"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.useTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="用章人" align="center" prop="useName" />
      <el-table-column label="合同名称" align="center" prop="usingReason" />
      <el-table-column label="用章处室" align="center" prop="useDept" />
      <!-- <el-table-column label="用户id" align="center" prop="userId" /> -->
      <el-table-column label="印章类型" align="center" prop="signetType">
        <template #default="scope">
          <dict-tag :options="signet_type" :value="scope.row.signetType" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="用章事由" align="center" prop="usingReason" /> -->
      <!-- <el-table-column label="主送单位" align="center" prop="activeSend" /> -->
      <el-table-column label="盖章份数" align="center" prop="signetCopies" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <!-- <el-table-column label="${comment}" align="center" prop="remark" /> -->
      <!-- <el-table-column label="用章类型" align="center" prop="sealType">
        <template #default="scope">
          <dict-tag :options="seal_type" :value="scope.row.sealType" />
        </template>
      </el-table-column> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="view"
            @click="handleView(scope.row)"
            >查看</el-button
          >
          <!-- <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['offcial:seal: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"
    />

    <sealApplicational
      ref="sealRefDialog"
      @guanbi="handleQuery"
    ></sealApplicational>
  </div>
</template>

<script setup name="Seal">
import {
  listSeal,
  getSeal,
  delSeal,
  addSeal,
  updateSeal,
} from "@/api/offcial/seal";
import sealApplicational from "@/views/liucheng/todowork/formView/sealApplicational.vue";

const { proxy } = getCurrentInstance();
const { signet_type, seal_type } = proxy.useDict("signet_type", "seal_type");

const sealList = ref([]);
const open = ref(false);
const dateRange = ref("");
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 data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    useTime: null,
    useName: null,
    useDept: null,
    userId: null,
    signetType: null,
    usingReason: null,
    activeSend: null,
    signetCopies: null,
    sealType: "2",
  },
  rules: {},
});

const { queryParams, form, rules } = toRefs(data);

/** 查询用章申请列表 */
function getList() {
  loading.value = true;
  listSeal(proxy.addDateRange(queryParams.value, dateRange.value)).then(
    (response) => {
      sealList.value = response.rows;
      total.value = response.total;
      loading.value = false;
    }
  );
}

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

// 表单重置
function reset() {
  form.value = {
    id: null,
    useTime: null,
    useName: null,
    useDept: null,
    userId: null,
    signetType: null,
    usingReason: null,
    activeSend: null,
    signetCopies: null,
    createBy: null,
    createTime: null,
    remark: null,
    sealType: "2",
  };
  proxy.resetForm("sealRef");
}
function handleView(row) {
  console.log(row);
  proxy.$refs.sealRefDialog.showDetail1(row);
  proxy.$refs.sealRefDialog.closeSendNext();
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  dateRange.value = [];
  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) {
  reset();
  const _id = row.id || ids.value;
  getSeal(_id).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改用章申请";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["sealRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != null) {
        updateSeal(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addSeal(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 delSeal(_ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "offcial/seal/exportHt",
    {
      ...queryParams.value,
    },
    `合同用章_${new Date().getTime()}.xlsx`
  );
}

getList();
</script>
