<script setup lang="ts">
import AForm from '@/components/AForm/index.vue'
import { FormConfigType } from '@/components/AForm/type'
import { ToolbarConfigType } from '@/components/AToolbar/type'
import { ColumnConfigType } from '@/components/ATable/type'
import { StrAnyObj, StrAnyObjArr } from '@/typings'
import { useHandleData } from '@/utils/useHandleData'
import { getPageApi, getDetailApi, addApi, editApi, deleteApi } from '@/api/business/contract/info'

const queryRef = ref<InstanceType<typeof AForm>>()
const formRef = ref<InstanceType<typeof AForm>>()

const showQuery = ref<boolean>(true)
const selectKeys = ref<string[]>([])
const pageTotal = ref<number>(0)

const queryData = ref<StrAnyObj>({ pageNum: 1, pageSize: 10 })
const tableData = ref<StrAnyObjArr>([])
const formData = ref<StrAnyObj>({})

const dialogTitle = ref<string>('')
const dialogVisible = ref<boolean>(false)

const queryConfig: FormConfigType[] = [
  {
    type: 'input',
    prop: 'contractNo',
    label: '合同编号'
  }
]

const toolbarConfig: ToolbarConfigType[] = [
  {
    common: 'search',
    click() {
      queryData.value.pageNum = 1
      getPage()
    }
  },
  {
    common: 'reset',
    click() {
      queryRef.value?.resetFields()
      getPage()
    }
  }
]

const columnConfig: ColumnConfigType[] = [
  {
    prop: 'contractNo',
    label: '合同编号'
  },
  {
    prop: 'signingDate',
    label: '签订日期'
  },
  {
    prop: 'startDate',
    label: '开始日期'
  },
  {
    prop: 'endDate',
    label: '结束日期'
  },
  {
    prop: 'currency',
    label: '币种'
  },
  {
    prop: 'exchangeRate',
    label: '汇率'
  },
  {
    prop: 'freightPrice',
    label: '运费'
  },
  {
    prop: 'insurance',
    label: '保险费'
  },
  {
    prop: 'amount',
    label: '金额'
  },
  {
    width: 250,
    handleConfig: [
      {
        common: 'update',
        click(row) {
          dialogVisible.value = true
          dialogTitle.value = '编辑'
          getDetailApi({ id: row.id }).then((resp: StrAnyObj) => {
            formData.value = resp
          })
        }
      },
      {
        common: 'delete',
        click(row) {
          handleRemove([row.id])
        }
      }
    ]
  }
]

const formConfig: FormConfigType[] = [
  {
    type: 'input',
    prop: 'contractNo',
    label: '合同编号',
    rule: [{ required: true, message: '合同编号不能为空', trigger: 'blur' }]
  },
  {
    type: 'input',
    prop: 'type',
    label: '合同类型 1-三梵合同，2-佰易来合同',
    rule: [{ required: true, message: '合同类型 1-三梵合同，2-佰易来合同不能为空', trigger: 'blur' }]
  },
  {
    type: 'input',
    prop: 'contractSubjectId',
    label: '合同主体id',
    rule: [{ required: true, message: '合同主体id不能为空', trigger: 'blur' }]
  },
  {
    type: 'input',
    prop: 'purchaseType',
    label: '采购类型 1-一件商品，2-批单商品',
    rule: [{ required: true, message: '采购类型 1-一件商品，2-批单商品不能为空', trigger: 'blur' }]
  },
  {
    type: 'input',
    prop: 'isTax',
    label: '是否含税（1是 0否）',
    rule: [{ required: true, message: '是否含税（1是 0否）不能为空', trigger: 'blur' }]
  },
  {
    type: 'input',
    prop: 'rankId',
    label: '等级id',
    rule: [{ required: true, message: '等级id不能为空', trigger: 'blur' }]
  },
  {
    type: 'input',
    prop: 'status',
    label: '合同状态 10进行中 20已完成',
    rule: [{ required: true, message: '合同状态 10进行中 20已完成不能为空', trigger: 'blur' }]
  },
  {
    type: 'input',
    prop: 'packingStatus',
    label: '包装状态 0未包装 1已包装',
    rule: [{ required: true, message: '包装状态 0未包装 1已包装不能为空', trigger: 'blur' }]
  },
  {
    type: 'input',
    prop: 'paymentType',
    label: '支付类型 1银行卡支付 2支付宝支付',
    rule: [{ required: true, message: '支付类型 1银行卡支付 2支付宝支付不能为空', trigger: 'blur' }]
  },
  {
    type: 'input',
    prop: 'sellAccountName',
    label: '卖家账户名',
    rule: [{ required: true, message: '卖家账户名不能为空', trigger: 'blur' }]
  },
  {
    type: 'input',
    prop: 'sellBankName',
    label: '卖家开户银行',
    rule: [{ required: true, message: '卖家开户银行不能为空', trigger: 'blur' }]
  },
  {
    type: 'input',
    prop: 'sellBankAccount',
    label: '卖家银行账号',
    rule: [{ required: true, message: '卖家银行账号不能为空', trigger: 'blur' }]
  },
  {
    type: 'input',
    prop: 'sellBankCnapsCode',
    label: '卖家联行号',
    rule: [{ required: true, message: '卖家联行号不能为空', trigger: 'blur' }]
  },
  {
    type: 'input',
    prop: 'sellBeneficiaryName',
    label: '卖家账户名称（外汇信息）',
    rule: [{ required: true, message: '卖家账户名称（外汇信息）不能为空', trigger: 'blur' }]
  },
  {
    type: 'input',
    prop: 'sellBeneficiaryBank',
    label: '卖家开户银行（外汇信息）',
    rule: [{ required: true, message: '卖家开户银行（外汇信息）不能为空', trigger: 'blur' }]
  },
  {
    type: 'input',
    prop: 'sellBeneficiaryBankAccount',
    label: '卖家账号（外汇信息）',
    rule: [{ required: true, message: '卖家账号（外汇信息）不能为空', trigger: 'blur' }]
  },
  {
    type: 'input',
    prop: 'sellBeneficiaryBankAddress',
    label: '卖家开户银行地址（外汇信息）',
    rule: [{ required: true, message: '卖家开户银行地址（外汇信息）不能为空', trigger: 'blur' }]
  },
  {
    type: 'input',
    prop: 'sellSwiftCode',
    label: '卖家银行代码（外汇信息）',
    rule: [{ required: true, message: '卖家银行代码（外汇信息）不能为空', trigger: 'blur' }]
  },
  {
    type: 'input',
    prop: 'sellBeneficiaryAddress',
    label: '卖家收款人地址（外汇信息）',
    rule: [{ required: true, message: '卖家收款人地址（外汇信息）不能为空', trigger: 'blur' }]
  },
  {
    type: 'input',
    prop: 'sellAlipayAccount',
    label: '卖家支付宝账号',
    rule: [{ required: true, message: '卖家支付宝账号不能为空', trigger: 'blur' }]
  },
  {
    type: 'input',
    prop: 'signingDate',
    label: '签订日期',
    rule: [{ required: true, message: '签订日期不能为空', trigger: 'blur' }]
  },
  {
    type: 'input',
    prop: 'startDate',
    label: '开始日期',
    rule: [{ required: true, message: '开始日期不能为空', trigger: 'blur' }]
  },
  {
    type: 'input',
    prop: 'endDate',
    label: '结束日期',
    rule: [{ required: true, message: '结束日期不能为空', trigger: 'blur' }]
  },
  {
    type: 'input',
    prop: 'validityDate',
    label: '报价有效期(天)',
    rule: [{ required: true, message: '报价有效期(天)不能为空', trigger: 'blur' }]
  },
  {
    type: 'input',
    prop: 'customerId',
    label: '客户id',
    rule: [{ required: true, message: '客户id不能为空', trigger: 'blur' }]
  },
  {
    type: 'input',
    prop: 'customerName',
    label: '客户名称',
    rule: [{ required: true, message: '客户名称不能为空', trigger: 'blur' }]
  },
  {
    type: 'input',
    prop: 'customerTelNum',
    label: '客户联系电话',
    rule: [{ required: true, message: '客户联系电话不能为空', trigger: 'blur' }]
  },
  {
    type: 'input',
    prop: 'customerCountry',
    label: '客户国家',
    rule: [{ required: true, message: '客户国家不能为空', trigger: 'blur' }]
  },
  {
    type: 'input',
    prop: 'customerAddress',
    label: '客户地址',
    rule: [{ required: true, message: '客户地址不能为空', trigger: 'blur' }]
  },
  {
    type: 'input',
    prop: 'destination',
    label: '目的地',
    rule: [{ required: true, message: '目的地不能为空', trigger: 'blur' }]
  },
  {
    type: 'input',
    prop: 'shippingMethod',
    label: '货运方式',
    rule: [{ required: true, message: '货运方式不能为空', trigger: 'blur' }]
  },
  {
    type: 'input',
    prop: 'deliveryTerms',
    label: '交货条款',
    rule: [{ required: true, message: '交货条款不能为空', trigger: 'blur' }]
  },
  {
    type: 'input',
    prop: 'deliveryTime',
    label: '交货时间(天)',
    rule: [{ required: true, message: '交货时间(天)不能为空', trigger: 'blur' }]
  },
  {
    type: 'input',
    prop: 'paymentTerms',
    label: '付款条件',
    rule: [{ required: true, message: '付款条件不能为空', trigger: 'blur' }]
  },
  {
    type: 'input',
    prop: 'warrantyPeriod',
    label: '保修时间(天)',
    rule: [{ required: true, message: '保修时间(天)不能为空', trigger: 'blur' }]
  },
  {
    type: 'input',
    prop: 'currency',
    label: '币种',
    rule: [{ required: true, message: '币种不能为空', trigger: 'blur' }]
  },
  {
    type: 'input',
    prop: 'exchangeRate',
    label: '汇率',
    rule: [{ required: true, message: '汇率不能为空', trigger: 'blur' }]
  },
  {
    type: 'input',
    prop: 'freightPrice',
    label: '运费',
    rule: [{ required: true, message: '运费不能为空', trigger: 'blur' }]
  },
  {
    type: 'input',
    prop: 'insurance',
    label: '保险费',
    rule: [{ required: true, message: '保险费不能为空', trigger: 'blur' }]
  },
  {
    type: 'input',
    prop: 'amount',
    label: '金额',
    rule: [{ required: true, message: '金额不能为空', trigger: 'blur' }]
  },
  {
    type: 'input',
    prop: 'remark',
    label: '备注',
    rule: [{ required: true, message: '备注不能为空', trigger: 'blur' }]
  },
  {
    type: 'input',
    prop: 'createBy',
    label: '创建者',
    rule: [{ required: true, message: '创建者不能为空', trigger: 'blur' }]
  },
  {
    type: 'input',
    prop: 'createTime',
    label: '创建时间',
    rule: [{ required: true, message: '创建时间不能为空', trigger: 'blur' }]
  },
  {
    type: 'input',
    prop: 'updateBy',
    label: '更新者',
    rule: [{ required: true, message: '更新者不能为空', trigger: 'blur' }]
  },
  {
    type: 'input',
    prop: 'updateTime',
    label: '最后更新时间',
    rule: [{ required: true, message: '最后更新时间不能为空', trigger: 'blur' }]
  },
  {
    type: 'input',
    prop: 'delFlag',
    label: '删除标志（0代表存在 1代表删除）',
    rule: [{ required: true, message: '删除标志（0代表存在 1代表删除）不能为空', trigger: 'blur' }]
  },
  {
    type: 'input',
    prop: 'tenantId',
    label: '租户id',
    rule: [{ required: true, message: '租户id不能为空', trigger: 'blur' }]
  },
]

onMounted(() => {
  getPage()
})

function getPage() {
  getPageApi(queryData.value).then((resp) => {
    tableData.value = resp.records
    pageTotal.value = resp.total
  })
}

function tableSelectionChange(item: StrAnyObjArr) {
  selectKeys.value = item.map((item) => item.id)
}

function formSubmit() {
  formRef.value?.validate(() => {
    if (formData.value.id) {
      editApi(formData.value).then(() => {
        dialogVisible.value = false
        ElMessage.success('修改成功')
        getPage()
      })
    } else {
      addApi(formData.value).then(() => {
        dialogVisible.value = false
        ElMessage.success('新增成功')
        getPage()
      })
    }
  })
}

function formClosed() {
  formRef.value?.resetFields()
}

function handleRemove(idList: string[]) {
  useHandleData('是否确认删除？', () => {
    deleteApi({ idList }).then(() => {
      ElMessage.success('删除成功')
      getPage()
    })
  })
}
</script>

<template>
  <div>
    <el-card v-if="showQuery">
      <a-form ref="queryRef" v-model="queryData" :config="queryConfig" :span="6"> </a-form>
    </el-card>

    <a-table
      :data="tableData"
      :page-total="pageTotal"
      :toolbar-config="toolbarConfig"
      :column-config="columnConfig"
      v-model:showQuery="showQuery"
      v-model:page-num="queryData.pageNum"
      v-model:page-size="queryData.pageSize"
      @page-num-change="getPage"
      @page-size-change="getPage"
      @selection-change="tableSelectionChange"
    >
    </a-table>

    <a-dialog
      v-model="dialogVisible"
      :title="dialogTitle"
      @submit="formSubmit"
      @closed="formClosed"
    >
      <a-form ref="formRef" v-model="formData" :config="formConfig" :span="24"> </a-form>
    </a-dialog>
  </div>
</template>
