<template>
  <MOSTY.PageTree :pageTitle="null" :isShowLeftTree="false">
    <template #serach>
      <el-divider />
      <MOSTY.Search :searchArr="searchArr" @submit="onSearch" />
    </template>
    <template #table>
      <el-divider />
      <div class="flxeR">
        <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">
    <Scgysxq v-if="comprehensiveShow" :saveBtnShow="saveBtnShow" :canteenInfoId="listQuery.canteenInfoId" :rowId="rowId" @close="close" :title="editObj.title" />
  </div>
</template>

<script setup>
// 引入
import { reactive, onMounted, ref,defineProps } from "vue";
import Scgysxq from "@/views/yxkzgl/stgl/commonds/scgysxq.vue";
import { getApi,postApi } from "@/api/tobAcco_api.js";
import { getAuth } from "@/utils/tools.js";
import * as MOSTY from "@/components/MyComponents/index";
import { getItem } from "@/utils/storage";
import { ElMessage } from "element-plus";
const props = defineProps({
  obj: {
    type: Object,
    default: {}
  }
})

const comprehensiveShow = ref(false);
const rowId = ref(null)
//查询条件
const searchArr = reactive([

  {
    showType: "input",
    prop: "suppliername",
    width: '250px',
    placeholder: '请输入食材供应商名称进行查询'
  }
]);
const editObj = reactive({
  deptname: "",
  title: "",
  savebtn: true,
  seeimg: true
});
const tableData = ref([]);
// 分页
const listQuery = reactive({
  pageCurrent: 1,
  pageSize: 20,
  orgcode: "",
  canteenInfoId: props.obj && props.obj.id,
});
const tableColumn = ref([
  {
    title: "供应商名称",
    prop: "suppliername",
  },
  {
    title: "法人名称",
    prop: "legalperson"
  },
  {
    title: "法人身份证",
    prop: "legalpersonidcard"
  },
  {
    title: "法人电话",
    prop: "legalpersonphone"
  },
  {
    title: "供应商联系人",
    prop: "suppliercontact",
  },
  {
    title: "供应商联系电话",
    prop: "supplierphone"
  },
  {
    title: "操作",
    prop: "actions",
    slot: true
  }
])
const pageConfiger = reactive({
  pageSize: 20,
  pageCurrent: 1,
  total: 0
})
const isAuth = ref(false)
onMounted(() => {
  isAuthorize()
  getListData();
});
// 是否可以操作
const isAuthorize = async () => {
  isAuth.value = await getAuth()
}
//获取部门数据
const getdeptId = (val) => {
  listQuery.orgcode = val.id;
  listQuery.pageCurrent = 1;
  pageConfiger.pageCurrent = 1;
  getListData();
};
const onSearch = (obj) => {
  listQuery.suppliername = obj.suppliername;
  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/ingredientsSupplier/queryIngredientsSupplier`
  ).then((res) => {
    if (res.records) {
      tableloading.value = false;
      tableData.value = res.records;
      pageConfiger.total = res.total;
      listQuery.suppliername = ''
    } 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') {
    rowId.value = null;
    saveBtnShow.value = true;
    editObj.title = "食材供应商新增";
  } else if (type == 'exit') {
    rowId.value = id
    editObj.title = "食材供应商修改";
    saveBtnShow.value = true;

  } else {
    rowId.value = id
    saveBtnShow.value = false;
    editObj.title = "食材供应商详情";
  }
  comprehensiveShow.value = true;

}

//删除
function delInfo(row) {
  postApi(
    {},
    "/mosty-jcgl/ingredientsSupplier/deleteIngredientsSupplierOne/" + 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();
};
</script>

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