<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true" label-width="120px">
            <el-form-item label="服务商ID" prop="serviceId">
              <el-select v-model="queryParams.serviceId" placeholder="请输入归属模块">
                <el-option v-for="dict in serviceList" :key="dict.serviceId" :label="dict.serviceName" :value="dict.serviceId"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="签章类型编码" prop="signNum">
              <el-input v-model="queryParams.signNum" placeholder="请输入签章类型" clearable @keyup.enter="handleQuery" />
            </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-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-button v-hasPermi="['print:typeConfig:add']" type="primary" icon="Plus" @click="handleAdd">新增</el-button>
          <right-toolbar v-model:showSearch="showSearch" @query-table="getList"></right-toolbar>
        </el-row>
      </template>
      <el-table v-loading="loading" :data="typeConfigList">
        <el-table-column label="服务商" align="center" prop="serviceName" />
        <el-table-column label="签章类型编码" align="center" prop="signNum" />
        <el-table-column label="服务商签名编码" align="center" prop="signCode" />
        <el-table-column label="签章类型概述" align="center" prop="signDesc" />
        <el-table-column label="状态" align="center" prop="status">
          <template #default="scope">
            <el-switch v-model="scope.row.status" active-value="Y" inactive-value="N" @change="handleStatusChange(scope.row)"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="说明" align="center" prop="description" />
      </el-table>
      <pagination v-show="total > 0" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" :total="total" @pagination="getList" />
    </el-card>
    <!-- 添加或修改签名类型配置对话框 -->
    <el-dialog v-model="dialog.visible" :title="dialog.title" width="60%" append-to-body>
      <el-form ref="typeConfigFormRef" :model="form" :rules="rules" :inline="true" label-width="120px">
        <el-form-item label="服务商ID" prop="serviceId">
          <el-select v-model="form.serviceId" placeholder="请输入归属模块" @change="selectService">
            <el-option v-for="dict in serviceList" :key="dict.serviceId" :label="dict.serviceName" :value="dict.serviceId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="服务商名称" prop="serviceName">
          <el-input v-model="form.serviceName" disabled />
        </el-form-item>
        <el-form-item label="服务商签名编码" prop="signCode">
          <el-input v-model="form.signCode" placeholder="请输入服务商签名编码" />
        </el-form-item>
        <el-form-item label="签章类型概述" prop="signDesc">
          <el-input v-model="form.signDesc" placeholder="请输入签章类型概述" />
        </el-form-item>
        <el-form-item label="说明" prop="description">
          <el-input v-model="form.description" type="textarea" rows="5" style="width: 400px" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="TypeConfig" lang="ts">
import {
  listTypeConfig,
  addTypeConfig,
  updateTypeConfig,
  getServiceList,
  changeSignTypeStatus
} from '@/api/print/typeConfig';
import { TypeConfigVO, TypeConfigQuery, TypeConfigForm } from '@/api/print/typeConfig/types';
import { parseTime } from '@/utils/ruoyi';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const typeConfigList = ref<TypeConfigVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const serviceList = ref([]);

const queryFormRef = ref<ElFormInstance>();
const typeConfigFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: TypeConfigForm = {
  id: undefined,
  serviceId: undefined,
  signNum: undefined,
  signCode: undefined,
  signDesc: undefined,
  status: undefined,
  description: undefined
};
const data = reactive<PageData<TypeConfigForm, TypeConfigQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    serviceId: undefined,
    signCode: undefined,
    signDesc: undefined,
    status: undefined,
    description: undefined,
    params: {}
  },
  rules: {
    serviceId: [{ required: true, message: '服务商ID不能为空', trigger: 'blur' }],
    signCode: [{ required: true, message: '服务商签名编码不能为空', trigger: 'blur' }],
    signDesc: [{ required: true, message: '签章类型概述不能为空', trigger: 'blur' }],
    status: [{ required: true, message: '状态不能为空', trigger: 'change' }],
    description: [{ required: true, message: '说明不能为空', trigger: 'blur' }]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询签名类型配置列表 */
const getList = async () => {
  loading.value = true;
  const res = await listTypeConfig(queryParams.value);
  typeConfigList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
};

/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  typeConfigFormRef.value?.resetFields();
};

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

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = '添加签名类型配置';
};

/** 提交按钮 */
const submitForm = () => {
  typeConfigFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updateTypeConfig(form.value).finally(() => (buttonLoading.value = false));
      } else {
        await addTypeConfig(form.value).finally(() => (buttonLoading.value = false));
      }
      proxy?.$modal.msgSuccess('操作成功');
      dialog.visible = false;
      await getList();
    }
  });
};

/**
 * 获取服务商列表
 */
const getServiceInfo = async () => {
  const res = await getServiceList();
  serviceList.value = res.data;
};

/**
 * 展示服务商名称
 */
const selectService = (value) => {
  const service = serviceList.value.find((item) => item.serviceId === value);
  form.value.serviceName = service.serviceName;
};

/**
 * 版本是否启用方法
 *
 */
const handleStatusChange = async (row) => {
  let text = row.status === 'Y' ? '启用' : '停用';
  try {
    await proxy?.$modal.confirm('确认要"' + text + '""' + row.signNum + '"签章类型编码吗?');
    await changeSignTypeStatus(row);
    proxy?.$modal.msgSuccess(text + '成功');
  } catch {
    row.status = row.status === 'Y' ? 'N' : 'Y';
  }
};

onMounted(() => {
  getList();
  getServiceInfo();
});
</script>
