<template>
  <div class="app-container new-box">
    <el-card>
      <!-- 搜索工作栏 -->
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <div class="filter-box">
          <div class="filter-lf">
            <el-form-item label="企业名称" prop="companyAme">
              <el-input v-model="queryParams.companyAme" placeholder="请输入供应商企业名称" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="法人代表" prop="legalPerson">
              <el-input v-model="queryParams.legalPerson" placeholder="请输入法人代表" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="联系方式" prop="phone">
              <el-input v-model="queryParams.phone" placeholder="请输入联系方式" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </div>
        </div>

      </el-form>

      <!-- 操作工具栏 -->
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['bs:supplier-company:create']">新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" :loading="exportLoading" v-hasPermi="['bs:supplier-company:export']">导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <!-- 列表 -->
      <el-table stripe border v-loading="loading" :data="list">
        <el-table-column label="企业名称" align="center" prop="companyAme" />
        <el-table-column label="供应商编码" align="center" prop="companyNumber" />
        <el-table-column label="企业性质" align="center" prop="companyType">
          <template v-slot="scope">
            <dict-tag :type="DICT_TYPE.BS_ENTERPRISEL_TYPE" :value="scope.row.companyType" />
          </template>
        </el-table-column>
        <el-table-column label="法人代表" align="center" prop="legalPerson" />
        <el-table-column label="联系方式" align="center" prop="phone" />
        <el-table-column label="统一社会信用代码" align="center" prop="creditCode" />
        <el-table-column label="银行户名" align="center" prop="bankName" />
        <el-table-column label="创建时间" align="center" prop="createTime" width="180">
          <template v-slot="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template v-slot="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['bs:supplier-company:update']">修改</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['bs:supplier-company:delete']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页组件 -->
      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize" @pagination="getList" />

    </el-card>
    <EditForm :enterpriseList="enterpriseList" :isOpen="isOpen" :id="id" @onClose="isOpen = false" @onConfirm="handleConfirm" />
  </div>
</template>

<script>
import { createSupplierCompany, updateSupplierCompany, deleteSupplierCompany, getSupplierCompany, getSupplierCompanyPage, exportSupplierCompanyExcel } from "@/api/procurement/supplierMsg";
import { listData } from "@/api/system/dict/data";
export default {
  name: "SupplierCompany",
  components: {
    EditForm: () => import('./components/editForm')
  },
  data() {
    return {
      id: null,
      isOpen: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 供应商信息列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        companyAme: null,
        companyNumber: null,
        companyType: null,
        legalPerson: null,
        phone: null,
        creditCode: null,
        address: null,
        businessStratDate: [],
        businessEndDate: [],
        capital: null,
        bankName: null,
        bankNumber: null,
        bankOfDeposit: null,
        isRegister: null,
        status: null,
        remark: null,
        files: null,
        createTime: [],
      },
      enterpriseList: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        companyAme: [{ required: true, message: "供应商企业名称不能为空", trigger: "blur" }],
        companyNumber: [{ required: true, message: "供应商编码不能为空", trigger: "blur" }],
        companyType: [{ required: true, message: "企业性质不能为空", trigger: "change" }],
        legalPerson: [{ required: true, message: "法人代表不能为空", trigger: "blur" }],
        phone: [{ required: true, message: "联系方式不能为空", trigger: "blur" }],
        creditCode: [{ required: true, message: "统一社会信用代码不能为空", trigger: "blur" }],
        address: [{ required: true, message: "企业地址不能为空", trigger: "blur" }],
        businessStratDate: [{ required: true, message: "营业期限(起)不能为空", trigger: "blur" }],
        businessEndDate: [{ required: true, message: "营业期限(止)不能为空", trigger: "blur" }],
        capital: [{ required: true, message: "注册资本(w)不能为空", trigger: "blur" }],
        bankName: [{ required: true, message: "银行户名不能为空", trigger: "blur" }],
        bankNumber: [{ required: true, message: "银行账号不能为空", trigger: "blur" }],
        bankOfDeposit: [{ required: true, message: "开户行名称不能为空", trigger: "blur" }],
        isRegister: [{ required: true, message: "是否注册不能为空", trigger: "blur" }],
        status: [{ required: true, message: "状态不能为空", trigger: "blur" }],
      }
    };
  },
  created() {
    this.handleInitList()
    this.getList();
  },
  methods: {
    handleInitList() {
      listData({
        pageNo: 1,
        pageSize: 50,
        dictType: 'supplier_enterprise_state',
      }).then((res) => {
        this.enterpriseList = res.data.list || []
      })
    },
    handleConfirm() {
      this.isOpen = false
      this.getList()
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      getSupplierCompanyPage(this.queryParams).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        companyAme: undefined,
        companyNumber: undefined,
        companyType: undefined,
        legalPerson: undefined,
        phone: undefined,
        creditCode: undefined,
        address: undefined,
        businessStratDate: undefined,
        businessEndDate: undefined,
        capital: undefined,
        bankName: undefined,
        bankNumber: undefined,
        bankOfDeposit: undefined,
        isRegister: undefined,
        status: undefined,
        remark: undefined,
        files: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      // this.reset();
      // this.open = true;
      // this.title = "添加供应商信息";
      this.id = null
      this.isOpen = true
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.id = row.id
      this.isOpen = true
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        // 修改的提交
        if (this.form.id != null) {
          updateSupplierCompany(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
          return;
        }
        // 添加的提交
        createSupplierCompany(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal.confirm('是否确认删除供应商信息编号为"' + id + '"的数据项?').then(function () {
        return deleteSupplierCompany(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = { ...this.queryParams };
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.$modal.confirm('是否确认导出所有供应商信息数据项?').then(() => {
        this.exportLoading = true;
        return exportSupplierCompanyExcel(params);
      }).then(response => {
        this.$download.excel(response, '供应商信息.xls');
        this.exportLoading = false;
      }).catch(() => { });
    }
  }
};
</script>

<style lang='scss' scoped>
@import '@/assets/styles/header/index.scss';
@import '@/assets/styles/common/index.scss';
</style>
