<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
            <!-- <el-form-item label="ID" prop="companyId">
        <el-input v-model="queryParams.companyId" placeholder="请输入ID" clearable @keyup.enter="handleQuery" />
      </el-form-item> -->
            <el-form-item label="企业名称" prop="title">
                <el-input v-model="queryParams.title" placeholder="请输入企业名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="状态" prop="status">
                <el-select v-model="queryParams.status" class="m-2" placeholder="请选择状态" clearable @keyup.enter.native="handleQuery">
                    <el-option v-for="option in options" :key="option.value" :label="option.label" :value="option.value" />
                </el-select>
            </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-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['VRdemo:company:add']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate" v-hasPermi="['VRdemo:company:edit']">修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['VRdemo:company:remove']">删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['VRdemo:company:export']">导出</el-button>
            </el-col>
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

        <el-table v-loading="loading" :data="companyList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="序号" align="center" prop="companyId" width="55" />
            <el-table-column label="企业名称" align="center" prop="title" />
            <el-table-column label="余额" align="center" prop="money" width="150">
                <template #default="scope">
                    <span>{{ formatPrice(scope.row.money) }} 元</span>
                </template>
            </el-table-column>
            <el-table-column label="累计充值金额" align="center" prop="totalMoney" width="150">
                <template #default="scope">
                    <span>{{ scope.row.totalMoney }} 元</span>
                </template>
            </el-table-column>
            <el-table-column label="联系电话" align="center" prop="phone" width="120" />
            <el-table-column label="邮箱" align="center" prop="email" width="120" />
            <el-table-column label="状态" align="center" prop="status" width="120">
                <template #default="scope">
                    <el-tag v-if="scope.row.status === '1'" round type="success">审核通过</el-tag>
                    <el-tag v-else-if="scope.row.status === '0'" round type="danger">未审核通过</el-tag>
                    <el-tag v-else-if="scope.row.status === '-1'" round type="info">已注销</el-tag>
                </template>
            </el-table-column>
            <el-table-column label="创建时间" align="center" prop="createTime" width="180">
                <template #default="scope">
                    <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template #default="scope">
                    <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['VRdemo:company:edit']">修改</el-button>
                    <el-button link type="primary" icon="Edit" @click="handleUpdateMoney(scope.row)" v-hasRole="['admin']">充值</el-button>
                    <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['VRdemo:company:remove']">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
    <el-table v-loading="loading" :data="companyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="companyId" width="55"/>
      <el-table-column label="企业名称" align="center" prop="title" />
      <el-table-column label="余额" align="center" prop="money" width="150">
        <template #default="scope">
          <span>{{ formatPrice(scope.row.money) }} 元</span>
        </template>
      </el-table-column>
      <el-table-column label="累计充值金额" align="center" prop="totalMoney" width="150">
        <template #default="scope">
          <span>{{ scope.row.totalMoney }} 元</span>
        </template>
      </el-table-column>
      <el-table-column label="联系电话" align="center" prop="phone" width="120"/>
      <el-table-column label="邮箱" align="center" prop="email" width="120"/>
      <el-table-column label="状态" align="center" prop="status" width="120">
      <template #default="scope">
        <el-tag v-if="scope.row.status === '1'" round type="success">审核通过</el-tag>
        <el-tag v-else-if="scope.row.status === '0'" round type="danger">未审核通过</el-tag>
        <el-tag v-else-if="scope.row.status === '-1'" round type="info">已注销</el-tag>
      </template>
    </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            >修改</el-button>
          <el-button link type="primary" icon="Edit" @click="handleUpdateMoney(scope.row)"
            >充值</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            >删除</el-button>
        </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-dialog :title="title" v-model="open" width="500px" append-to-body>
            <el-form ref="companyRef" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="企业名称" prop="title">
                    <el-input v-model="form.title" placeholder="请输入企业名称" />
                </el-form-item>
                <el-form-item label="联系电话" prop="phone">
                    <el-input v-model="form.phone" placeholder="请输入联系电话" />
                </el-form-item>
                <el-form-item label="邮箱" prop="email">
                    <el-input v-model="form.email" placeholder="请输入邮箱" />
                </el-form-item>
                <el-form-item label="状态" prop="status">
                    <el-select v-model="form.status" class="m-2" placeholder="请选择">
                        <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
                    </el-select>
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="submitForm">确 定</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </div>
            </template>
        </el-dialog>

        <el-dialog :title="title" v-model="openupdateMoney" width="500px" append-to-body>
            <el-form ref="companyRef" :model="form" :rules="rules" label-width="120px">
                <el-form-item label="金额" prop="charge"> <el-input v-model="form.charge" placeholder="请输入充值金额" style="width: 240px" />元 </el-form-item>

                <el-form-item label="支付类型" prop="payType">
                    <el-select v-model="form.payType" placeholder="支付类型" clearable style="width: 240px">
                        <el-option v-for="dict in pay_type" :key="dict.value" :label="dict.label" :value="dict.value" />
                    </el-select>
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="submitForm">确 定</el-button>
                    <el-button @click="cancelupdateMoney">取 消</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script setup name="Company">
import { listCompany, getCompany, delCompany, addCompany, updateCompany, updateCompanyMoney } from '@/api/VRdemo/company'

const { proxy } = getCurrentInstance()
const { vr_company_status } = proxy.useDict('vr_company_status')
const { pay_type } = proxy.useDict('pay_type')

const companyList = 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 openupdateMoney = ref(false)

const data = reactive({
    form: {},
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyId: null,
        title: null,
        status: null,
    },

    rules: {
        title: [{ required: true, message: '企业名称不能为空', trigger: 'blur' }],
        phone: [
            { required: true, message: '联系电话不能为空', trigger: 'blur' },
            { pattern: /^1[3456789]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' },
        ],
        email: [
            { required: false, trigger: 'blur' },
            { pattern: /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/, message: '请输入正确的邮箱', trigger: 'blur' },
        ],
        status: [{ required: true, message: '状态不能为空', trigger: 'change' }],
    },
})

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

// 查询状态选择
const options = [
    { value: '0', label: '未审核通过' },
    { value: '1', label: '已审核通过' },
    { value: '-1', label: '已注销' },
]

//格式化金额
const formatPrice = (value) => {
    return value.toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 })
}

/** 查询企业管理列表 */
function getList() {
    loading.value = true
    listCompany(queryParams.value).then((response) => {
        companyList.value = response.rows
        total.value = response.total
        loading.value = false
    })
}

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

// 表单重置
function reset() {
    form.value = {
        companyId: null,
        title: null,
        money: null,
        totalMoney: null,
        phone: null,
        email: null,
        status: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        payType: null,
        updateTime: null,
    }
    proxy.resetForm('companyRef')
}

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

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

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

/** 新增按钮操作 */
function handleAdd() {
    reset()
    open.value = true
    title.value = '添加企业管理'
}

/** 修改按钮操作 */
function handleUpdate(row) {
    reset()
    const _companyId = row.companyId || ids.value
    getCompany(_companyId).then((response) => {
        form.value = response.data
        open.value = true
        title.value = '修改企业管理'
    })
}

function cancelupdateMoney() {
    openupdateMoney.value = false
    reset()
}

function handleUpdateMoney(row) {
    reset()
    const _companyId = row.companyId || ids.value
    getCompany(_companyId).then((response) => {
        form.value = response.data
        openupdateMoney.value = true
        title.value = '修改【企业管理员信息】'
    })
}

/** 提交按钮 */
function submitForm() {
    proxy.$refs['companyRef'].validate((valid) => {
        if (valid) {
            if (form.value.companyId != null) {
                if (open.value == true) {
                    updateCompany(form.value).then((response) => {
                        proxy.$modal.msgSuccess('修改成功')
                        open.value = false
                        getList()
                    })
                }
                if (openupdateMoney.value == true) {
                    updateCompanyMoney({
                        companyId: form.value.companyId,
                        charge: form.value.charge,
                        payType: form.value.payType,
                    }).then((response) => {
                        proxy.$modal.msgSuccess('充值成功')
                        openupdateMoney.value = false
                        getList()
                    })
                }
            } else {
                addCompany(form.value).then((response) => {
                    proxy.$modal.msgSuccess('新增成功')
                    open.value = false
                    getList()
                })
            }
        }
    })
}

/** 删除按钮操作 */
function handleDelete(row) {
    const _companyIds = row.companyId || ids.value
    proxy.$modal
        .confirm('是否确认删除编号为"' + _companyIds + '"的数据项？')
        .then(function () {
            return delCompany(_companyIds)
        })
        .then(() => {
            getList()
            proxy.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
    proxy.download(
        'VRdemo/company/export',
        {
            ...queryParams.value,
        },
        `company_${new Date().getTime()}.xlsx`
    )
}

getList()
</script>

<style>
.status-disabled {
    color: rgb(243, 104, 104);
    /* 设置禁用状态的文本颜色为红色 */
}

.status-enabled {
    color: rgb(110, 235, 110);
    /* 设置启用状态的文本颜色为绿色 */
}
</style>
