<template>
  <div>
    <el-card :body-style="cardBodyStyle">
      <el-form :inline="true">
        <el-form-item label="开户行名称" style="margin-bottom: 5px;" prop="bankNm">
          <el-input v-model="query.bankNm" placeholder="请输入开户行名称" clearable></el-input>
        </el-form-item>
      </el-form>
    </el-card>

    <ButtonBar @search="handleSearch" @add="handleAdd" @remove="handleRemoveBatch" @edit="handleEditBatch"
      @upload="handleUpload" @download="handleDownload" @import="handleImport" @export="handleExport"></ButtonBar>
    <el-card :body-style="cardBodyStyle">
      <el-table :data="tableData" border highlight-current-row @selection-change="handleSelectionChange" height="63vh">
        <el-table-column type="selection" fixed></el-table-column>
        <el-table-column type="index" label="序号" align="center" width="60">
          <template #default="scope">
            <span>{{ scope.$index + (currentPage - 1) * pageSize + 1 }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="true" prop="beneficiaryAccount" label="收款账号" min-width="240" header-align="center"
          align="center" show-overflow-tooltip>
        </el-table-column>
        <el-table-column v-if="true" prop="bankNo" label="联行号" width="180" header-align="center" align="center"
          show-overflow-tooltip>
        </el-table-column>
        <el-table-column v-if="true" prop="bankNm" label="开户行名称" width="300" header-align="center" align="center"
          show-overflow-tooltip>
        </el-table-column>
        <el-table-column v-if="true" prop="bankProvince" label="省" width="60" header-align="center" align="center"
          show-overflow-tooltip>
          <template #default="{ row }">
            {{ dicList.PROVINCE_CODE.find(item => item.dicCode === row.bankProvince)?.dicName }}
          </template>
        </el-table-column>
        <el-table-column v-if="true" prop="bankCity" label="市" width="60" header-align="center" align="center"
          show-overflow-tooltip>
        </el-table-column>

        <el-table-column label="操作" header-align="center" align="center" width="200px" fixed="right">
          <template #default="scope">
            <el-button plain type="warning" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button plain type="danger" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-card :body-style="cardBodyStyle">
      <el-pagination :current-page="currentPage" :page-size="pageSize" :page-sizes="pageSizes" :total="totalRows"
        background="background" layout="total, sizes, prev, pager, next, jumper" @size-change="handleSizeChange"
        @current-change="handleCurrentChange">
      </el-pagination>
    </el-card>

    <el-dialog :close-on-click-modal="false" draggable title="编辑" v-model="dialogVisible" width="400px" top="5vh"
      @open="openDialog" @close="handleCancelSave">
      <el-form :model="form" label-width="100px" label-position="right" inline ref="ruleFormRef" :rules="rules">
        <el-form-item label="收款账号" prop="beneficiaryAccount">
          <el-input v-model="form.beneficiaryAccount" clearable style="width: 200px"></el-input>
        </el-form-item>
        <el-form-item label="联行号" prop="bankNo">
          <el-input v-model="form.bankNo" clearable style="width: 200px"></el-input>
        </el-form-item>
        <el-form-item label="开户行名称" prop="bankNm">
          <el-input v-model="form.bankNm" clearable style="width: 200px"></el-input>
        </el-form-item>
        <el-form-item label="省" prop="bankProvince">
          <el-select v-model="form.bankProvince" placeholder="请选择" style="width: 200px;">
            <el-option v-for="item in dicList.PROVINCE_CODE" :key="item.dicCode" :label="item.dicName"
              :value="item.dicCode" />
          </el-select>
        </el-form-item>
        <el-form-item label="市" prop="bankCity">
          <el-input v-model="form.bankCity" clearable style="width: 200px"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="success" @click="handleSave(ruleFormRef)">保存</el-button>
          <el-button type="danger" @click="handleCancelSave">取消</el-button>
        </div>
      </template>
    </el-dialog>


  </div>
</template>

<script setup>
import ButtonBar from '@/components/ButtonBar.vue'

import {
  ref,
  reactive,
  onMounted,
  getCurrentInstance
} from 'vue'

const {
  proxy
} = getCurrentInstance()

import tools from '@/tools'
const cardBodyStyle = ref(tools.style.card)
const inputStyle = ref(tools.style.input)

onMounted(() => {
  handleSearch()
  getDictList()
})



/**
 *  查询字典
 */
const dicList = ref({
  PROVINCE_CODE: [],
})
const getDictList = () => {
  var url = 'framework/Common/querySelectorList'
  var param = {
    type: JSON.stringify(['PROVINCE_CODE'])
  }
  proxy.$axios.get(url, param).then(response => {
    if (response.data.code == '0') {
      dicList.value = response.data.data
    } else {
      ElMessage.error('操作失败！' + response.data.msg)
    }
  })
}
/**
 *  查询字典
 */


/**
 *   查询
 */
const query = ref({
  bankNm: ''
})
const tableData = ref([])
const handleSearch = () => {
  currentPage.value = 1
  getTableData()
}

const getTableData = () => {
  var url = 'pur/PayBank/query'
  var param = {
    page: currentPage.value,
    rows: pageSize.value,
    params: JSON.stringify(query.value)
  }
  proxy.$axios.get(url, param).then(response => {
    if (response.data.code == '0') {
      tableData.value = response.data.data.records
      totalRows.value = response.data.data.total
    } else {
      ElMessage.error('操作失败！' + response.data.msg)
    }
  })
}
/**
 *   查询
 */

/**
 * 分页
 */
const currentPage = ref(1)
const totalRows = ref(0)
const pageSizes = ref([5, 10, 20, 30, 40])
const pageSize = ref(5)
const handleSizeChange = (val) => {
  pageSize.value = val
  getTableData()
}
const handleCurrentChange = (val) => {
  currentPage.value = val
  getTableData()
}
/**
 * 分页
 */


/**
 * 新增
 */
const dialogVisible = ref(false)
const form = ref({
  bankNo: '',
  bankNm: '',
  bankProvince: '',
  bankCity: '',
  beneficiaryAccount: ''
})
const ruleFormRef = ref()
const rules = reactive({
  bankNo: [{
    required: true,
    message: '银联号必须录入',
  }],
  bankNm: [{
    required: true,
    message: '开户行名称必须录入',
  }],
  bankProvince: [{
    required: true,
    message: '省必须录入',
  }],
  bankCity: [{
    required: true,
    message: '市必须录入',
  }],
  beneficiaryAccount: [{
    required: true,
    message: '收款账号必须录入',
  }],
})

const handleAdd = () => {
  if (ruleFormRef.value) {
    ruleFormRef.value.resetFields();
  }
  form.value = {
    bankNo: '',
    bankNm: '',
    bankProvince: '',
    bankCity: '',
    beneficiaryAccount: ''
  }
  dialogVisible.value = true
}
/**
 * 新增
 */


/**
 * 保存、修改、删除
 */

const openDialog = () => {
}

const handleSave = (ruleFormRef) => {
  ruleFormRef.validate((valid, fields) => {
    if (valid) {
      var url = 'pur/PayBank/save'
      var param = {
        json: JSON.stringify(form.value),
      }
      proxy.$axios.post(url, param).then(response => {
        if (response.data.code == 0) {
          form.value = response.data.data
          ElMessage({
            message: '保存成功',
            type: 'success',
          })
          getTableData()
          dialogVisible.value = false
        } else {
          // ElMessage.error('操作失败！' + response.data.msg)
          ElMessage({
            message: '操作失败' + response.data.msg,
            type: 'error'
          })
        }
      })

    } else {
      console.log('error submit!', fields)
    }
  })
}
const handleCancelSave = () => {
  form.value = {
    bankNo: '',
    bankNm: '',
    bankProvince: '',
    bankCity: '',
    beneficiaryAccount: ''
  }
  dialogVisible.value = false
}
const handleEdit = (row) => {
  if (ruleFormRef.value) {
    ruleFormRef.value.resetFields();
  }
  dialogVisible.value = true
  form.value = row
}
const handleDelete = (row) => {
  ElMessageBox.confirm(
    '确定删除本条记录?',
    '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
    draggable: true,
  }
  )
    .then(() => {
      var url = 'pur/PayBank/remove'
      var param = {
        id: row.id
      }
      proxy.$axios.post(url, param).then(response => {
        if (response.data.code == 0) {
          ElMessage({
            type: 'success',
            message: '删除成功',
          })
          getTableData()
        } else {
          ElMessage({
            type: 'error',
            message: '删除失败' + response.data.msg
          });
        }
      });
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '已取消',
      })
    })
}

/**
 * 保存、修改、删除
 */

const multipleSelection = ref([])
const handleSelectionChange = (val) => {
  multipleSelection.value = val
}

const handleRemoveBatch = () => {
  console.log('handleRemoveBatch')

  if (multipleSelection.value.length < 1) {
    ElMessage({
      type: 'warning',
      message: '请选择要删除的数据!'
    })
    return
  }

  ElMessageBox.confirm('此操作将删除该数据, 是否继续?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    var ids = []
    multipleSelection.value.forEach((item, index) => {
      ids.push(item.id)
    })
    var url = 'pur/PayBank/removeBatch'
    var param = {
      ids: JSON.stringify(ids)
    }
    proxy.$axios.post(url, param).then(response => {
      if (response.data.code == '0') {
        ElMessage({
          type: 'success',
          message: '操作成功!',
        });
        getTableData()
        dialogVisible.value = false
      } else {
        ElMessage({
          type: 'error',
          message: '操作失败!' + response.data.msg
        });
      }
    })
  }).catch(() => {
    ElMessage({
      type: 'info',
      message: '已取消删除'
    })
  })
}

const handleEditBatch = () => {
  console.log('handleEditBatch')
  if (multipleSelection.value.length != 1) {
    ElMessage({
      type: 'warning',
      message: '请选择一条数据编辑!'
    })
    return
  }
  handleEdit(multipleSelection.value[0])
}

const handleUpload = () => {
  console.log('handleUpload')
}

const handleDownload = () => {
  console.log('handleDownload')
}

const handleImport = () => {
  console.log('handleImport')
}

const handleExport = () => {
  console.log('handleExport')
}

</script>

<style></style>