﻿<template>
  <div class="mesSupplier-container">
    <el-card shadow="hover" :body-style="{ paddingBottom: '0' }">
      <el-form :model="queryParams" ref="queryForm" labelWidth="90">
        <el-row>
          <el-col :xs="24" :sm="12" :md="8" :lg="4" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
            <el-form-item label="供应商编码">
              <el-input v-model="queryParams.supplierCode" clearable="" placeholder="请输入供应商编码" />

            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="8" :lg="4" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
            <el-form-item label="供应商名称">
              <el-input v-model="queryParams.supplierName" clearable="" placeholder="请输入供应商名称" />

            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="8" :lg="4" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
            <el-form-item label="供应商简称">
              <el-input v-model="queryParams.supplierAs" clearable="" placeholder="请输入供应商简称" />

            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="8" :lg="4" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
            <el-form-item label="供应商等级">
              <el-input v-model="queryParams.lv" clearable="" placeholder="请输入供应商等级" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="8" class="mb10 ml10">
            <el-button-group>
              <el-button type="primary" icon="ele-Search" @click="handleQuery" v-auth="'mesSupplier/page'"> 查询
              </el-button>
              <el-button icon="ele-Refresh" @click="() => queryParams = {}"> 重置 </el-button>

            </el-button-group>

            <el-button-group style="margin-left:20px">
              <el-button type="primary" icon="ele-Plus" @click="openAddMesSupplier" v-auth="'mesSupplier/add'"> 新增
              </el-button>
            </el-button-group>
            <el-button-group style="margin-left:20px">
              <el-button type="primary" icon="ele-RefreshRight" @click="syncClick"> 同 步
              </el-button>
              <el-button type="primary" icon="ele-Histogram" @click="historySync">
              </el-button>
            </el-button-group>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <el-card class="full-table" shadow="hover" style="margin-top: 8px">
      <el-table :data="tableData" style="width: 100%" v-loading="loading" tooltip-effect="light" row-key="id" border="">
        <el-table-column type="index" label="序号" width="55" align="center" />
        <el-table-column prop="supplierCode" label="供应商编码" width="140" show-overflow-tooltip="" />
        <el-table-column prop="supplierName" label="供应商名称" width="140" show-overflow-tooltip="" />
        <el-table-column prop="supplierAs" label="供应商简称" width="140" show-overflow-tooltip="" />
        <el-table-column prop="supplierTypeName" label="供应商类型" width="140" show-overflow-tooltip="" />
        <el-table-column prop="lv" label="供应商等级" width="140" show-overflow-tooltip="" />
        <el-table-column prop="payAgreementName" label="采购委外收付款协议名称" width="165" show-overflow-tooltip="" />
        <el-table-column prop="legalName" label="法人" width="140" show-overflow-tooltip="" />
        <el-table-column prop="contactsName" label="联系人" width="140" show-overflow-tooltip="" />
        <el-table-column prop="contactsMobile" label="联系电话" width="140" show-overflow-tooltip="" />
        <el-table-column prop="account" label="账号" width="140" show-overflow-tooltip="" />
        <el-table-column prop="duty" label="税号" width="140" show-overflow-tooltip="" />
        <el-table-column prop="bank" label="开户行" width="140" show-overflow-tooltip="" />
        <el-table-column prop="expandDate" label="发展日期" width="140" show-overflow-tooltip="" />
        <el-table-column prop="businessName" label="专营业务员名称" width="105" show-overflow-tooltip="" />
        <el-table-column prop="invoiceTitle" label="发票抬头" width="140" show-overflow-tooltip="" />
        <el-table-column prop="invoiceAddress" label="开票地址" width="140" show-overflow-tooltip="" />
        <el-table-column prop="invoiceMobile" label="开票电话" width="140" show-overflow-tooltip="" />
        <el-table-column prop="address" label="地址" width="140" show-overflow-tooltip="" />
        <el-table-column prop="industry" label="行业" width="140" show-overflow-tooltip="" />
        <el-table-column prop="nature" label="企业性质" width="140" show-overflow-tooltip="" />
        <el-table-column prop="remark" label="备注" width="140" show-overflow-tooltip="" />
        <el-table-column label="操作" width="140" align="center" fixed="right" show-overflow-tooltip=""
          v-if="auth('mesSupplier/edit') || auth('mesSupplier/delete')">
          <template #default="scope">
            <el-button icon="ele-Edit" size="small" text="" type="primary" @click="openEditMesSupplier(scope.row)"
              v-auth="'mesSupplier/edit'"> 编辑 </el-button>
            <el-button icon="ele-Delete" size="small" text="" type="primary" @click="delMesSupplier(scope.row)"
              v-auth="'mesSupplier/delete'"> 删除 </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination v-model:currentPage="tableParams.page" v-model:page-size="tableParams.pageSize"
        :total="tableParams.total" :page-sizes="[10, 20, 50, 100, 200, 500]" small="" background=""
        @size-change="handleSizeChange" @current-change="handleCurrentChange"
        layout="total, sizes, prev, pager, next, jumper" />
      <printDialog ref="printDialogRef" :title="printMesSupplierTitle" @reloadTable="handleQuery" />
      <editDialog ref="editDialogRef" :title="editMesSupplierTitle" @reloadTable="handleQuery" />
      <syncProcess ref="syncProcessRef" />
    </el-card>
  </div>
</template>

<script lang="ts" setup="" name="mesSupplier">
import { ref } from "vue";
import { ElMessageBox, ElMessage } from "element-plus";
import { auth } from '/@/utils/authFunction';
import { getDictDataItem as di, getDictDataList as dl } from '/@/utils/dict-utils';
import { formatDate } from '/@/utils/formatTime';



import editDialog from '/@/views/mes/mesSupplier/component/editDialog.vue'
import { pageMesSupplier, deleteMesSupplier, getSuppliers } from '/@/api/mes/mesSupplier';
import syncProcess from '/@/views/mes/components/syncProcess/index.vue'
const syncProcessRef = ref();
const historySync = () => {
  syncProcessRef.value.openDialog();
}

const showAdvanceQueryUI = ref(true);
const printDialogRef = ref();
const editDialogRef = ref();
const loading = ref(false);
const tableData = ref<any>([]);
const queryParams = ref<any>({});
const tableParams = ref({
  page: 1,
  pageSize: 10,
  total: 0,
});

const printMesSupplierTitle = ref("");
const editMesSupplierTitle = ref("");

// 改变高级查询的控件显示状态
const changeAdvanceQueryUI = () => {
  showAdvanceQueryUI.value = !showAdvanceQueryUI.value;
}

const syncClick = () => {
  ElMessageBox.confirm(`确定要同步数据吗?`, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'success',
  })
    .then(async () => {
      await getSuppliers();
      ElMessage.success('开始同步');
    })
    .catch(() => { });
}

// 查询操作
const handleQuery = async () => {
  loading.value = true;
  var res = await pageMesSupplier(Object.assign(queryParams.value, tableParams.value));
  tableData.value = res.data.result?.items ?? [];
  tableParams.value.total = res.data.result?.total;
  loading.value = false;
};

// 打开新增页面
const openAddMesSupplier = () => {
  editMesSupplierTitle.value = '添加供应商档案';
  editDialogRef.value.openDialog({});
};

// 打开编辑页面
const openEditMesSupplier = (row: any) => {
  editMesSupplierTitle.value = '编辑供应商档案';
  editDialogRef.value.openDialog(row);
};

// 删除
const delMesSupplier = (row: any) => {
  ElMessageBox.confirm(`确定要删除吗?`, "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(async () => {
      await deleteMesSupplier(row);
      handleQuery();
      ElMessage.success("删除成功");
    })
    .catch(() => { });
};

// 改变页面容量
const handleSizeChange = (val: number) => {
  tableParams.value.pageSize = val;
  handleQuery();
};

// 改变页码序号
const handleCurrentChange = (val: number) => {
  tableParams.value.page = val;
  handleQuery();
};

handleQuery();
</script>
<style scoped>
:deep(.el-ipnut),
:deep(.el-select),
:deep(.el-input-number) {
  width: 100%;
}
</style>

