<template>
    <!-- 退料接收主列表 -->
    <div>
        <el-form
            :model="queryParams"
            ref="queryForm"
            size="small"
            :inline="true"
            v-show="showSearch"
            label-width="100px"
        >
            <el-form-item label="关键字" prop="keyWord">
                <el-input
                    v-model="queryParams.keyWord"
                    placeholder="请输入关键字"
                    clearable
                    style="width: 240px"
                />
            </el-form-item>
            <el-form-item label="退料单位" prop="unitId">
                <el-select
                    v-model="queryParams.unitId"
                    clearable
                    filterable
                    @change="getAgreementByUnit"
                    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="工程名称" prop="lotId">
                <el-select
                    v-model="queryParams.lotId"
                    clearable
                    filterable
                    @change="getAgreementByProId"
                    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="协议号" prop="agreementCode">
                <el-input
                    v-model="queryParams.agreementCode"
                    placeholder="请选择协议号"
                    clearable
                    disabled
                    style="width: 240px"
                />
            </el-form-item>
            <el-form-item label="工机具类型" prop="typeId">
                <Tree
                    ref="mychildSon"
                    :width="240"
                    :dataList="deptList"
                    @changeId="selectDrop"
                ></Tree>
            </el-form-item>

            <el-form-item label="退料状态" prop="taskStatus">
                <el-select
                    v-model="queryParams.taskStatus"
                    clearable
                    filterable
                    style="width: 240px"
                    placeholder="请选择"
                >
                    <el-option
                        v-for="item in taskStatusList"
                        :key="item.id"
                        :label="item.name"
                        :value="item.id"
                    ></el-option>
                </el-select>
            </el-form-item>

            <el-form-item label="退料申请时间" prop="dateRange">
                <el-date-picker
                    v-model="dateRange"
                    type="datetimerange"
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                    value-format="yyyy-MM-dd"
                    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
            size="mini"
            @click="handleExam"
            :disabled="multiple"
            >批量审核</el-button
          >
        </el-col> -->
            <el-col :span="1.5">
                <el-button
                    type="success"
                    plain
                    icon="el-icon-download"
                    size="mini"
                    @click="handleExport"
                    v-hasPermi="['return:receive:export']"
                    >导出</el-button
                >
            </el-col>

            <right-toolbar
                :showSearch.sync="showSearch"
                @queryTable="getList"
            ></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="typeList" border>
            <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
            />
            <el-table-column
                label="退料单位"
                align="center"
                prop="unitName"
                show-overflow-tooltip
            />
            <el-table-column
                label="退料工程"
                align="center"
                prop="lotName"
                show-overflow-tooltip
            />
            <el-table-column
                label="退料机具"
                align="center"
                prop="typeName"
                show-overflow-tooltip
            />
            <el-table-column
                label="退料人员"
                align="center"
                prop="backPerson"
                show-overflow-tooltip
            />
            <el-table-column
                label="联系电话"
                align="center"
                prop="phone"
                show-overflow-tooltip
            />
            <el-table-column
                label="申请时间"
                align="center"
                prop="backTime"
                show-overflow-tooltip
            />
            <el-table-column
                label="协议号"
                align="center"
                prop="agreementCode"
                show-overflow-tooltip
            />
            <el-table-column
                label="退料状态"
                align="center"
                prop="taskName"
                show-overflow-tooltip
            ></el-table-column>

            <el-table-column label="操作" align="center" width="200">
                <template slot-scope="scope">
                    <el-button
                        size="medium"
                        type="text"
                        icon="el-icon-zoom-in"
                        v-hasPermi="['return:receive:view']"
                        @click="handleSee(scope.row, 'see')"
                        >查看</el-button
                    >
                    <el-button
                        size="medium"
                        type="text"
                        @click="handleReturn(scope.row, 'see')"
                        icon="el-icon-d-arrow-right"
                        v-hasPermi="['return:receive:handler']"
                        v-if="scope.row.taskStatus != '40'"
                    >
                        退料
                    </el-button>
                    <el-button
                        size="medium"
                        type="text"
                        icon="el-icon-tickets"
                        @click="handlePrint(scope.row)"
                        v-hasPermi="['return:receive:list']"
                        >退料单</el-button
                    >
                </template>
            </el-table-column>
        </el-table>

        <pagination
            v-show="total > 0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
        />

        <!-- 退料单 -->
        <dialogForm
            :dialogTitle="title"
            :isShowFlag.sync="isShowOneFlag"
            :rowObj="rowObj"
            :priKey="priKey"
        ></dialogForm>
    </div>
</template>

<script>
import {
    getBackAuditList,
    getViewByExamine,
    ApiBackApplyAudit,
    ApiBackApplyRefuse,
    getBackReceiveList,
} from '@/api/claimAndRefund/return.js'
import { getInfo } from '@/api/login'
import {
    getUnitData,
    getProData,
    getAgreementInfoById,
} from '@/api/claimAndRefund/receive.js'
import dialogForm from '@/views/claimAndRefund/return/dialogFormExame.vue'
import { listPartTypeApi } from '@/api/repairTest/repair'
import Tree from '@/views/claimAndRefund/return/tree.vue'
export default {
    // name: "ReturnExamine",
    components: { dialogForm, Tree },
    data() {
        return {
            type: '',
            isShowOneFlag: false, //退料单
            priKey: '',
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 字典表格数据
            typeList: [],
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            // 日期范围
            dateRange: [],
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                keyWord: '', //关键字
                unitId: '', //单位id
                lotId: '', //工程id
                taskStatus: '', //状态
                typeId: '', //工机具类型
                time: '',
                agreementCode: '', //协议
                startTime: '',
                endTime: '',
            },
            companyId: '',
            deptList: [],
            unitList: [],
            proList: [],
            taskStatusList: [
                {
                    name: '待退料审核',
                    id: '37',
                },
                {
                    name: '退料审核通过',
                    id: '38',
                },
                {
                    name: '退料核查中',
                    id: '39',
                },
                {
                    name: '退料完成',
                    id: '40',
                },
                {
                    name: '退料审核驳回',
                    id: '101',
                },
            ],
            rowObj: {},
        }
    },
    created() {
        this.initSelectData()
        this.InitIGetInfo()
        this.getTree()
    },
    methods: {
        InitIGetInfo() {
            getInfo().then((res) => {
                this.companyId = res.user.companyId
                this.getList()
            })
        },
        getTree() {
            listPartTypeApi().then((response) => {
                console.log(
                    '🚀 ~ listPartTypeApi ~ response.data:',
                    response.data,
                )
                this.deptList = response.data
            })
        },
        //机具类型下拉点击
        selectDrop(value) {
            this.queryParams.typeId = value
        },
        //获取渲染下拉数据
        initSelectData() {
            this.GetUnitData()
            this.GetProData()
        },
        // 获取 来往单位 列表数据
        async GetUnitData() {
            const params = {}
            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)
        },
        // 获取 工程名称 列表数据
        async InitGetAgreementInfoById() {
            const { unitId, proId } = this.queryParams
            if (!unitId || !proId) {
                return
            }
            const params = {
                unitId: unitId,
                projectId: proId,
            }
            const res = await getAgreementInfoById(params)
            // this.proList = res.data
            console.log('getAgreementInfoById ======================', res)
            this.queryParams.agreementCode = res.data.agreementCode
        },
        getAgreementByProId() {
            this.InitGetAgreementInfoById()
        },
        getAgreementByUnit() {
            this.GetProData()
        },
        /** 查询列表 startTime,结束日期endTime  */
        async getList() {
            // this.loading = true;
            this.queryParams.startTime = this.dateRange[0]
            this.queryParams.endTime = this.dateRange[1]
            try {
                let params = {
                    flag: 1,
                    companyId: this.companyId,
                    ...this.queryParams,
                }
                console.log('paramsparamsparams', params)
                const res = await getBackReceiveList(params)
                this.typeList = res.data.rows
                this.total = res.data.total
                this.loading = false
            } catch (error) {}
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1
            this.getList()
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.dateRange = []
            // this.resetForm("queryForm");
            this.$refs.queryForm.resetFields()
            this.$refs.mychildSon.inputValue = ''
            this.initSelectData()
            this.handleQuery()
        },
        //打开退料单
        handlePrint(row) {
            // this.title = "退料单";
            this.isShowOneFlag = true
            this.rowObj = row
        },
        //查看按钮
        handleSee(row) {
            // this.$tab.closeOpenPage({
            //     path: '/claimAndRefund/returnInDetail',
            //     query: {
            //         Id: row.id,
            //         isView: true,
            //     },
            // })

            this.$emit('receiveView', row.id)
        },
        //退料按钮
        handleReturn(row) {
            // this.$tab.closeOpenPage({
            //     path: '/claimAndRefund/returnInDetail',
            //     query: {
            //         Id: row.id,
            //         taskId: row.taskId,
            //     },
            // })
            this.$emit('receiveReturn', row.id, row.taskId)
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download(
                '/material/base/backReceive/export',
                {
                    ...this.queryParams,
                },
                `退料接收_${new Date().getTime()}.xlsx`,
            )
        },
    },
}
</script>
<style lang="scss" scoped>
.submit_box {
    display: flex;
    justify-content: flex-start;
    align-items: center;

    .submit_box_title {
        display: flex;
        flex-direction: column;
        margin-left: 15px;

        :first-child {
            font-size: 14px;
            font-weight: 600;
        }

        :last-child {
            margin-top: 6px;
            font-size: 12px;
        }
    }
}

.submit_box_two {
    display: flex;
    flex-direction: row;
    flex-wrap: nowrap;
    align-items: center;
    margin-left: 30%;

    .submit_box_title {
        display: flex;
        flex-direction: column;
        margin-left: 10px;
        font-size: 18px;
        font-weight: 600;
    }
}

::v-deep.el-table .fixed-width .el-button--mini {
    width: 60px !important;
    margin-bottom: 10px;
}
.dialog-footer {
    display: flex;
    justify-content: flex-end;
    align-items: center;
}
</style>
