<template>
  <MOSTY.PageTree :pageTitle="'食品经营许可证信息列表'" :searchType="'01'" @getDeptId="getdeptId">
    <template #serach>
      <el-divider />
      <MOSTY.Search :searchArr="searchArr" @submit="onSearch" />
    </template>
    <template #table>
      <el-divider />
      <div class="btnBox">
        <el-button @click="addInfo('add', null)" v-if='isAuth'><el-icon size="small">
            <Plus />
          </el-icon>新增</el-button>
      </div>
      <section class="eltab9">
        <MOSTY.Table :table-columns="tableColumn" :tableData="tableData" :tableloading="tableloading" :isSelect="false"
          :isSort="false" @SortChange="SortChange" style="width: 100%;">
          <!-- 操作 -->
          <template #actions="{ row }">
            <span class="operedit" v-if='isAuth' @click="addInfo('exit', row.id)" size="small"><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="addInfo('see', row.id)" size="small"><el-icon>
                <View />
              </el-icon>查看</span>
          </template>
        </MOSTY.Table>
      </section>
      <div class="fenye">
        <MOSTY.Pages :pageConfiger="pageConfiger" @changeNo="handleCurrentChange" @changeSize="handleSizeChange">
        </MOSTY.Pages>
      </div>
    </template>
  </MOSTY.PageTree>
  <!-- 新增 -->
  <div v-if="comprehensiveShow" class="dialog-box">
    <Addcyfw :saveBtnShow="saveBtnShow" :editObj="editObj" @close="close" :title="editObj.title" />
  </div>
</template>

<script setup>
// 引入
import { reactive, onMounted, ref } from "vue";
import Addcyfw from "@/views/yxkzgl/stgl/commonds/addcyfw.vue";
import { getAuth } from "@/utils/tools.js";
import { getApi, postApi } from "@/api/tobAcco_api.js";
import * as MOSTY from "@/components/MyComponents/index";
import { getItem } from "@/utils/storage";
import { ElMessage } from "element-plus";
const comprehensiveShow = ref(false);
//查询条件
const searchArr = reactive([
  {
    showType: "input",
    prop: "issuingagencies",
    width: '250px',
    placeholder: '请输入颁发机关名称进行查询'
  }
]);
const editObj = reactive({
  deptname: "",
  title: "",
  savebtn: true,
  seeimg: true,
});
const tableData = ref([]);
// 分页
const listQuery = reactive({
  pageCurrent: 1,
  pageSize: 20,
  orgcode: ""
});
const tableColumn = ref([
  {
    title: "颁发机关",
    prop: "issuingagencies",
  },
  {
    title: "颁发日期",
    prop: "issuingdate"
  },
  {
    title: "法定代表人",
    prop: "legalperson"
  },
  {
    title: "发证日期",
    prop: "validstartdate"
  },
  {
    title: "有效结束日期",
    prop: "validenddate",
  },
  {
    title: "地址",
    prop: "address"
  },
  {
    title: "操作",
    prop: "actions",
    slot: true
  }
])
const pageConfiger = reactive({
  pageSize: 20,
  pageCurrent: 1,
  total: 0
})
const isAuth = ref(false)
//获取部门数据
onMounted(() => {
  isAuthorize()
});
// 是否可以操作
const isAuthorize = async () => {
  isAuth.value = await getAuth()
}
const getdeptId = (val) => {
  listQuery.orgcode = val.id;
  listQuery.pageCurrent = 1;
  pageConfiger.pageCurrent = 1;
  getListData();
};

// 获取表格数据
const tableloading = ref(false);
function getListData() {
  tableloading.value = true;
  editObj.deptname = getItem("deptId").ssbmid;
  postApi(listQuery, `/mosty-jcgl/foodsStorage/queryFoodsStorage`).then(
    (res) => {
      if (res.records) {
        tableloading.value = false;
        tableData.value = res.records;
        pageConfiger.total = res.total;
        listQuery.issuingagencies = ''
      } else {
        tableData.value = [];
        tableloading.value = false;
      }
    }
  ).finally(() => {
    tableloading.value = false;
  });
}
//弹窗关闭
const close = () => {
  comprehensiveShow.value = false;
  getListData();
};


const saveBtnShow = ref(true);
//新增
function addInfo(type, id) {
  if (type == 'add') {
    editObj.id = null;
    saveBtnShow.value = true;
    editObj.title = "食品经营许可证新增";
  } else if (type == 'exit') {
    editObj.title = "食品经营许可证修改";
    editObj.id = id
    saveBtnShow.value = true;
  } else {
    editObj.title = "食材供应商详情";
    editObj.id = id
    saveBtnShow.value = false;
  }
  comprehensiveShow.value = true;
}

//删除
function delInfo(row) {
  postApi({}, "/mosty-jcgl/foodsStorage/deleteFoodsStorageOne/" + row.id).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();
};
function onSearch(obj) {
  listQuery.issuingagencies = obj.issuingagencies;
  listQuery.pageCurrent = 1;
  getListData();
}


</script>

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