<template>
  <div class="app-container organization-manage">
    <!-- 搜索 -->
    <el-form :model="state.queryParams" ref="queryRef" :inline="true">
      <el-form-item label="菜单名称" prop="paysortname">
        <el-input v-model="state.queryParams.paysortname" placeholder="账号/公司名称" clearable @keyup.enter="handleQuery({initPageIndex: true})" />
      </el-form-item>
      <img src="@/assets/images/phone-head.png" alt="" />
      <!-- 按钮组件 -->
      <all-button
        :buttonType="['add', '', 'query', 'refresh']"
        @addButton="accountEditRef.openDialog()"
        @queryButton="handleQuery({initPageIndex: true})"
        @refreshButton="handleQuery({initPageIndex: true})"
      />
    </el-form>
    <!-- 表格 -->
    <el-table v-loading="state.tableLoading" :data="state.tableData" row-key="id" style="flex: 1" stripe border table-layout="fixed" @cell-dblclick="tableCelldbl">
      <el-table-column align="center" prop="paysortid" label="支付类别id" min-width="150" sortable></el-table-column>
      <el-table-column align="center" prop="paysortname" label="支付名称" min-width="150"></el-table-column>
      <el-table-column align="center" prop="paysortcustomerid" label="关联归属人员id" min-width="150">
        <template #default="scope">
          <span>{{ scope.row[scope.column.property] ? scope.row[scope.column.property] : '-' }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="paysortwxappid" label="微信appid" min-width="150"></el-table-column>
      <el-table-column align="center" prop="paysortwxencodingaeskey" label="微信消息密钥" min-width="150"></el-table-column>
      <el-table-column align="center" prop="paysortalipayuid" label="支付宝商户id" min-width="150"></el-table-column>
      <el-table-column align="center" prop="paysortsfky" label="产品状态" min-width="150">
        <template #default="scope">
          <el-tag v-if="scope.row[scope.column.property] == 1">可用</el-tag>
          <el-tag v-if="scope.row[scope.column.property] == 0">不可用</el-tag>
        </template>
      </el-table-column>

      <el-table-column align="center" prop="paysortsfky" label="产品是否可用" min-width="120">
        <template #default="scope">
          <el-switch
            v-model="scope.row[scope.column.property]"
            :active-value="1"
            :inactive-value="0"
            :loading="scope.row.loading"
            @change="availableChange(scope.row, $event, scope.column.property)"
          ></el-switch>
        </template>
      </el-table-column>

      <el-table-column align="center" prop="paysortcreateuser" label="创建人" min-width="150"></el-table-column>
      <el-table-column align="center" prop="paysortcreatedate" label="创建时间" min-width="150"></el-table-column>
      <el-table-column align="center" :width="fitWidth" label="操作" class-name="small-padding fixed-width" fixed="right">
        <template #default="scope">
          <div class="operation-box">
            <el-text class="operate-btn-item" type="primary" @click="accountEditRef.openDialog(scope.row)">编辑</el-text>
            <!-- <el-text class="operate-btn-item" type="danger" @click="handleDictDel(scope.row)">删除</el-text> -->
          </div>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-box">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="paginaionConfig.query.pageindex"
        :page-sizes="paginaionConfig.sizeOptions"
        :page-size="paginaionConfig.query.rows"
        layout="total, sizes, prev, pager, next, jumper"
        :total="paginaionConfig.total"
      >
      </el-pagination>
    </div>

    <paymentEdit width="50%" ref="accountEditRef" @refresh="handleQuery" />
  </div>
</template>

<script setup name="Account">
import wxmenuStoreApi from '@store/modules/wxmenuStoreApi'
import useSystemManageStore from '@store/modules/systemManage'
import paymentEdit from '../components/paymentEdit/index.vue'
import _ from 'lodash'
import usePagination from '@/hooks/usePagination'
import useFitColumn from '@/hooks/useFitColumn'
import tableCelldbl from '@/hooks/tableCelldbl'

const {proxy} = getCurrentInstance()
const systemManageStore = useSystemManageStore()
const {paginaionConfig, handleSizeChange, handleCurrentChange} = usePagination(handleQuery)
const {fitWidth} = useFitColumn()
// 接口封装文件
const productManageStore = wxmenuStoreApi()

const state = reactive({
  tableLoading: false,
  tableData: [],
  queryParams: {
    paysortname: ''
  }
})

// pageindex=1&rows=15&paysortname=&showfield=paysortid,paysortname,paysortcustomerid,paysortwxappid,paysortwxmchid,paysortwxappsecret,paysortwxkey,paysortwxencodingaeskey,paysortwxtoken,paysortalipayuid,paysortalipayappid,paysortalipayprivekey,paysortalipaypublickey,paysortsfky,paysortcreatedate,paysortcreateuser,paysortupdate,paysortupuser&entityname=paysort
// components组件
const accountEditRef = ref(null)

// 角色组信息
const roleList = ref([])
provide('roleList', readonly(roleList))

// 重置搜索项
const resetButton = () => {
  state.queryParams = {}
  handleQuery({initPageIndex: true})
}

// 默认信息
const getRoleList = async () => {
  const {Total, Data} = await systemManageStore.sysusergroupLoad({
    pageindex: 1,
    rows: 1000
  })
  roleList.value = Data
}

function handleQuery(config) {
  if (config?.initPageIndex) paginaionConfig.query.pageindex = 1
  getList(Object.assign(_.cloneDeep(paginaionConfig.query), state.queryParams))
}

// 列表中的开关
const availableChange = async (row, newVal, cell, event) => {
  row.loading = true
  try {
    await productManageStore.up({'paysortid.value': row.paysortid, paysortsfky: newVal})
    row.loading = false
    proxy.$modal.msgSuccess('操作成功！')
  } catch (error) {
    handleQuery()
  }
}

/** 查询菜单列表 */
const getList = async (query) => {
  state.tableLoading = true
  const {Total, Data} = await productManageStore.list(query)
  paginaionConfig.total = Total
  state.tableData = Data
  state.tableLoading = false
}

/** 删除 */
const handleDictDel = async (query) => {
  try {
    let params = JSON.parse(JSON.stringify(query))
    await proxy.$modal.confirm('请问确认删除吗？')
    await productManageStore.del(params)
    proxy.$modal.msgSuccess('删除成功！')
    handleQuery()
  } catch (error) {
    console.log(error, 'error')
  }
}

onMounted(() => {
  handleQuery({initPageIndex: true})
  // 其他信息
  // getRoleList()
})
</script>
