<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label-width="70px" label="督办事项" prop="title">
        <el-input
          style="width: 240px"
          v-model="queryParams.title"
          placeholder="请输入督办事项"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <!-- <el-form-item label="议定事项" prop="itemPostil">
        <el-input
          style="width: 240px"
          v-model="queryParams.itemPostil"
          placeholder="请输入议定事项"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="责任处室" prop="prersonLiable">
        <el-input
          style="width: 240px"
          v-model="queryParams.prersonLiable"
          placeholder="请输入责任处室"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="进度评价" prop="progressEva">
        <el-select
          v-model="queryParams.progressEva"
          placeholder="请选择进度评价"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in progress_eva"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="是否催办" prop="isCb">
        <el-select
          placeholder="请选择是否催办"
          style="width: 240px"
          v-model="queryParams.isCb"
          clearable
        >
          <el-option
            v-for="dict in sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </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="success" plain icon="Select" @click="submitSelect"
          >提交</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain icon="plus" @click="addDuban"
          >添加</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['offcial:supervision:expediting']"
          type="danger"
          plain
          icon="Position"
          @click="piliang"
          >催办</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['offcial:supervision:export']"
          >导出</el-button
        >
      </el-col>

      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row> -->

    <el-table
      v-loading="loading"
      :data="supervisionList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        :selectable="isRowSelectable"
        width="55"
        align="center"
      ></el-table-column>
      <el-table-column width="50" label="序号" align="center" type="index" />
      <el-table-column label="督办事项" align="center" prop="title" />
      <!-- <el-table-column label="信息类别" align="center" prop="infoType" /> -->
      <!-- <el-table-column label="信息来源类型" align="center" prop="infoSource" /> -->
      <!-- <el-table-column label="议定事项" align="center" prop="itemPostil" /> -->
      <el-table-column label="责任处室" align="center" prop="prersonLiable" />

      <!-- <el-table-column label="进展情况" align="center" prop="progress" /> -->
      <el-table-column
        width="80"
        label="进度评价"
        align="center"
        prop="progressEva"
      >
        <template #default="scope">
          <dict-tag :options="progress_eva" :value="scope.row.progressEva" />
        </template>
      </el-table-column>
      <el-table-column label="发起人" align="center" prop="departmentHead" />
      <!-- <el-table-column label="工作id" align="center" prop="workId" /> -->
      <!-- <el-table-column label="完成时限内容" align="center" prop="doneContent" /> -->
      <el-table-column width="80" label="是否催办" align="center" prop="isCb">
        <template #default="scope">
          <dict-tag :options="sys_yes_no" :value="scope.row.isCb" />
        </template>
      </el-table-column>
      <el-table-column
        width="165"
        label="创建时间"
        align="center"
        prop="createTime"
      >
        <template #default="scope">
          <span>{{
            parseTime(scope.row.createTime, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column
        label="信息来源内容"
        align="center"
        prop="infoSourceContent"
      /> -->
      <el-table-column
        label="操作"
        align="center"
        width="210"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="List"
            @click="openPdfViewer(scope.row)"
            >处理</el-button
          >
          <!-- <el-button
            v-if="scope.row.workId != null"
            link
            type="warning"
            icon="view"
            @click="handleView(scope.row)"
            >查看</el-button
          > -->

          <!-- <el-button
            v-else
            link
            type="primary"
            icon="edit"
            @click="handleUpdate(scope.row)"
            >编辑</el-button
          > -->
          <!-- <el-button
            v-if="scope.row.workId != null && scope.row.isCb != 'Y'"
            v-hasPermi="['offcial:supervision:expediting']"
            link
            type="danger"
            icon="Position"
            @click="handleUrge(scope.row)"
            >催办</el-button
          > -->
          <!-- <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['offcial:supervision: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"
    />

    <Supervise
      :showSub="showSub"
      ref="superRef"
      @guanbi="handleQuery"
    ></Supervise>
    <el-dialog
      v-model="dialogVisibleSupervise"
      title="督办事项"
      width="80%"
      :before-close="handleClose"
    >
      <SuperviseEdit
        :idNow="idNow"
        ref="SuperviseEditRef"
        @guanbi="handleQuery"
      ></SuperviseEdit>
    </el-dialog>
    <el-dialog
      v-model="dialogVisibleSuperviseAdd"
      title="督办事项"
      width="80%"
      :before-close="handleClose"
    >
      <SuperviseAdd ref="SuperviseAddRef" @guanbi="handleQuery"></SuperviseAdd>
    </el-dialog>
  </div>
</template>

<script setup name="Supervision">
import {
  listSupervision,
  getSupervision,
  delSupervision,
  addSupervision,
  updateSupervision,
  myselfDetail,
  plcuiban,
  submitSupervision,
  dblist,
} from "@/api/offcial/supervision";
import Supervise from "@/views/liucheng/todowork/formView/newSupervise.vue";
import SuperviseEdit from "@/views/clocking/SuperviseTijiao.vue";
import SuperviseAdd from "@/views/clocking/Supervise.vue";

const { proxy } = getCurrentInstance();

const supervisionList = ref([]);
const open = ref(false);
const dialogVisibleSupervise = ref(false);
const dialogVisibleSuperviseAdd = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const showSub = ref(true);
const total = ref(0);
const idNow = ref(null);
const dateRange = ref("");
const title = ref("");
const { sys_yes_no, progress_eva } = proxy.useDict(
  "sys_yes_no",
  "progress_eva"
);
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    title: null,
    infoType: null,
    infoSource: null,
    itemPostil: null,
    prersonLiable: null,
    doneTime: null,
    progress: null,
    progressEva: null,
    departmentHead: null,
    workId: null,
    doneContent: null,
    isCb: null,
    infoSourceContent: null,
    orderByColumn: "createTime",
    isAsc: "Desc",
  },
  rules: {},
});

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

function openPdfViewer(data) {
  proxy.$refs.superRef.showDetail1(data);
}
function addDuban() {
  dialogVisibleSuperviseAdd.value = true;
}
function handleEdit(row) {
  console.log("row", row);
}
function isRowSelectable(row) {
  // 只有状态为'active'的行可选
  return row.workId === null;
}
function submitSelect() {
  if (ids.value != "" && ids.value != null) {
    proxy.$modal.loading("提交中...");
    submitSupervision(ids.value)
      .then((res) => {
        proxy.$modal.msgSuccess("提交成功");
        handleQuery();
        proxy.$modal.closeLoading();
      })
      .catch(() => {
        proxy.$modal.closeLoading();
      });
  } else {
    proxy.$modal.msgError("请选择要提交的督办事项");
  }
}
function handleView(data) {
  console.log(data);
  proxy.$refs.superRef.showDetail1(data);
  proxy.$refs.superRef.closeSendNext();
}
/** 查询督办列表 */
function getList() {
  loading.value = true;
  dblist(proxy.addDateRange(queryParams.value, dateRange.value)).then(
    (response) => {
      supervisionList.value = response.rows;
      total.value = response.total;
      loading.value = false;
    }
  );
}

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

// 表单重置
function reset() {
  form.value = {
    id: null,
    title: null,
    infoType: null,
    infoSource: null,
    itemPostil: null,
    prersonLiable: null,
    doneTime: null,
    progress: null,
    progressEva: null,
    departmentHead: null,
    createBy: null,
    createTime: null,
    remark: null,
    workId: null,
    doneContent: null,
    isCb: null,
    infoSourceContent: null,
  };
  proxy.resetForm("supervisionRef");
}

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

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  dateRange.value = [];
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  console.log(selection);
  ids.value = selection.map((item) => item.id).join(",");

  console.log(ids.value);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加督办";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  dialogVisibleSupervise.value = true;
  reset();
  const _id = row.id || ids.value;
  this.idNow = _id;
}

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

function piliang(row) {
  const _ids = row.id || ids.value;
  console.log(row);
  proxy.$modal
    .confirm("是否催办已选督办事项？")
    .then(function () {
      return plcuiban(_ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("批量催办成功");
    })
    .catch(() => {
      proxy.$modal.msgError("批量催办失败");
    });
}
/** 催办按钮操作 */
function handleUrge(row) {
  const _ids = row.id || ids.value;
  console.log(row);
  proxy.$modal
    .confirm("是否催办该督办事项？")
    .then(function () {
      return myselfDetail(_ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("催办成功");
    })
    .catch(() => {
      proxy.$modal.msgError("催办失败");
    });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "offcial/supervision/export",
    {
      ...queryParams.value,
    },
    `督办数据_${new Date().getTime()}.xlsx`
  );
}

getList();
</script>
