<template>
    <div class="">
        <TableBase ref="TableRef" border :columns="columns" :requestApi="Review_Finance_List_Post" :initParam="data.initParam">
            <template #table-header>
                <div style="display: flex;justify-content: space-between;flex: 1;flex-wrap: wrap;">
                    <el-form :inline="true" :model="data.initParam" class="demo-form-inline">
                        <el-form-item label="项目名称">
                            <el-input v-model="data.initParam.T_name" placeholder="搜索项目名称" />
                        </el-form-item>
                        <el-form-item label="状态">
                            <el-select v-model="data.initParam.T_audit" style="width: 160px;" laceholder="请选择状态">
                                <el-option v-for="item in optionReviewFinance" :key="item.value" :label="item.name" :value="item.value" />
                            </el-select>
                        </el-form-item>
                        <el-form-item label="项目负责人">
                            <el-select v-model="data.initParam.T_submit" style="width: 160px;" placeholder="请选择项目负责人">
                                <el-option v-for="item in data.optionSubmit" :key="item.T_submit" :label="item.T_name" :value="item.T_submit" />
                            </el-select>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="onclicksearch">查询</el-button>
                        </el-form-item>
                    </el-form>
                    <el-button type="primary" icon="Download" @click="onclickExcel">导出</el-button>
                </div> 
            </template>
            <template #T_audit="{ row }">
                <el-tag :type="
                    [1,2].includes(row.T_audit)?'warning':
                    [3,5].includes(row.T_audit)?'primary':
                     [4,6].includes(row.T_audit)?'danger':''">
                    {{ 
                        row.T_audit=='1'?'待提交审核':
                        row.T_audit=='2'?'待审核':
                        row.T_audit=='3'?'财务通过':
                        row.T_audit=='4'?'财务驳回':
                        row.T_audit=='5'?'总经理通过':
                        row.T_audit=='6'?'总经理驳回':''
                    }}
                </el-tag>
            </template>
            <template #T_have_brokerage_fee="{ row }">
                <el-tag :type="row.T_have_brokerage_fee?'primary':'success'">
                    {{ row.T_have_brokerage_fee?'是':'否' }}
                </el-tag>
            </template>
            <template #right="{ row }">
                <el-button link type="primary" size="small" :icon="View" @click="openDrawer('详情', row)">详情</el-button>
                <el-button link type="primary" size="small" :disabled="[2].includes(row.T_audit)?false:true" 
                icon="Check" @click="openDrawer('审核', row)" >审核</el-button>
                <el-button link type="danger" size="small" :icon="Delete" @click="openDelect(row)">删除</el-button>
            </template>
        </TableBase>
        <particulars ref="btnRef" :fromData="data.fromData" :audit="<any>[3,4]" types="Finance" @successFun="successFun"></particulars>
    </div>
</template>

<script setup lang='ts'>
import { ref, reactive,onMounted } from 'vue';
import { View,Delete} from '@element-plus/icons-vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { GlobalStore } from '@/stores/index'
import particulars from './modules/particulars.vue'
import { optionReviewFinance } from './modules/optionsData'

import { 
    Review_Submit_User_Post,//项目负责人
    Review_Finance_List_Post,//财务列表
    Review_Excel_Post,//导出
    ContractReview_Del_Post,//删除
    ContractReview_Submit_Post} from '@/api/contractReview/index'

import TableBase from '@/components/TableBase/index.vue'

const globalStore = GlobalStore()
const TableRef = ref()
const btnRef = ref()

const successFun = async (data:any)=>{
    TableRef.value?.getTableList()
}

const data = reactive({
    initParam :{
        // User_tokey: globalStore.GET_User_tokey,
        T_name: '',
        T_submit: '',
        T_audit: '',
    },
    fromData:{},
    optionSubmit:<any>[]
})
// 搜索以及参数
const columns = [
    { type: 'index', label: '序号', width: 80, align: 'center ' },
    { prop: 'T_audit', label: '状态', name: 'T_audit'},
    { prop: 'T_name', label: '项目名称' },
    { prop: 'T_address', label: '项目地址' },
    { prop: 'T_submit_name', label: '项目负责人' },
    { prop: 'T_predict_sign_time', label: '预计签约时间' },
    { prop: 'T_money', label: '总金额' },
    { prop: 'T_discount_money', label: '最终优惠金额' },
    { prop: 'T_have_brokerage_fee', label: '是否有居间费', name: 'T_have_brokerage_fee' },
    { prop: 'T_brokerage_fee_money', label: '居间费金额' },
    { prop: 'operation', label: '操作', fixed: 'right',width:'300px' }
]

const onclicksearch = (row: any) => {
    TableRef.value?.getTableList()
}

/**
 * Excel
 */
const onclickExcel = async ()=>{
    const result:any = await Review_Excel_Post(data.initParam)
    if (result.Code === 200) {
        window.open(result.Data)
  }
    console.log('导出',result)
}

/**
 * 编辑
 * @param tit 
 * @param row 
 */
 const openDrawer = (tit:string,row: any) => {
    btnRef.value.outerVisible = true
    btnRef.value.data.drawerTiti = tit
    data.fromData = row
}
/**
 * 项目负责人
 * @param row 
 */
const SubmituserApi = async() => {
    const result:any = await Review_Submit_User_Post({T_type:'Finance'})
    if(result.Code==200){
        console.log('项目负责人',result)
        let obj = { T_name: "全部", T_submit:""}
        let arr = result.Data.Data || []
        data.optionSubmit = [obj,...arr]
    }
}
/**
 * 删除
 * @param row 
 */
 const openDelect = (row: any) => {
    ElMessageBox.confirm('删除操作，是否执行操作?','删除',{
        confirmButtonText: '立即删除',
        cancelButtonText: '取消',
        type: 'warning',
        center: true,
    }).then(async() => {
        const result:any = await ContractReview_Del_Post({T_id:row.Id})
        if(result.Code==200){
            ElMessage.success('删除成功')
            TableRef.value?.getTableList()
        }
    }).catch(() => {})
}
onMounted(()=>{
    SubmituserApi()
})
</script>
<style lang="scss">
/* @import url(); 引入css类 */
</style>