<template>
    <!-- 领料申请主列表 -->
    <div>
        <el-form
            :model="queryParams"
            ref="queryForm"
            size="small"
            :inline="true"
            v-show="showSearch"
            label-width="80px"
        >
            <el-form-item label="关键字" prop="keyWord">
                <el-input
                    v-model="queryParams.keyWord"
                    placeholder="请输入关键字"
                    clearable
                    :maxlength="50"
                    style="width: 240px"
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>

            <el-form-item label="领料单位">
                <el-select
                    v-model="queryParams.unitId"
                    filterable
                    clearable
                    @change="GetProData"
                    style="width: 240px"
                    placeholder="请选择"
                >
                    <el-option
                        v-for="item in unitList"
                        :key="item.id"
                        :label="item.name"
                        :value="item.id"
                    >
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="工程名称">
                <el-select
                    v-model="queryParams.proId"
                    filterable
                    clearable
                    @change="GetUnitData"
                    style="width: 240px"
                    placeholder="请选择"
                >
                    <el-option
                        v-for="item in proList"
                        :key="item.id"
                        :label="item.name"
                        :value="item.id"
                    >
                    </el-option>
                </el-select>
            </el-form-item>

            <el-form-item label="申请日期">
                <el-date-picker
                    v-model="queryParams.time"
                    type="daterange"
                    value-format="yyyy-MM-dd"
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                    style="width: 240px"
                >
                </el-date-picker>
            </el-form-item>
            <el-form-item>
                <el-button
                    type="primary"
                    icon="el-icon-search"
                    size="mini"
                    @click="handleQuery"
                    >查询</el-button
                >
                <el-button
                    icon="el-icon-refresh"
                    size="mini"
                    @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="el-icon-plus"
                    size="mini"
                    @click="handleAdd"
                    v-hasPermi="['picking:apply:apply']"
                    >新建</el-button
                >
            </el-col>
            <el-col :span="1.5">
                <el-button
                    type="warning"
                    plain
                    icon="el-icon-download"
                    size="mini"
                    @click="handleExport"
                    v-hasPermi="['picking:apply:export']"
                    >导出</el-button
                >
            </el-col>
            <!--      <el-col :span="1.5">-->
            <!--        <el-button-->
            <!--          type="danger"-->
            <!--          plain-->
            <!--          icon="el-icon-refresh"-->
            <!--          size="mini"-->
            <!--          @click="handleRefreshCache"-->
            <!--          v-hasPermi="['store:labelType:remove']"-->
            <!--        >刷新缓存</el-button>-->
            <!--      </el-col>-->
            <right-toolbar
                :showSearch.sync="showSearch"
                @queryTable="getList"
            ></right-toolbar>
        </el-row>

        <el-table
            v-loading="loading"
            :data="leaseAuditList"
            @selection-change="handleSelectionChange"
            border
        >
            <!-- <el-table-column type="selection" width="55" align="center" /> -->
            <el-table-column
                align="center"
                label="序号"
                type="index"
                :index="
                    indexContinuation(queryParams.pageNum, queryParams.pageSize)
                "
            />
            <el-table-column
                label="领料单号"
                align="center"
                prop="code"
                :show-overflow-tooltip="true"
                width="150"
            />
            <el-table-column
                label="领料单位"
                align="center"
                prop="unitName"
                :show-overflow-tooltip="true"
            />
            <el-table-column
                label="领料工程"
                align="center"
                prop="proName"
                :show-overflow-tooltip="true"
            />
            <el-table-column
                label="协议号"
                align="center"
                prop="agreementCode"
                :show-overflow-tooltip="true"
            />
            <el-table-column
                label="现场领料人"
                align="center"
                prop="leasePerson"
                :show-overflow-tooltip="true"
            />
            <el-table-column
                label="联系电话"
                align="center"
                prop="leasePhone"
                :show-overflow-tooltip="true"
            />
            <el-table-column
                label="领料申请人"
                align="center"
                prop="applyFor"
                :show-overflow-tooltip="true"
            />
            <el-table-column
                label="申请时间"
                align="center"
                prop="createTimes"
                :show-overflow-tooltip="true"
            />
            <el-table-column
                label="任务状态"
                align="center"
                prop="taskName"
                :show-overflow-tooltip="true"
                width="120px"
            />
            <el-table-column
                label="审批意见 "
                align="center"
                :show-overflow-tooltip="true"
            >
                <template slot-scope="scope">
                    <span v-if="scope.row.taskStatus < 31"></span>
                    <span
                        v-if="
                            scope.row.taskStatus == 31 ||
                            scope.row.taskStatus == 98
                        "
                        >{{
                            scope.row.leaseApplyInfoList[0]
                                .companyAuditRemark || ''
                        }}
                    </span>
                    <span
                        v-if="
                            scope.row.taskStatus == 32 ||
                            scope.row.taskStatus == 99
                        "
                        >{{
                            scope.row.leaseApplyInfoList[0].deptAuditRemark ||
                            ''
                        }}
                    </span>
                    <span
                        v-if="
                            scope.row.taskStatus == 33 ||
                            scope.row.taskStatus == 100
                        "
                        >{{
                            scope.row.leaseApplyInfoList[0].directAuditRemark ||
                            ''
                        }}
                    </span>
                    <span
                        v-if="
                            scope.row.taskStatus > 33 &&
                            scope.row.taskStatus < 98
                        "
                        >{{
                            scope.row.leaseApplyInfoList[0].directAuditRemark ||
                            ''
                        }}</span
                    >
                    <span v-if="scope.row.taskStatus > 100">{{
                        scope.row.leaseApplyInfoList[0].directAuditRemark || ''
                    }}</span>
                    <!-- <span v-else>{{ scope.row.leaseApplyInfoList[0].directAuditRemark }}</span> -->
                </template>
            </el-table-column>
            <el-table-column
                label="备注"
                align="center"
                :show-overflow-tooltip="true"
            >
                <template slot-scope="scope">
                    <span>{{ scope.row.leaseApplyInfoList[0].remark }}</span>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" width="160">
                <template slot-scope="scope">
                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-zoom-in"
                        @click="handleView(scope.row)"
                        v-hasPermi="['picking:apply:view']"
                        >查看</el-button
                    >
                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-edit"
                        v-if="scope.row.taskStatus == 30"
                        @click="handleUpdate(scope.row)"
                        v-hasPermi="['picking:apply:edit']"
                        >编辑</el-button
                    >
                    <el-button
                        size="mini"
                        type="text"
                        v-if="
                            (scope.row.taskStatus == 98 ||
                                scope.row.taskStatus == 99 ||
                                scope.row.taskStatus == 100) &&
                            scope.row.createBy === currentUserName
                        "
                        @click="handleUpdate2(scope.row)"
                        v-hasPermi="['picking:apply:reject:submit']"
                        >驳回提交</el-button
                    >
                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-delete"
                        v-if="scope.row.taskStatus == 30"
                        @click="handleDelete(scope.row)"
                        v-hasPermi="['picking:apply:del']"
                        >删除</el-button
                    >
                </template>
            </el-table-column>
        </el-table>

        <pagination
            v-show="total > 0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            :page-sizes="[5, 10, 15, 20, 30]"
            @pagination="getList"
        />
    </div>
</template>

<script>
import {
    listType,
    getType,
    delType,
    addType,
    updateType,
    refreshCache,
} from '@/api/system/dict/type'
import {
    getLeaseAuditListAll,
    getUnitData,
    getProData,
    deleteTask,
} from '@/api/claimAndRefund/receive'

export default {
    // name: "ReceiveApply",
    dicts: ['sys_normal_disable'],
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 字典表格数据
            leaseAuditList: [],
            // 日期范围
            dateRange: [],
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                dictName: undefined,
                dictType: undefined,
                status: undefined,

                time: null, //申请时间
                unitId: null, //来往单位id
                proId: null, //工程id

                types: 1, // 1申请列表 2审核列表
                keyWord: '',
            },
            unitList: [], //来往单位集合
            proList: [], //工程集合
            currentUserName: sessionStorage.getItem('userName'),
        }
    },
    created() {
        this.getList()
        this.GetUnitData()
        this.GetProData()
    },
    methods: {
        // 获取 申请列表
        async getList() {
            this.loading = true

            const params = {
                unitId: this.queryParams.unitId,
                projectId: this.queryParams.proId,
                keyWord: this.queryParams.keyWord,
                startTime: this.queryParams.time && this.queryParams.time[0],
                endTime: this.queryParams.time && this.queryParams.time[1],
                types: this.queryParams.types,
                souceBy: 0,
                pageSize: this.queryParams.pageSize,
                pageNum: this.queryParams.pageNum,
            }

            const res = await getLeaseAuditListAll(params)
            this.loading = false
            this.leaseAuditList = res.data.rows
            this.total = res.data.total
        },

        // 获取 来往单位 列表数据
        async GetUnitData() {
            const params = {
                id: this.queryParams.proId,
            }
            const res = await getUnitData(params)
            this.unitList = res.data
            console.log('GetUnitData ======================', res)
        },
        // 获取 工程名称 列表数据
        async GetProData() {
            const params = {
                id: this.queryParams.unitId,
            }
            const res = await getProData(params)
            this.proList = res.data
            console.log('GetProData ======================', res)
        },

        // 表单重置
        reset() {
            this.form = {
                dictId: undefined,
                dictName: undefined,
                dictType: undefined,
                status: '0',
                remark: undefined,
            }
            this.resetForm('form')
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1
            this.getList()
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.queryParams.time = []
            this.queryParams.unitId = ''
            this.queryParams.proId = ''
            this.resetForm('queryForm')
            this.GetUnitData()
            this.GetProData()
            this.handleQuery()
        },

        /** 新增按钮操作 */
        handleAdd() {
            // this.$tab.closeOpenPage({
            //     path: '/claimAndRefund/receive/receiveApplyAdd',
            //     query: {
            //         isEdit: true,
            //     },
            // })

            this.$emit('addPicking')
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.dictId)
            this.single = selection.length != 1
            this.multiple = !selection.length
        },
        handleView(row) {
            // this.$tab
            //     .closeOpenPage({
            //         path: '/claimAndRefund/receive/receiveApplyAdd',
            //         query: {
            //             taskId: row.taskId,
            //             isEdit: false,
            //         },
            //     })
            //     .then(() => {
            //         this.$tab.refreshPage()
            //     })

            this.$emit('viewPicking', row.taskId)
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            // this.$tab.closeOpenPage({
            //     path: '/claimAndRefund/receive/receiveApplyAdd',
            //     query: {
            //         taskId: row.taskId,
            //         isEdit: true,
            //     },
            // })
            this.$emit('editPicking', row.taskId)
        },
        /** 驳回按钮操作 */
        handleUpdate2(row) {
            // this.$tab.closeOpenPage({
            //     path: '/claimAndRefund/receive/receiveApplyAdd',
            //     query: {
            //         taskId: row.taskId,
            //         isEdit: true,
            //         isBack: true,
            //     },
            // })
            this.$emit('editPicking', row.taskId, row.taskStatus)
        },
        /** 删除按钮操作 */
        async handleDelete(row) {
            this.$confirm('此操作将永久删除该领料任务, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }).then(async () => {
                const res = await deleteTask(row.taskId)
                if (res.code == 200) {
                    this.$message({
                        type: 'success',
                        message: '删除成功',
                    })

                    this.getList()
                }
            })
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download(
                '/material/base/tm_task/applyExport',
                {
                    ...this.queryParams,
                },
                `领料申请单_${new Date().getTime()}.xlsx`,
            )
        },
        /** 刷新缓存按钮操作 */
        handleRefreshCache() {
            refreshCache().then(() => {
                this.$modal.msgSuccess('刷新成功')
                this.$store.dispatch('dict/cleanDict')
            })
        },
    },
    watch: {
        $route: {
            handler(to) {
                if (to.query.keyWord) {
                    this.queryParams.keyWord = to.query.keyWord
                }
            },
            deep: true,
            immediate: true,
        },
    },
}
</script>
<style lang="scss" scoped>
::v-deep.el-table .fixed-width .el-button--mini {
    width: 60px !important;
    margin-bottom: 10px;
}
</style>
