<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="110px">
      <el-form-item label="供应商名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入供应商名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商法人类型" prop="corporationType">
        <el-select
          v-model="queryParams.corporationType"
          placeholder="请选择类型"
          clearable
          @keyup.enter.native="handleQuery"
          style="width: 200px;"
        >
          <el-option
            v-for="item in supplier_corporation_type"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="search" @click="handleQuery">搜索</el-button>
        <el-button icon="refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="plus"
          @click="handleAdd"
          v-hasPermi="['business:supplier:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:supplier:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:supplier:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="download"
          @click="handleExport"
          v-hasPermi="['business:supplier:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="recordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="供应商名称" align="center" prop="name" />
      <el-table-column label="供应商评分" align="center" prop="score" />
      <el-table-column label="供应商规模" align="center" prop="scale" />
      <el-table-column label="供应商经营开始时间" align="center" prop="startTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="供应商法人类型" align="center" prop="corporationType" />
      <el-table-column label="供应商股票名称" align="center" prop="stockName" />
      <el-table-column label="是否小微企业" align="center" prop="smallVendors" />
      <el-table-column label="供应商注册号" align="center" prop="registNo" />
      <el-table-column label="供应商注册资本" align="center" prop="registCapital" />
      <el-table-column label="供应商登记机关" align="center" prop="registAuthority" />
      <el-table-column label="营业执照" align="center" prop="businessLicense" />
      <el-table-column label="营业地址" align="center" prop="businessAddress" />
      <el-table-column label="供应商注册行业" align="center" prop="registSector" />
      <el-table-column label="供应商核准时间" align="center" prop="approvalTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.approvalTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="中标信息" align="center" prop="winBidInfo" />
      <el-table-column label="合同信息" align="center" prop="contractInfo" />
      <el-table-column label="合同有效期" align="center" prop="expirationTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.expirationTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:supplier:edit']"
          >修改</el-button>
          <el-button
            link
            type="danger"
            icon="delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:supplier:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改供应商档案信息对话框 -->
    <el-dialog :title="title" v-model="open" width="1000px" append-to-body>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="160px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="供应商名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入供应商名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="供应商评分" prop="score">
              <el-input v-model="form.score" placeholder="请输入供应商评分" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="供应商规模" prop="scale">
              <el-select
                v-model="form.scale"
                placeholder="请选择规模"
                clearable
              >
                <el-option
                  v-for="item in supplier_scale"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="供应商经营开始时间" prop="startTime">
              <el-date-picker clearable
                v-model="form.startTime"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择供应商经营开始时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="供应商股票名称" prop="stockName">
              <el-input v-model="form.stockName" placeholder="请输入供应商股票名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="供应商法人类型" prop="corporationType">
              <el-select
                v-model="form.corporationType"
                placeholder="请选择类型"
                clearable
              >
                <el-option
                  v-for="item in supplier_corporation_type"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否小商企业" prop="smallVendors">
              <el-select
                v-model="form.smallVendors"
                placeholder="请选择"
                clearable
              >
                <el-option
                  v-for="item in [{ value: 0, label: '否' }, { value: 1, label: '是' }]"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="供应商注册号" prop="registNo">
              <el-input v-model="form.registNo" placeholder="请输入供应商注册号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="股票曾用名" prop="stockNameUsed">
              <el-input v-model="form.stockNameUsed" placeholder="请输入供应商股票曾用名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="供应商注册资本" prop="registCapital">
              <el-input v-model="form.registCapital" placeholder="请输入供应商注册资本" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="供应商登记机关" prop="registAuthority">
              <el-input v-model="form.registAuthority" placeholder="请输入供应商登记机关" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="供应商注册行业" prop="registSector">
              <el-input v-model="form.registSector" placeholder="请输入供应商注册行业" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="供应商核准时间" prop="approvalTime">
              <el-date-picker clearable
                v-model="form.approvalTime"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择供应商核准时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="中标信息" prop="winBidInfo">
              <el-input v-model="form.winBidInfo" placeholder="请输入中标信息" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="合同信息" prop="contractInfo">
              <el-input v-model="form.contractInfo" placeholder="请输入合同信息" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="合同有效期" prop="expirationTime">
              <el-date-picker clearable
                v-model="form.expirationTime"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择合同有效期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="营业执照" prop="businessLicense">
              <el-input v-model="form.businessLicense" placeholder="请输入营业执照" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="营业地址" prop="businessAddress">
              <el-input v-model="form.businessAddress" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="附件是否上传" prop="attachment">
              <el-select
                v-model="form.attachment"
                placeholder="请选择"
                clearable
              >
                <el-option
                  v-for="item in [{ value: 0, label: '否' }, { value: 1, label: '是' }]"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24" v-if="form.attachment">
            <el-form-item label="执照相片" prop="licenseImg">
              <image-upload
                :limit="1"
              >
              </image-upload>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import { listSupplierRecord, getSupplierRecord, delSupplierRecord, addSupplierRecord, updateSupplierRecord } from "@/api/supplier/record";
import ImageUpload from "@/components/ImageUpload";

import useForm from '@/hooks/useForm.js';

const defaultForm = {
  id: null,
  name: null,
  score: null,
  scale: null,
  stockName: null,
  stockNameUsed: null,
  startTime: null,
  corporationType: null,
  smallVendors: null,
  registNo: null,
  registCapital: null,
  registAuthority: null,
  registSector: null,
  approvalTime: null,
  winBidInfo: null,
  contractInfo: null,
  expirationTime: null,
  businessLicense: null,
  businessAddress: null,
  attachment: 0,
  licenseImg: null,
}

const defaultParams = {
  pageNum: 1,
  pageSize: 10,
  name: null,
  corporationType: null
}

const formRef = ref(null);
const queryRef = ref(null);

const { proxy } = getCurrentInstance()

const data = reactive({
  // 遮罩层
  loading: true,
  // 选中数组
  ids: [],
  // 非单个禁用
  single: true,
  // 非多个禁用
  multiple: true,
  // 显示搜索条件
  showSearch: true,
  // 总条数
  total: 0,
  // 分类表格数据
  recordList: [],
  // 弹出层标题
  title: "",
  // 是否显示弹出层
  open: false,
  // 表单校验
  rules: {
  }
})

const { loading, ids, single, multiple, showSearch, total, recordList, title, open, rules } = toRefs(data);

const { form, validateForm, resetForm } = useForm(formRef, defaultForm)
const { form: queryParams, resetForm: resetQueryForm } = useForm(queryRef, defaultParams)

const { supplier_scale, supplier_corporation_type } = proxy.useDict('supplier_scale', 'supplier_corporation_type')

getList();

function getList() {
  loading.value = true;
  listSupplierRecord(queryParams.value).then(response => {
    recordList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  resetForm();
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  resetQueryForm();
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids = selection.map(item => item.id)
  single = selection.length!==1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加档案";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const id = row.id || ids.value
  getSupplierRecord(id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改档案";
  });
}

/** 提交按钮 */
function submitForm() {
  validateForm.then(() => {
    if (form.value.id != null) {
      updateSupplierRecord(form.value).then(response => {
        proxy.$modal.msgSuccess("修改成功");
        open.value = false;
        getList();
      });
    } else {
      addSupplierRecord(form.value).then(response => {
        proxy.$modal.msgSuccess("新增成功");
        open.value = false;
        getList();
      });
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除分类编号为"' + ids + '"的数据项？').then(function() {
    return delSupplierRecord(ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('business/supplier/export', {
    ...queryParams.value
  }, `supplier-record_${new Date().getTime()}.xlsx`)
}
</script>
