<template>
  <PageWrapper dense>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleAdd">新增</a-button>
        <!-- <a-button type="primary" @click="addWorker">配置人员</a-button> -->
        <a-button type="primary" @click="addService">配置服务目录</a-button>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column && record && column.key === 'action'">
          <a-button type="link" @click="handleEdit(record.id)">编辑</a-button>
          <a-popconfirm
            title="确定要删除吗?"
            ok-text="是"
            cancel-text="否"
            @confirm="handleDelete(record.id)"
          >
            <a-button type="link">删除</a-button>
          </a-popconfirm>
          <a-button type="link" @click="handleDetail(record.id)">详情</a-button>
        </template>
      </template>
    </BasicTable>
    <a-modal
      v-model:open="serviceVisible"
      title="服务目录配置"
      @ok="serviceSubmit"
      @cancel="closeService"
      width="30%"
    >
      <a-form :model="serviceForm" layout="vertical" :rules="serviceRules" ref="serviceRef">
        <a-row :gutter="[16, 16]">
          <a-col :span="24">
            <a-form-item label="合同名称" name="contractId">
              <a-select
                v-model:value="serviceForm.contractId"
                :options="contractOptions"
                placeholder="请选择"
                :fieldNames="{
                  value: 'id',
                  label: 'contractName',
                  options: 'options',
                }"
              />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="24">
            <a-form-item label="目录名称" name="categoryId">
              <a-select
                v-model:value="serviceForm.categoryId"
                :options="categoryOptions"
                placeholder="请选择"
                :fieldNames="{
                  value: 'id',
                  label: 'categoryName',
                  options: 'options',
                }"
              />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-modal>
    <detailDrawer ref="detailDrawerRef" />
    <addModal ref="addModalRef" />
  </PageWrapper>
</template>

<script setup lang="ts">
  import { PageWrapper } from '@/components/Page';
  import { BasicTable, useTable } from '@/components/Table';
  import { list, removeByIds, createPlans, getCategory } from './api';
  import { formSchemas, columns } from './data';
  import detailDrawer from './detailDrawer.vue';
  import { ref, reactive } from 'vue';
  import addModal from './addModal.vue';
  import { message } from 'ant-design-vue';

  defineOptions({ name: 'ContractManage' });

  const [registerTable, { reload }] = useTable({
    rowSelection: {
      type: 'checkbox',
    },
    title: '合同管理',
    api: list,
    showIndexColumn: true,
    rowKey: 'id',
    useSearchForm: true,
    formConfig: {
      schemas: formSchemas,
      name: 'contractManage',
      baseColProps: {
        xs: 24,
        sm: 24,
        md: 24,
        lg: 6,
      },
    },
    columns: columns,
    actionColumn: {
      width: 200,
      title: '操作',
      key: 'action',
      fixed: 'right',
    },
  });

  //抽屉
  const detailDrawerRef = ref();
  const handleDetail = (id: any) => {
    detailDrawerRef.value.showDrawer(id);
  };
  const handleDelete = async (id: any) => {
    await removeByIds([id]);
    reload();
  };
  //新增编辑弹窗
  const addModalRef = ref();
  const handleAdd = () => {
    addModalRef.value.showModal(1);
  };
  const handleEdit = (id: any) => {
    addModalRef.value.showModal(2, id);
  };

  //服务配置弹窗
  let serviceForm = reactive({
    contractId: '',
    categoryId: '',
  });
  const serviceVisible = ref(false);
  const serviceRef = ref();
  const serviceSubmit = () => {
    serviceRef.value.validate().then((valid: any) => {
      if (valid) {
        let params = {
          contractId: serviceForm.contractId,
          categoryId: serviceForm.categoryId,
        };
        createPlans(params).then((_) => {
          message.success('操作成功');
          closeService();
        });
      }
    });
  };
  const serviceRules = {
    contractId: [{ required: true, message: '请选择' }],
    categoryId: [{ required: true, message: '请选择' }],
  };
  const addService = () => {
    serviceVisible.value = true;
    getOptions();
  };
  const closeService = () => {
    serviceVisible.value = false;
    serviceForm = {
      contractId: '',
      categoryId: '',
    };
  };
  const contractOptions = ref([]);
  const getContractOptions = () => {
    list({ pageNum: 1, pageSize: 100 }).then((res) => {
      contractOptions.value = res.rows;
    });
  };

  const categoryOptions = ref([]);
  const getCategoryOptions = () => {
    getCategory().then((res) => {
      categoryOptions.value = res;
    });
  };
  const getOptions = () => {
    getCategoryOptions();
    getContractOptions();
  };
</script>

<style scoped></style>
