<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="80px">
            <!--            <el-form-item label="有无合同" prop="contractFlag">-->
            <!--              <el-select v-model="queryParams.contractFlag" placeholder="请选择有无合同" clearable>-->
            <!--                <el-option v-for="dict in contract_flag" :key="dict.value" :label="dict.label" :value="dict.value" />-->
            <!--              </el-select>-->
            <!--            </el-form-item>-->
            <el-form-item label="合同编号" prop="contractCode">
              <el-input v-model="queryParams.contractCode" placeholder="请输入合同编号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="合同名称" prop="contractName">
              <el-input v-model="queryParams.contractName" placeholder="请输入合同名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <!--            <el-form-item label="合同大类" prop="contractCategory">-->
            <!--              <el-select v-model="queryParams.contractCategory" placeholder="请选择合同大类" clearable>-->
            <!--                <el-option v-for="dict in contract_category" :key="dict.value" :label="dict.label" :value="dict.value" />-->
            <!--              </el-select>-->
            <!--            </el-form-item>-->
            <!--            <el-form-item label="合同类型" prop="contractType">-->
            <!--              <el-select v-model="queryParams.contractType" placeholder="请选择合同类型" clearable>-->
            <!--                <el-option v-for="dict in contract_type" :key="dict.value" :label="dict.label" :value="dict.value" />-->
            <!--              </el-select>-->
            <!--            </el-form-item>-->
            <el-form-item label="业务方向" prop="businessDirection">
              <el-select v-model="queryParams.businessDirection" placeholder="请选择业务方向" clearable>
                <el-option v-for="dict in business_direction" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
            <!--            <el-form-item label="部门" prop="contractDeptId">-->
            <!--              <el-input v-model="queryParams.contractDeptId" placeholder="请输入部门" clearable @keyup.enter="handleQuery" />-->
            <!--            </el-form-item>-->
            <!--            <el-form-item label="合同状态" prop="contractStatus">-->
            <!--              <el-select v-model="queryParams.contractStatus" placeholder="请选择合同状态" clearable>-->
            <!--                <el-option v-for="dict in contract_status" :key="dict.value" :label="dict.label" :value="dict.value" />-->
            <!--              </el-select>-->
            <!--            </el-form-item>-->
            <!--            <el-form-item label="流程状态" prop="flowStatus">-->
            <!--              <el-input v-model="queryParams.flowStatus" 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-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['oa/erp:contractInfo:add']"> 新增 </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['oa/erp:contractInfo:edit']"
              >修改
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['oa/erp:contractInfo:remove']"
              >删除
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['oa/erp:contractInfo:export']">导出 </el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" :columns="columns" :search="true" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" border :data="contractInfoList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="合同ID" align="center" prop="contractId" v-if="columns[0].visible" />
        <el-table-column label="有无合同" align="center" prop="contractFlag" v-if="columns[2].visible">
          <template #default="scope">
            <dict-tag :options="contract_flag" :value="scope.row.contractFlag" />
          </template>
        </el-table-column>
        <el-table-column label="合同编号" align="center" prop="contractCode" width="140" v-if="columns[3].visible" />
        <el-table-column label="合同名称" align="center" prop="contractName" width="200" v-if="columns[4].visible" />
        <el-table-column label="合同大类" align="center" prop="contractCategory" v-if="columns[5].visible">
          <template #default="scope">
            <dict-tag :options="contract_category" :value="scope.row.contractCategory" />
          </template>
        </el-table-column>
        <el-table-column label="合同类型" align="center" prop="contractType" v-if="columns[6].visible">
          <template #default="scope">
            <dict-tag :options="contract_type" :value="scope.row.contractType" />
          </template>
        </el-table-column>
        <el-table-column label="业务方向" align="center" prop="businessDirection" width="100" v-if="columns[7].visible">
          <template #default="scope">
            <dict-tag :options="business_direction" :value="scope.row.businessDirection" />
          </template>
        </el-table-column>
        <el-table-column label="部门" align="center" prop="deptName" width="100" v-if="columns[8].visible" />
        <el-table-column label="合同时间" align="center" prop="contractDate" width="100" v-if="columns[9].visible">
          <template #default="scope">
            <span>{{ parseTime(scope.row.contractDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="合同总价" align="center" prop="totalPrice" v-if="columns[10].visible" />
        <el-table-column label="甲方公司" align="center" prop="oneCustomerName" width="200" v-if="columns[11].visible" />
        <el-table-column label="甲方授权代表" align="center" prop="oneRepresent" width="110" v-if="columns[12].visible" />
        <el-table-column label="甲方签字日期" align="center" prop="aDate" width="120" v-if="columns[13].visible">
          <template #default="scope">
            <span>{{ parseTime(scope.row.oneDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="乙方公司" align="center" prop="twoCustomerName" width="200" v-if="columns[14].visible" />
        <el-table-column label="乙方授权代表" align="center" prop="twoRepresent" width="110" v-if="columns[15].visible" />
        <el-table-column label="乙方签字日期" align="center" prop="twoDate" width="120" v-if="columns[16].visible">
          <template #default="scope">
            <span>{{ parseTime(scope.row.twoDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="合同状态" align="center" prop="contractStatus" v-if="columns[17].visible">
          <template #default="scope">
            <dict-tag :options="contract_status" :value="scope.row.contractStatus" />
          </template>
        </el-table-column>
        <el-table-column label="流程状态" align="center" prop="flowStatus" v-if="columns[18].visible" />
        <el-table-column label="合同模板ID" align="center" prop="templateId" v-if="columns[19].visible" />
        <el-table-column label="附件ID" align="center" prop="ossId" v-if="columns[20].visible" />
        <el-table-column label="付款方式" align="center" prop="paymentMethod" v-if="columns[21].visible" />
        <el-table-column label="签字合同附件" align="center" prop="signatureAppendix" v-if="columns[22].visible" />
        <el-table-column label="合同税率(预留)" align="center" prop="taxRate" v-if="columns[23].visible" />
        <el-table-column label="备注" align="center" prop="remark" v-if="columns[24].visible" />
        <el-table-column label="激活标识" align="center" prop="activeFlag" v-if="columns[25].visible">
          <template #default="scope">
            <dict-tag :options="active_flag" :value="scope.row.activeFlag" />
          </template>
        </el-table-column>
        <el-table-column label="创建人" align="center" prop="createBy" v-if="columns[28].visible" />
        <el-table-column label="创建时间" align="center" prop="createTime" width="180" v-if="columns[29].visible">
          <template #default="scope">
            <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="更新人" align="center" prop="updateBy" v-if="columns[30].visible" />
        <el-table-column label="更新时间" align="center" prop="updateTime" width="180" v-if="columns[31].visible">
          <template #default="scope">
            <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['oa/erp:contractInfo:edit']"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['oa/erp:contractInfo:remove']"></el-button>
            </el-tooltip>
          </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-card>
  </div>
</template>

<script setup name="ContractInfo" lang="ts">
import { listContractInfo, delContractInfo } from '@/api/oa/erp/contractInfo';
import { ContractInfoVO, ContractInfoQuery } from '@/api/oa/erp/contractInfo/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { contract_category, business_direction, active_flag, contract_flag, contract_type, contract_status } = toRefs<any>(
  proxy?.useDict('contract_category', 'business_direction', 'active_flag', 'contract_flag', 'contract_type', 'contract_status')
);

const contractInfoList = ref<ContractInfoVO[]>([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();

// 列显隐信息
const columns = ref<FieldOption[]>([
  { key: 0, label: `合同ID`, visible: false },
  { key: 1, label: `租户编号`, visible: false },
  { key: 2, label: `有无合同`, visible: true },
  { key: 3, label: `合同编号`, visible: true },
  { key: 4, label: `合同名称`, visible: true },
  { key: 5, label: `合同大类`, visible: true },
  { key: 6, label: `合同类型`, visible: true },
  { key: 7, label: `业务方向`, visible: true },
  { key: 8, label: `部门`, visible: true },
  { key: 9, label: `合同时间`, visible: true },
  { key: 10, label: `合同总价`, visible: true },
  { key: 11, label: `甲方公司`, visible: true },
  { key: 12, label: `甲方授权代表`, visible: true },
  { key: 13, label: `甲方签字日期`, visible: true },
  { key: 14, label: `乙方公司`, visible: true },
  { key: 15, label: `乙方授权代表`, visible: true },
  { key: 16, label: `乙方签字日期`, visible: true },
  { key: 17, label: `合同状态`, visible: true },
  { key: 18, label: `流程状态`, visible: true },
  { key: 19, label: `合同模板ID`, visible: false },
  { key: 20, label: `附件ID`, visible: false },
  { key: 21, label: `付款方式`, visible: true },
  { key: 22, label: `签字合同附件`, visible: false },
  { key: 23, label: `合同税率(预留)`, visible: false },
  { key: 24, label: `备注`, visible: true },
  { key: 25, label: `激活标识`, visible: false },
  { key: 26, label: `删除标志`, visible: false },
  { key: 27, label: `创建部门`, visible: false },
  { key: 28, label: `创建人`, visible: false },
  { key: 29, label: `创建时间`, visible: false },
  { key: 30, label: `更新人`, visible: false },
  { key: 31, label: `更新时间`, visible: false }
]);

const data = reactive<{ queryParams: ContractInfoQuery }>({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    contractFlag: undefined,
    contractCode: undefined,
    contractName: undefined,
    contractCategory: undefined,
    contractType: undefined,
    businessDirection: undefined,
    contractDeptId: undefined,
    contractDate: undefined,
    totalPrice: undefined,
    oneCustomerId: undefined,
    oneRepresent: undefined,
    oneDate: undefined,
    twoCustomerId: undefined,
    twoRepresent: undefined,
    twoDate: undefined,
    contractStatus: undefined,
    flowStatus: undefined,
    templateId: undefined,
    ossId: undefined,
    paymentMethod: undefined,
    signatureAppendix: undefined,
    taxRate: undefined,
    activeFlag: undefined,
    params: {}
  }
});

const { queryParams } = toRefs(data);

/** 查询合同信息列表 */
const getList = async () => {
  loading.value = true;
  const res = await listContractInfo(queryParams.value);
  contractInfoList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

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

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

/** 多选框选中数据 */
const handleSelectionChange = (selection: ContractInfoVO[]) => {
  ids.value = selection.map((item) => item.contractId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

/** 新增按钮操作 */
const handleAdd = () => {
  proxy?.$tab.openPage('/oa/erp/contractInfo/edit', '添加市场销售合同');
};

/** 修改按钮操作 */
const handleUpdate = (row?: ContractInfoVO) => {
  const _contractId = row?.contractId || ids.value[0];
  proxy?.$tab.openPage(`/oa/erp/contractInfo/edit`, '合同信息-修改', { id: _contractId });
};

/** 删除按钮操作 */
const handleDelete = async (row?: ContractInfoVO) => {
  const _contractIds = row?.contractId || ids.value;
  await proxy?.$modal.confirm('是否确认删除合同信息编号为"' + _contractIds + '"的数据项？').finally(() => (loading.value = false));
  await delContractInfo(_contractIds);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download(
    'oa/erp/contractInfo/export',
    {
      ...queryParams.value
    },
    `contractInfo_${new Date().getTime()}.xlsx`
  );
};

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