<template>
  <div>
    <MOSTY.PageTree :pageTitle="'食堂管理列表'" :searchType="'03'" @getDeptId="getdeptId">
      <template #serach>
        <el-divider />
        <MOSTY.Search :searchArr="searchArr" @submit="onSearch" />
        <el-divider />
        <div class="btnBox">
          <el-button type="primary" @click="addInfo" v-if="isAuth">
            <el-icon>
              <CirclePlus />
            </el-icon>
            <span>新增</span>
          </el-button>
          <el-button type="primary" @click="export2Excel" v-loading="loadingShow">
            <el-icon>
              <Download />
            </el-icon>
            <span>导出表格</span>
          </el-button>
        </div>
      </template>
      <template #table>
        <section style='height:calc(100vh - 380px)'>
          <MOSTY.Table :table-columns="tableColumn" :tableData="tableData" :tableloading="tableloading" :isSelect="true"
            :isSort="true" @SortChange="SortChange">
            <template #sign="{ row }">
              <p style=" width: 12px; height: 12px;border-radius: 50%;margin: 0 auto"
                :style="{ 'background-color': row?.sign ? '#33CC00' : '#FC5F76' }"></p>
            </template>
            <template #actions="{ row }">
              <span class="operedit" @click="handlexg(row)" v-if="isAuth">
                <el-icon>
                  <EditPen />
                </el-icon>编辑</span>
              <el-popconfirm v-if="isAuth" confirm-button-text="是" cancel-button-text="否" icon-color="red"
                title="确定要删除?" @confirm="delInfo(row)">
                <template #reference>
                  <span class="operdel" size="small">
                    <el-icon>
                      <Delete />
                    </el-icon>
                    删除</span>
                </template>
              </el-popconfirm>
              <span class="operesee" @click="handleSee(row)">
                <el-icon>
                  <View />
                </el-icon>查看</span>
            </template>
          </MOSTY.Table>
        </section>
        <div class="fenye">
          <MOSTY.Pages :pageConfiger="pageConfiger" @changeNo="handleCurrentChange" @changeSize="handleSizeChange" />
        </div>
      </template>
    </MOSTY.PageTree>
    <!-- 新增 -->
    <div v-if="comprehensiveShow" class="dialog-box">
      <Addstgl :formData="formData" :zdlist="{
        D_BZ_SF, D_BZ_XFZDBWHZLX, D_BZ_XFZDBWWXDJ, D_BZ_QLYY
        , D_TB_NHDJ, D_BZ_HZQK
      }" :orgObj="orgObj" @changePage="saveSuccess" />
    </div>

  </div>
</template>

<script setup>
// 引入
import { reactive, onMounted, ref, getCurrentInstance } from "vue"
import { exportXlsx } from "@/excel/Export2Excel.js"
import { ElMessage } from "element-plus"
import { getApi, postApi } from "@/api/tobAcco_api.js"
import * as MOSTY from "@/components/MyComponents/index"
import { useRouter } from "vue-router"
import { getItem } from "@/utils/storage"
import Addstgl from "./Addstgl.vue"
import { getAuth } from "@/utils/tools.js"
import { arrayFiltering } from "@/utils/tools.js"


const router = useRouter()
const { proxy } = getCurrentInstance()
const type = ref("add")
const {
  D_BZ_SF, D_BZ_XFZDBWHZLX, D_BZ_XFZDBWWXDJ, D_BZ_QLYY
  , D_TB_NHDJ, D_BZ_HZQK, D_TB_JCYWLX
} = proxy.$dict("D_BZ_SF",
  "D_BZ_XFZDBWHZLX", "D_BZ_XFZDBWWXDJ", "D_BZ_QLYY", "D_TB_NHDJ", "D_BZ_HZQK", "D_TB_JCYWLX")
const comprehensiveShow = ref(false)
//查询条件
const searchArr = reactive([
  {
    showType: "input",
    prop: "canteenName",
    width: "250px",
    placeholder: "请输入食堂名称进行查询"
  }
])
const loadingShow = ref(false)
//查询条件
const formData = ref({
  title: ""
})
const tableData = ref([])
// 分页
const listQuery = reactive({
  pageCurrent: 1,
  pageSize: 20
  // type: '3' // 消防重点部位
})
/** 当前需要编辑的rowid */
const rowId = ref()
let sign = ["canteenName", "siteName", "buildingName",  "partName"]

const tableColumn = ref([
  {
    title: "标记",
    prop: "sign",
    sortable: false,
    slot: true
  },
  {
    title: "食堂名称",
    prop: "canteenName",
    sortable: true
  },
  {
    title: "所属单位",
    prop: "orgName"
  },
  {
    title: "责任部门",
    prop: "responsibleDeptName",
    sortable: true
  },
  {
    title: "岗位负责人",
    prop: "responsibleUserName",
    sortable: true
  },

  {
    title: "操作",
    prop: "actions",
    slot: true
  }
])
const pageConfiger = reactive({
  pageSize: 20,
  pageCurrent: 1,
  total: 0
})
/** 部门信息 */
const orgObj = ref({})
const isAuth = ref(false)

onMounted(() => {
  isAuthorize()
})
// 是否可以操作
const isAuthorize = async () => {
  isAuth.value = await getAuth()
}
//获取部门数据
const getdeptId = (val) => {
  orgObj.value = {
    orgId: val.id,
    orgName: val.orgname
  }
  listQuery.orgId = val.id
  listQuery.pageCurrent = 1
  pageConfiger.pageCurrent = 1
  getListData()
}

function onSearch(obj) {
  listQuery.pageCurrent = 1
  pageConfiger.pageCurrent = 1
  listQuery.canteenName = obj.canteenName
  getListData()
}

//排序
function SortChange(prop, order) {
  if (prop == "partname") {
    listQuery.sortField = "id"
  } else {
    listQuery.sortField = prop
  }

  listQuery.sortOrder = order
  getListData()
}

//导出表格
const export2Excel = () => {
  loadingShow.value = true
  let title = {
    canteenName: "食堂名称",
    orgName: "所属单位",
    responsibleDeptName: "责任部门",
    responsibleUserName: "岗位负责人"
  }

  postApi(listQuery, "/mosty-jcgl/canteenInfo/queryCanteenInfoList").then(
    (res) => {
      if (res) {
        let data = res
        if (data) {
          let tablename = "食堂管理列表.xlsx"
          exportXlsx(title, data, "食堂管理列表", tablename)
        } else {
          ElMessage.warning("暂无数据")
        }
      }
    }
  ).finally(() => {
    loadingShow.value = false
  })
}
// 获取表格数据
const tableloading = ref(false)

function getListData() {
  tableloading.value = true///canteenInfo/queryCanteenInfoPage
  postApi(listQuery, `/mosty-jcgl/canteenInfo/queryCanteenInfoPage`).then(
    (res) => {
      tableData.value = []
      if (res.records) {
        tableData.value = arrayFiltering(res.records, sign)
        pageConfiger.total = res.total
      }
    }
  ).finally(() => {
    tableloading.value = false
  })
}


//新增
function addInfo() {
  formData.value = {}
  type.value = "add"
  formData.value.deptname = getItem("deptId").ssbmmc
  formData.value.deptid = getItem("deptId").ssbmid
  orgObj.value = {
    orgId: formData.value.deptname,
    orgName: formData.value.deptid
  }
  formData.value.title = "食堂管理新增"
  formData.value.saveBtnShow = true
  comprehensiveShow.value = true
}

// 新增成功
const saveSuccess = () => {
  formData.value = {}
  comprehensiveShow.value = false
  getListData()
}

//修改
function handlexg(row) {
  type.value = "exit"
  // formData.value.id = row.id;
  formData.value = { ...row }
  rowId.value = row.id
  formData.value.title = "食堂管理修改"
  formData.value.saveBtnShow = true
  comprehensiveShow.value = true
}

//详情
function handleSee(row) {
  type.value = "see"
  formData.value = { ...row }
  formData.value.id = row.id
  formData.value.title = "食堂管理详情"
  formData.value.saveBtnShow = false
  comprehensiveShow.value = true
}

//删除
function delInfo(row) {
  postApi(
    [row.id],
    `/mosty-jcgl/canteenInfo/delCanteenInfo`
  ).then((res) => {
    ElMessage.success("删除成功")
    getListData()
  })
}

//分页
const handleSizeChange = (currentSize) => {
  listQuery.pageSize = currentSize
  pageConfiger.pageSize = currentSize
  getListData()
}
const handleCurrentChange = (currentPage) => {
  listQuery.pageCurrent = currentPage
  pageConfiger.pageCurrent = currentPage
  getListData()
}

</script>

<style lang="scss" scoped>
.btnBox {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  margin-bottom: 12px;
}
</style>
