<template>
  <div class="app-container">
    <div class="search-con">
      <div class="title">查询条件</div>
      <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="100px">
        <el-form-item label="pos机名称" prop="name">
          <el-input v-model="queryParams.name" placeholder="请输入pos机名称" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="queryParams.type" placeholder="请选择类型" clearable>
            <el-option v-for="dict in pos_type" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="账户状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="请选择账户状态" clearable>
            <el-option v-for="dict in account_status" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="商户名称" prop="merchantName">
          <el-input v-model="queryParams.merchantName" placeholder="请输入商户名称" clearable @keyup.enter="handleQuery" />
        </el-form-item>
      </el-form>
      <div class="search-btn-con">
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button type="info" icon="Refresh" @click="resetQuery">重置</el-button>
      </div>
    </div>
    <div class="main-con">
      <div class="title-con">
        <div class="title">基本信息</div>
        <div class="operate-btn-con">
          <el-button @click="handleAdd" icon="Plus" v-hasPermi="['invest:posmachine:add']">新增</el-button>
          <el-button :disabled="multiple" icon="Delete" @click="handleDelete" v-hasPermi="['invest:posmachine:remove']">删除</el-button>
          <!-- <el-button @click="handleExport" icon="Download" v-hasPermi="['invest:posmachine:export']">导出</el-button> -->
        </div>
      </div>
      <div class="content-con" v-loading="loading">
        <el-table v-loading="loading" :data="posmachineList" @selection-change="handleSelectionChange" height="calc(100% - 0.62rem)">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="pos机名称" align="center" prop="name" />
          <el-table-column label="支付公司" align="center" prop="payCompany" />
          <el-table-column label="类型" align="center" width="100" prop="type">
            <template #default="scope">
              <dict-tag :options="pos_type" :value="scope.row.type" />
            </template>
          </el-table-column>
          <el-table-column label="账户状态" width="100" align="center" prop="status">
            <template #default="scope">
              <dict-tag :options="account_status" :value="scope.row.status" />
            </template>
          </el-table-column>
          <el-table-column label="商户名称" align="center" prop="merchantName" />
          <el-table-column label="刷卡费率" align="center" width="150" prop="rateName" />
          <el-table-column label="费率+" align="center" width="100" prop="ratePlus" />
          <el-table-column label="结算卡" align="center" prop="bankNameCode" />
          <!-- <el-table-column label="商户类型" align="center" width="150" prop="merchantType">
            <template #default="scope">
              <dict-tag :options="merchant_type" :value="scope.row.merchantType" />
            </template>
          </el-table-column>
          <el-table-column label="商户码" align="center" prop="merchantCode" /> -->
          <el-table-column label="操作" align="center" width="150" class-name="small-padding fixed-width">
            <template v-slot="scope">
              <div class="ctrl-btn d-flex">
                <el-tooltip v-for="item in operateList" :key="item.id" class="item" effect="dark" :content="item.title" placement="top">
                  <el-button :icon="item.icon" v-hasPermi="item.hasPermi" circle @click="handleOperate(item.id, scope.row)"></el-button>
                </el-tooltip>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination small background layout="total, prev, pager, next" :total="total" @current-change="handleCurrentChange" />
      </div>
    </div>
    <!-- 添加或修改pos机信息对话框 -->
    <el-dialog :title="title" v-model="open" width="880px" append-to-body>
      <el-form ref="posmachineRef" :model="form" :inline="true" :rules="rules" label-width="120px">
        <el-form-item label="pos机名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入pos机名称" />
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择类型">
            <el-option v-for="dict in pos_type" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="账户状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择账户状态">
            <el-option v-for="dict in account_status" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商户名称" prop="merchantName">
          <el-input v-model="form.merchantName" placeholder="请输入商户名称" />
        </el-form-item>
        <el-form-item label="编号" prop="code">
          <el-input v-model="form.code" placeholder="请输入编号" />
        </el-form-item>
        <el-form-item label="结算卡" prop="debitCard">
          <el-select v-model="form.debitCard" placeholder="请选择结算卡" clearable>
            <el-option v-for="debitCard in debitCardList" :key="debitCard.id" :label="debitCard.nameCode" :value="debitCard.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="刷卡费率" prop="rate">
          <el-input v-model="form.rate" type="number" placeholder="请输入刷卡费率">
            <template #suffix>%</template>
          </el-input>
        </el-form-item>
        <el-form-item label="费率+" prop="ratePlus">
          <el-input v-model="form.ratePlus" type="number" placeholder="请输入费率+" />
        </el-form-item>
        <el-form-item label="舍入模式" prop="roundingMode">
          <el-select v-model="form.roundingMode" placeholder="请选择舍入模式">
            <el-option v-for="dict in rounding_mode" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="厂商" prop="manufacture">
          <el-input v-model="form.manufacture" placeholder="请输入厂商" />
        </el-form-item>
        <el-form-item label="支付公司" prop="payCompany">
          <el-input v-model="form.payCompany" placeholder="请输入支付公司" />
        </el-form-item>
        <el-form-item label="商户类型" prop="merchantType">
          <el-select v-model="form.merchantType" placeholder="请选择商户类型">
            <el-option v-for="dict in merchant_type" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商户码" prop="merchantCode">
          <el-input v-model="form.merchantCode" placeholder="请输入商户码" />
        </el-form-item>
        <el-form-item label="启用日期" prop="activationDate">
          <el-date-picker clearable v-model="form.activationDate" type="date" value-format="YYYY-MM-DD" placeholder="请选择启用日期"> </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" style="width: 792px" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <template v-if="title !== '查看pos机信息'" #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Posmachine">
import { listPosmachine, getPosmachine, delPosmachine, addPosmachine, updatePosmachine } from '@/api/invest/posmachine'
import { listBankcardLend } from '@/api/invest/bankcardlend'
// eslint-disable-next-line no-unused-vars
import { require } from '@/utils/require'
const { proxy } = getCurrentInstance()
const { merchant_type, pos_type, account_status, rounding_mode } = proxy.useDict('merchant_type', 'pos_type', 'account_status', 'rounding_mode')

const posmachineList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref('')
const debitCardList = ref([])
const operateList = ref([
  { id: 'view', icon: 'View', title: '查看', hasPermi: ['invest:posmachine:query'] },
  { id: 'edit', icon: 'Edit', title: '修改', hasPermi: ['invest:posmachine:edit'] },
  { id: 'delete', icon: 'Delete', title: '删除', hasPermi: ['invest:posmachine:remove'] }
])
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    type: null,
    status: null,
    merchantName: null
  },
  queryDebitCardParams: {
    pageNum: 1,
    type: '1',
    debitType: '1',
    status: '1',
    pageSize: 1000
  },
  rules: {
    name: [{ required: true, message: 'pos机名称不能为空', trigger: 'blur' }],
    status: [{ required: true, message: '账户状态不能为空', trigger: 'blur' }],
    code: [{ required: true, message: '编号不能为空', trigger: 'blur' }],
    // payCompany: [{ required: true, message: '支付公司不能为空', trigger: 'blur' }],
    type: [{ required: true, message: '类型不能为空', trigger: 'change' }],
    // manufacture: [{ required: true, message: '厂商不能为空', trigger: 'blur' }],
    merchantName: [{ required: true, message: '商户名称不能为空', trigger: 'blur' }],
    rate: [{ required: true, message: '刷卡费率不能为空', trigger: 'blur' }],
    ratePlus: [{ required: true, message: '费率+不能为空', trigger: 'blur' }],
    roundingMode: [{ required: true, message: '舍入模式不能为空', trigger: 'blur' }],
    debitCard: [{ required: true, message: '结算卡不能为空', trigger: 'blur' }]
    // merchantType: [{ required: true, message: '商户类型不能为空', trigger: 'change' }]
  }
})

const handleOperate = (operate, row) => {
  switch (operate) {
    case 'view':
      handleView(row)
      break
    case 'edit':
      handleUpdate(row)
      break
    case 'delete':
      handleDelete(row)
      break
    default:
      break
  }
}

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

/** 查询储蓄卡管理列表 */
function getDebitCardList() {
  listBankcardLend(queryDebitCardParams.value).then((response) => {
    debitCardList.value = response.rows
  })
}

/** 查询pos机信息列表 */
function getList() {
  loading.value = true
  listPosmachine(queryParams.value).then((response) => {
    posmachineList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

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

// 表单重置
function reset() {
  form.value = {
    id: null,
    name: null,
    type: null,
    manufacture: null,
    payCompany: null,
    merchantName: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    delFlag: null,
    remark: null,
    code: null,
    rate: null,
    debitCard: null,
    merchantType: null,
    merchantCode: null,
    ratePlus: 0,
    activationDate: null,
    status: null,
    roundingMode: '1'
  }
  proxy.resetForm('posmachineRef')
}

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

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm('queryRef')
  handleQuery()
}

// 分页
const handleCurrentChange = (val) => {
  queryParams.value.pageNum = val
  getList()
}

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

// 查看
const handleView = (row) => {
  title.value = '查看pos机信息'
  form.value = row
  open.value = true
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = '添加pos机信息'
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getPosmachine(_id).then((response) => {
    form.value = response.data
    open.value = true
    title.value = '修改pos机信息'
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs.posmachineRef.validate((valid) => {
    if (valid) {
      if (form.value.id != null) {
        updatePosmachine(form.value).then((response) => {
          proxy.$modal.msgSuccess('修改成功')
          open.value = false
          getList()
        })
      } else {
        addPosmachine(form.value).then((response) => {
          proxy.$modal.msgSuccess('新增成功')
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal
    .confirm('是否确认删除选中的数据项？')
    .then(function () {
      return delPosmachine(_ids)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess('删除成功')
    })
    .catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    'invest/posmachine/export',
    {
      ...queryParams.value
    },
    `posmachine_${new Date().getTime()}.xlsx`
  )
}
getDebitCardList()
getList()
</script>
