<template>
  <div class="overall">
    <div class="titleBox">
      <div class="title">责任书管理</div>
      <div class="btnBox">
        <el-button type="primary" @click.stop="handelAddEdit('add', '')">
          <el-icon>
            <CirclePlus />
          </el-icon>
          <span>网签新增</span>
        </el-button>
        <el-button type="primary" @click.stop="handelAddEdit('offLineAdd', '')">
          <el-icon>
            <CirclePlus />
          </el-icon>
          <span>线下签新增</span>
        </el-button>
        <el-button type="primary" @click.stop="batchDelete">
          <el-icon>
            <Delete />
          </el-icon>
          <span>批量删除</span>
        </el-button>
        <el-popconfirm confirm-button-text="是" cancel-button-text="否" icon-color="#1983E5" title="确定导出数据?"
          @confirm="getExcelData()">
          <template #reference>
            <el-button type="primary" v-loading="loadingshow">
              <el-icon>
                <Download />
              </el-icon>
              <span>导出Excel</span>
            </el-button>
          </template>
        </el-popconfirm>
      </div>
    </div>
    <div class="searchBox" ref="searchBox">
      <el-form :model="listQuery" class="mosty-from-wrap" :inline="true">
        <el-form-item label="年份">
          <el-date-picker v-model="timeValue" type="year" placeholder="请选择时间" style="margin-right: 15px" />
        </el-form-item>
        <el-form-item label="名称">
          <el-input placeholder="请输入责任书名称" v-model="listQuery.zrsmc" clearable></el-input>
        </el-form-item>
        <el-form-item label="类型">
          <el-select v-model="listQuery.zrslx" placeholder="请选择责任书类型">
            <el-option v-for="item in D_TB_ZRSLX" :key="item" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="签订方式">
          <el-select v-model="listQuery.signtype" placeholder="请选择签订方式">
            <el-option v-for="item in D_BZ_ZRSQDFS" :key="item" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="handleFilter">查询</el-button>
          <el-button @click="reset()"> 重置 </el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="main-box">
      <div class="treeBox" :style="{ height: treeHeight }">
        <MOSTY.DepartmentTree width="280px" placeholder="管理部门ID" clearable filterable v-model="listQuery.fqbmid" />
      </div>
      <div class="tabBox">
        <el-table :data="tableData" border stripe v-loading="loading" style="width: 100%" :height="tableHeight"
          :key="keyCount" @selection-change="handleSelectionChange">
          <el-table-column type="selection" :selectable="selectable" width="55" />
          <el-table-column prop="zrsmc" align="center" width="220" show-overflow-tooltip
            label="责任书名称"></el-table-column>
          <el-table-column prop="idEntityCard" align="center" label="责任书类型" width="160">
            <template #default="{ row }">
              <dict-tag :options="D_TB_ZRSLX" :value="row.zrslx" :tag="false" />
            </template>
          </el-table-column>
          <el-table-column width="160" label="发起单位">
            <template #default="{ row }">
              <span v-if="row.fqxgsmc">{{ row.fqxgsmc }}</span>
              <span v-else-if="row.fqsgsmc">{{ row.fqsgsmc }}</span>
              <span v-else>{{ store.getters.defaultDeptName }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="fqbmmc" width="160" align="center" label="发起部门">
          </el-table-column>
          <el-table-column prop="rybmmc" width="160" align="center" label="被签订单位/部门"></el-table-column>
          <el-table-column prop="ldxm" width="100" align="center" label="审批人"></el-table-column>
          <el-table-column prop="qdsj" width="160" align="center" label="签订时间"></el-table-column>
          <el-table-column prop="mobile" width="100" align="center" label="签订方式">
            <template #default="{ row }">
              <dict-tag :options="D_BZ_ZRSQDFS" :value="row.signtype" :tag="false" />
            </template>
          </el-table-column>
          <el-table-column prop="ryxm" width="100" align="center" label="签订人"></el-table-column>
          <el-table-column prop="mobile" width="100" align="center" label="签订状态">
            <template #default="{ row }">
              <dict-tag :options="D_TB_ZRSQDZT" :value="row.qdzt" :tag="false" />
            </template>
          </el-table-column>
          <el-table-column align="right" label="操作" fixed="right" width="200">
            <template #default="{ row }">
              <el-button @click.stop="handelAddEdit('edit', row)" size="small" :disabled="row.isDelete ? false : true"
                v-if="row.signtype == 1">修改</el-button>
              <el-dropdown style="margin-left: 12px; margin-right: 12px">
                <el-button style="" size="small" @click.stop="handelAddEdit('detail', row)">
                  详情
                </el-button>
              </el-dropdown>
              <el-popconfirm confirm-button-text="是" cancel-button-text="否" icon-color="red" title="确定要删除?"
                @confirm="delRole(row)">
                <template #reference>
                  <el-button type="danger" :disabled="row.isDelete ? false : true" size="small">删除</el-button>
                </template>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <div class="fenye" :style="{ top: tableHeight + 'px' }">
          <el-pagination class="pagination" @size-change="handleSizeChange" @current-change="handleCurrentChange"
            :current-page="listQuery.page" :page-sizes="[2, 5, 10, 20]" :page-size="listQuery.size"
            layout="total, prev, pager, next, sizes, jumper" :total="total"></el-pagination>
        </div>
      </div>
    </div>
    <!-- <AddDutyOnline ref="DutyOnline" @getData="handleFilter" />
    <AddDutyOffline ref="DutyOffline" @addSuccess="handleFilter" /> -->
  </div>
</template>

<script setup>
import * as MOSTY from "@/components/MyComponents/index";
import * as XLSX from "xlsx";
// import AddDutyOffline from "./components/addDutyOffline.vue";
// import AddDutyOnline from "./components/addDutyOnline.vue";
import * as rule from "@/utils/rules.js";
import { ElMessage } from "element-plus";
import {
  ref,
  reactive,
  computed,
  watch,
  onMounted,
  getCurrentInstance,
  onUnmounted
} from "vue";
import { getApi, deleteApi } from "@/api/tobAcco_api.js";
import { dateFormat, exportXlsx } from "@/utils/tools.js";
import { useStore } from "vuex";
const { proxy } = getCurrentInstance();
const title = ref("新增责任书");
const loadingshow = ref(false)
const store = useStore();
const timeValue = ref(dateFormat());
const searchBox = ref(null); // 搜索盒子
const keyCount = ref(0); //tabel组件刷新值
const tableHeight = ref(); // 表格高度
const treeHeight = ref(""); // 树高度
const rules = ref({});
const elform = ref(null);
const showOnline = ref(false); //线上表单
const showOffline = ref(false); //线下表单
const updateId = ref(""); //
const DutyOnline = ref(null); //网签组件
const DutyOffline = ref(null); //线下组件
// 数据相关
const batchIds = ref([]); //批量选中的数据ID
const tableData = ref([]);
const dialogFormVisible = ref(true);
const loading = ref(false);
//用户所属部门前4位
const userDept = JSON.parse(
  window.localStorage.getItem("deptId")
).ssbmid.substring(0, 4);
const listQuery = ref({
  pageSize: 20,
  pageCurrent: 1
});
const total = ref(0);
const currentUserId = ref("");
const roleDialogVisible = ref(false); //是否现实分配角色
const { D_TB_ZRSQDZT, D_TB_ZRSLX, D_BZ_ZRSQDFS } = proxy.$dict(
  "D_TB_ZRSQDZT",
  "D_TB_ZRSLX",
  "D_BZ_ZRSQDFS"
);
watch(
  () => listQuery.value.fqbmid,
  (newVal) => {
    listQuery.value.pageCurrent = 1;
    _getZrsList();
  },
  { deep: true }
);
onMounted(() => {
  tabHeightFn();
  treeHeightFn();
  window.onresize = function () {
    treeHeightFn();
    tabHeightFn();
  };
  _getZrsList();
});

//获取导出表格数据
const getExcelData = () => {
  loadingshow.value = true
  let obj = {
    pageSize: 100000,
    pageCurrent: 1,
    year: dateFormat({ time: timeValue.value }).substring(0, 4),
  }
  getApi(obj, "/mosty-jcgl/zrs/getList").then((res) => {
    let title = {
      zrsmc: "责任书名称",
      zrslxmc: "责任书类型",
      fqbmmc: "发起部门",
      rybmmc: "被签订单位/部门",
      ldxm: "审批人",
      qdsj: "签订时间",
      signtypemc: "签订方式",
      ryxm: "签订人",
      qdztmc: "签订状态"
    };
    if (res.length > 0) {
      for (let i = 0; i < res.length; i++) {
        res[i].zrslxmc = proxy.$getDictVal(res[i].zrslx, D_TB_ZRSLX);
        res[i].signtypemc = proxy.$getDictVal(res[i].signtype, D_BZ_ZRSQDFS);
        res[i].qdztmc = proxy.$getDictVal(res[i].qdzt, D_TB_ZRSQDZT);
      }
      exportXlsx(title, res, XLSX, "责任书");
      loadingshow.value = false
    }
  }).catch(err => {
    loadingshow.value = false
  });
};
//判断当前表单数据
const selectable = (row) => {
  if (row.isDelete) {
    return true;
  } else {
    return false;
  }
};
//重置
const reset = () => {
  listQuery.value = {
    pageSize: 20,
    pageCurrent: 1
  };
  _getZrsList();
};
//批量选中的数据
const handleSelectionChange = (selection) => {
  batchIds.value = selection.map((item) => {
    return item.id;
  });
};
//批量删除
const batchDelete = () => {
  if (batchIds.value.length <= 0) {
    proxy.$message.warning("请先选择数据");
    return;
  }
  deleteApi({ ids: batchIds.value }, "/mosty-jcgl/zrs/delBatchs").then(
    (res) => {
      proxy.$message.success("删除成功");
      handleFilter();
    }
  );
};
//单个删除
const delRole = (row) => {
  batchIds.value = [row.id];
  batchDelete();
};
//点击修改按钮
const handelAddEdit = (type, row) => {
  if (row.signtype == "1" || type == "add") {
    DutyOnline.value.init(type, row);
  } else {
    DutyOffline.value.init(type, row);
  }
};
//查询责任书数据
const _getZrsList = () => {
  loading.value = true;
  tableData.value = [];
  listQuery.value.year = dateFormat({ time: timeValue.value }).substring(0, 4);
  getApi(listQuery.value, "/mosty-jcgl/zrs/getPageList")
    .then((res) => {
      loading.value = false;
      if (res && res.records.length > 0) {
        //处理用户能否删除责任书
        for (let i = 0; i < res.records.length; i++) {
          let deptCode = res.records[i].id.substring(0, 4);
          if (userDept == deptCode) res.records[i].isDelete = true;
        }
        tableData.value = res.records;
      }
      total.value = res.total;
    })
    .catch((err) => {
      loading.value = false;
    });
};
//查询按钮
const handleFilter = () => {
  listQuery.value.pageCurrent = 1;
  listQuery.value.zrsmc = "";
  _getZrsList();
};
//页码发生变化
const handleCurrentChange = (val) => {
  listQuery.value.pageCurrent = val;
  _getZrsList();
};
//查询条数发生变化
const handleSizeChange = (val) => {
  listQuery.value.pageSize = val;
  _getZrsList();
};
// 表格高度计算
const tabHeightFn = () => {
  tableHeight.value =
    window.innerHeight - 57 - 37 - searchBox.value.offsetHeight - 130;
};
// 树高度计算
const treeHeightFn = () => {
  treeHeight.value =
    window.innerHeight - 57 - 37 - searchBox.value.offsetHeight - 93 + "px";
};
</script>

<style lang="scss" scoped>
@import "~@/assets/css/layout.scss";
@import "~@/assets/css/element-plus.scss";
</style>
