<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
                    maxlength="50"
                    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="proId">
                <el-select
                    v-model="queryParams.proId"
                    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="typeId">
                <el-cascader
                    v-model="queryParams.typeId"
                    :options="deptOptions"
                    :props="{
                        expandTrigger: 'hover',
                        label: 'typeName',
                        value: 'typeId',
                        checkStrictly: true,
                    }"
                    @change="handleNodeClick"
                    aria-placeholder="请选择极具类型"
                    style="width: 240px"
                ></el-cascader>
            </el-form-item> -->
            <el-form-item label="退料申请时间" prop="time">
                <el-date-picker
                    v-model="queryParams.time"
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                    type="date"
                    value-format="yyyy-MM-dd"
                    style="width: 240px"
                ></el-date-picker>
            </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="agreementCode">
                <el-input
                    maxlength="50"
                    v-model="queryParams.agreementCode"
                    placeholder="请选择协议号"
                    clearable
                    style="width: 240px"
                />
            </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="handleAdd"
                    >退料接收</el-button
                >
            </el-col>
            <el-col :span="1.5">
                <el-button
                type="warning"
                plain
                size="mini"
                @click="handleSubmit" 
                >提交</el-button
                >
            </el-col>
            <el-col :span="1.5">
                <el-button
                    type="success"
                    plain
                    icon="el-icon-download"
                    size="mini"
                    @click="handleExport"
                    >导出</el-button
                >
            </el-col>

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

        <el-table
            v-loading="loading"
            :data="typeList"
            @selection-change="handleSelectionChange"
        >
            <!-- <el-table-column type="selection" width="55" align="center" /> -->
            <!-- <el-table-column
                label="序号"
                align="center"
                width="80"
                type="index"
            >
                <template scope="scope">
                    <span>{{
                        (queryParams.pageNum - 1) * 10 + scope.$index + 1
                    }}</span>
                </template>
            </el-table-column> -->
            <el-table-column
                align="center"
                label="序号"
                type="index"
            />
          
            <el-table-column
                label="退料单位"
                align="center"
                prop="unitName"
                :show-overflow-tooltip="true"
            />
            <el-table-column
                label="退料工程"
                align="center"
                prop="lotName"
                :show-overflow-tooltip="true"
            />
            <el-table-column
                label="退料单号"
                align="center"
                prop="code"
                :show-overflow-tooltip="true"
            />
            <el-table-column
                label="物资类型"
                align="center"
                prop="typeName"
                :show-overflow-tooltip="true"
            />
            <el-table-column
                label="退料人"
                align="center"
                prop="backPerson"
                :show-overflow-tooltip="true"
            />
            <el-table-column
                label="退料人电话"
                align="center"
                prop="phone"
                :show-overflow-tooltip="true"
            />
            <el-table-column
                label="创建时间"
                align="center"
                prop="createTime"
                :show-overflow-tooltip="true"
            />
            <el-table-column
                label="创建人"
                align="center"
                prop="createBy"
                :show-overflow-tooltip="true"
            />
            <!-- <el-table-column
                label="协议号"
                align="center"
                prop="agreementCode"
                :show-overflow-tooltip="true"
            /> -->
            <el-table-column
                label="状态"
                align="center"
                :show-overflow-tooltip="true"
            >
                <template slot-scope="scope">
                    <!-- 状态 37-待审核 38-已审核 -->
                    <el-button type="text" v-if="scope.row.taskStatus == '37'"
                        >待审核</el-button
                    >
                    <el-button
                        type="text"
                        style="color: #67c23a"
                        v-if="scope.row.taskStatus == '38'"
                        >已审核</el-button
                    >
                    <el-button type="text" v-if="scope.row.taskStatus == '39'"
                        >退料核查</el-button
                    >
                    <el-button
                        type="text"
                        style="color: #67c23a"
                        v-if="scope.row.taskStatus == '40'"
                        >退料完成</el-button
                    >
                    <el-button
                        type="text"
                        style="color: #67c23a"
                        v-if="scope.row.taskStatus == '101'"
                        >退料驳回</el-button
                    >
                </template>
            </el-table-column>
            <el-table-column
                label="打印状态"
                align="center"
                prop=""
                :show-overflow-tooltip="true"
            >
                <template slot-scope="scope">
                    <div>
                        未打印
                    </div>
                </template>
            </el-table-column>
            <el-table-column
                label="备注"
                align="center"
                prop="remark"
                :show-overflow-tooltip="true"
            />
            <el-table-column
                label="操作"
                align="center"
                class-name="small-padding fixed-width"
                width="250"
            >
                <template slot-scope="scope">
                    <el-button
                        size="mini"
                        plain
                        icon="el-icon-zoom-in"
                        @click="handleSee(scope.row, 'see')"
                        >查看</el-button
                    >
                    <!-- <el-button
            size="mini"
            type="warning"
            plain
            icon="el-icon-edit"
            @click="handlUpdate(scope.row, 'update')" 
            >编辑</el-button
          > -->
                    <el-button
                        size="mini"
                        type="info"
                        v-if="scope.row.taskStatus == '101'"
                        @click="handleUpdate(scope.row)"
                    >
                        驳回提交
                    </el-button>
                    <el-button
                        size="mini"
                        type="danger"
                        icon="el-icon-delete"
                        @click="handleDelete(scope.row)"
                        >删除</el-button
                    >
                    <el-button
                        size="mini"
                        type="info"
                        @click="handlePrint(scope.row)"
                        >退料单</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"
        />

        <!-- 添加或修改参数配置对话框 -->
        <el-dialog
            :title="title"
            :visible.sync="open"
            width="1000px"
            append-to-body
            :close-on-click-modal="false"
        >
            <el-form
                :model="dialogQueryParams"
                ref="queryForm1"
                size="small"
                :inline="true"
                v-show="showSearch"
                label-width="100px"
            >
                <el-row>
                    <el-form-item label="退料单位" prop="unitName">
                        <el-input
                            disabled
                            v-model="dialogQueryParams.unitName"
                            placeholder="请输入退料单位"
                            clearable
                            style="width: 240px"
                        />
                    </el-form-item>
                    <el-form-item label="退料工程" prop="lotName">
                        <el-input
                            disabled
                            v-model="dialogQueryParams.lotName"
                            placeholder="请输入退料工程"
                            clearable
                            style="width: 240px"
                        />
                    </el-form-item>
                    <el-form-item label="退料协议编号" prop="agreementCode">
                        <el-input
                            disabled
                            v-model="dialogQueryParams.agreementCode"
                            placeholder="请输入退料协议编号"
                            clearable
                            style="width: 240px"
                        />
                    </el-form-item>
                    <el-form-item label="预退料时间" prop="backTime">
                        <el-date-picker
                            style="width: 240px"
                            disabled
                            v-model="dialogQueryParams.backTime"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="选择时间"
                        ></el-date-picker>
                    </el-form-item>
                </el-row>
                <el-row>
                    <el-form-item label="退料人" prop="backPerson">
                        <el-input
                            disabled
                            v-model="dialogQueryParams.backPerson"
                            placeholder="请输入退料人"
                            clearable
                            style="width: 240px"
                        />
                    </el-form-item>
                    <el-form-item label="退料人电话" prop="phone">
                        <el-input
                            disabled
                            v-model="dialogQueryParams.phone"
                            placeholder="请输入退料人电话"
                            clearable
                            style="width: 240px"
                        />
                    </el-form-item>
                </el-row>

                <el-row>
                    <el-form-item label="关键字" prop="keyWord">
                        <el-input
                            v-model="dialogQueryParams.keyWord"
                            placeholder="请输入关键字"
                            clearable
                            style="width: 240px"
                        />
                    </el-form-item>
                    <el-form-item>
                        <el-button
                            type="primary"
                            icon="el-icon-search"
                            size="mini"
                            @click="handleQuery1"
                            >查询</el-button
                        >
                        <el-button
                            icon="el-icon-refresh"
                            size="mini"
                            @click="resetQuery1"
                            >重置</el-button
                        >
                    </el-form-item>
                </el-row>
            </el-form>

            <el-table
                v-loading="loading"
                :data="loadingList"
                height="500px"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center" />
                <el-table-column
                    label="序号"
                    align="center"
                    width="80"
                    type="index"
                >
                    <template slot-scope="scope">
                        <span>{{
                            (dialogQueryParams.pageNum - 1) * 10 +
                            scope.$index +
                            1
                        }}</span>
                    </template>
                </el-table-column>
                <el-table-column
                    label="类型名称"
                    align="center"
                    prop="typeName"
                    :show-overflow-tooltip="true"
                />
                <el-table-column
                    label="规格型号"
                    align="center"
                    prop="typeCode"
                    :show-overflow-tooltip="true"
                />
                <!-- <el-table-column
                    label="编码"
                    align="center"
                    prop="maCode"
                    :show-overflow-tooltip="true"
                /> -->
                <el-table-column
                    label="当前在用量"
                    align="center"
                    prop="useNum"
                    :show-overflow-tooltip="true"
                />
                <el-table-column
                    label="退料数量"
                    align="center"
                    prop="num"
                    class-name="small-padding fixed-width"
                    width="200"
                >
                    <!-- <template slot-scope="scope">
            <el-input
              v-model="scope.row.num"
              :disabled="true"
              placeholder="请输入退料数量"
              clearable
            />
          </template> -->
                </el-table-column>
            </el-table>

            <pagination
                v-show="loadingTotal > 0"
                :total="loadingTotal"
                :page.sync="dialogQueryParams.pageNum"
                :limit.sync="dialogQueryParams.pageSize"
                @pagination="getDialogList"
            />

            <div
                slot="footer"
                class="dialog-footer"
                style="text-align: center"
                v-if="loadingType == 'update'"
            >
                <el-button type="primary" @click="submitForm">确定</el-button>
                <el-button @click="cancel">取消</el-button>
            </div>
        </el-dialog>
        <!-- 退料单 -->
        <!-- <dialogFormByCq
            :dialogTitle="title"
            :isShowFlag.sync="isShowOneFlag"
            :priKey="priKey"
            :rowObj="rowObj"
        ></dialogFormByCq> -->
        <!-- 提交 -->
        <el-dialog
            v-loading.fullscreen.lock="fullscreenLoading"
            :title="title"
            :visible.sync="openOne"
            append-to-body
            width="400px"
        >
            <div class="submit_box">
                <div>
                    <i
                        class="el-icon-circle-check"
                        style="color: #ff9900; font-size: 30px"
                    ></i>
                </div>
                <div class="submit_box_title">
                    <div>{{ openTextOne }}</div>
                    <div>{{ openTextTwo }}</div>
                </div>
            </div>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancel">返回</el-button>
                <el-button type="primary" @click="submitOpenOneForm()"
                    >确定</el-button
                >
            </div>
        </el-dialog>

        <el-dialog
            v-loading.fullscreen.lock="fullscreenLoading"
            :title="title"
            :visible.sync="openTwo"
            append-to-body
            width="400px"
        >
            <div class="submit_box_two">
                <div>
                    <i
                        class="el-icon-circle-check"
                        style="color: #00c196; font-size: 30px"
                    ></i>
                </div>
                <div class="submit_box_title">{{ openTextThree }}</div>
            </div>
            <div slot="footer" class="dialog-footer"></div>
        </el-dialog>
    </div>
</template>

<script>
 
// import {
//     ApiGetBackApplyList,
//     getViewByApply,
//     backApplyDelApi,
//     submitBackApplyApi,
// } from '@/api/claimAndRefund/return.js'
// import { getInfo, h } from '@/api/login'
// import {
//     getUnitData,
//     getProData,
//     getAgreementInfoById,
//     // getUseNumByTypeId
// } from '@/api/claimAndRefund/receive.js'
// import dialogFormByCq from '@/views/claimAndRefund/return/dialogFormByCq.vue'
 
export default {
    name: '',
    dicts: ['sys_normal_disable'],
    // components: { dialogFormByCq },
    data() {
        return {
            fullscreenLoading: false,
            type: '',
            isShowOneFlag: false,
            priKey: '',
            // 遮罩层
            loading: false,
            // 选中数组
            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: '',
                proId: '',
            },
            dialogQueryParams: {
                pageNum: 1,
                pageSize: 10,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                dictName: [
                    {
                        required: true,
                        message: '字典名称不能为空',
                        trigger: 'blur',
                    },
                ],
                dictType: [
                    {
                        required: true,
                        message: '字典类型不能为空',
                        trigger: 'blur',
                    },
                ],
            },
            openOne: false,
            openTwo: false,
            openTextOne: '',
            openTextTwo: '',
            openTextThree: '',
            companyId: '',
            deptOptions: [],
            unitList: [],
            proList: [],
            taskStatusList: [
                {
                    name: '待退料审核',
                    id: '37',
                },
                {
                    name: '退料审核通过',
                    id: '38',
                },
                {
                    name: '退料核查中',
                    id: '39',
                },
                {
                    name: '退料完成',
                    id: '40',
                },
                {
                    name: '退料审核驳回',
                    id: '101',
                },
            ],
            rowObj: {},
            loadingType: '',
            loadingTotal: 0,
            loadingList: [],
        }
    },
    created() {
        // this.getList();
        // this.initSelectData()
        // this.InitIGetInfo()
    },
    methods: {
        // claimAndRefund/return/returnApplyAdd
        // claimAndRefund/return/returnApplyAdd
        /** 查询字典类型列表 startTime,结束日期endTime  */
        async getList() {
            this.loading = true
            if (this.queryParams.time && this.queryParams.time.length > 0) {
                this.queryParams.startTime = this.queryParams.time[0]
                this.queryParams.endTime = this.queryParams.time[1]
            }

            try {
                let params = {
                    companyId: this.companyId,
                    ...this.queryParams,
                }
                // const res = await ApiGetBackApplyList(params)
                // this.typeList = res.data.rows
                // this.total = res.data.total
                this.loading = false
            } catch (error) {}
        },
        // 取消按钮
        cancel() {
            this.open = false
            this.reset()
        },
        // 表单重置
        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.resetForm("queryForm");
            this.$refs.queryForm.resetFields()
            this.initSelectData()
            this.handleQuery()
        },
        /** 新增按钮操作 */
        handleAdd() {
            // this.$tab.closeOpenPage('/claimAndRefund/return/returnApplyAddByCq')
            this.$emit('returnApply')
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.dictId)
            this.single = selection.length != 1
            this.multiple = !selection.length
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            // this.$tab.closeOpenPage({
            //     path: '/claimAndRefund/return/returnApplyAddByCq',
            //     query: {
            //         Id: row.id,
            //         isEdit: true,
            //     },
            // })
            this.$emit('rejectSubmit', { id: row.id, agreementId: row.agreementId })
        },
        // 退料单
        handleUpdateOrder() {
            this.title = '查看'
            this.isShowOneFlag = true
        },
        // 提交
        handleSubmit() {
            this.title = '确认操作'
            this.openTextOne = '确认提交退料么?'
            this.openTextTwo = '确认提交退料么?'
            this.openOne = true
        },
        submitOpenOneForm() {
            this.openTextThree = '提交成功！'
            this.openTwo = true
        },
        /** 提交按钮 */
        submitForm: function () {
            this.$refs['queryForm1'].validate((valid) => {
                if (valid) {
                    let backApplyInfo = []
                    for (let i = 0; i < this.loadingList.length; i++) {
                        this.loadingList[i].companyId = this.companyId
                    }
                    backApplyInfo.push({
                        backPerson: '张三',
                        phone: '1234578988',
                        remark: '1425备注',
                        backTime: '2023-12-14',
                        // backPerson: this.dialogQueryParams.backPerson,
                        // phone: this.dialogQueryParams.phone,
                        // remark: this.dialogQueryParams.remark,
                        // backTime: this.dialogQueryParams.backTime,
                        companyId: this.companyId,
                    })
                    let params = {
                        companyId: this.companyId,
                        agreementId: this.rowObj.agreementId,
                        backApplyInfo: JSON.stringify(backApplyInfo),
                        backApplyDetails: JSON.stringify(this.loadingList),
                    }
                    console.log('param----', params)
                    // submitBackApplyApi(params).then((res) => {})

                    // if (this.form.dictId != undefined) {
                    //   updateType(this.form).then(response => {
                    //     this.$modal.msgSuccess("修改成功");
                    //     this.open = false;
                    //     this.getList();
                    //   });
                    // } else {
                    //   addType(this.form).then(response => {
                    //     this.$modal.msgSuccess("新增成功");
                    //     this.open = false;
                    //     this.getList();
                    //   });
                    // }
                } else {
                }
            })
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            let param = {
                id: row.id + '',
            }
            this.$modal
                .confirm('是否确认删除所选择的数据项？')
                .then(() => {
                    // return backApplyDelApi(param)
                })
                .then(() => {
                    this.getList() 
                    this.$message.success('操作成功!')
                })
                .catch(() => { 
                })
        },
        handlePrint(row) {
            this.isShowOneFlag = true
            this.rowObj = row
        },

        handleSee(row, type) {
            this.dialogQueryParams.unitName = row.unitName
            this.dialogQueryParams.lotName = row.lotName
            this.dialogQueryParams.agreementCode = row.agreementCode
            this.dialogQueryParams.backPerson = row.backPerson
            this.dialogQueryParams.phone = row.phone
            this.dialogQueryParams.backTime = row.backTime + ''
            this.$set(this.dialogQueryParams, 'keyWord', '')
            this.loadingType = type
            this.rowObj = row
            this.open = true
            this.getDialogList()
        },
        handlUpdate(row, type) {
            this.loadingType = type
            this.rowObj = row
            this.open = true
            this.getDialogList()
        },

        /** 导出按钮操作 */
        handleExport() {
            this.download(
                'material/backApply/export',
                {
                    ...this.queryParams,
                },
                `退料申请单_${new Date().getTime()}.xlsx`,
            )
        },
        getDialogList() {
            let params = {
                id: this.rowObj.id,
                agreementId: this.rowObj.agreementId,
                keyWord: this.dialogQueryParams.keyWord,
                pageNum: this.dialogQueryParams.pageNum,
                pageSize: this.dialogQueryParams.pageSize,
            }
            // getViewByApply(params).then((res) => {
            //     console.log(res)
            //     this.loadingList = res.data.rows 
            //     this.loadingTotal = res.data.total
            // })
        },
        InitIGetInfo() {
            // getInfo().then((res) => {
            //     this.companyId = res.user.companyId
            //     this.getList()
            // })
            // this.initGetUseTypeTree()
        },
        handleNodeClick(ev) {},
        initSelectData() {
            this.GetUnitData()
            this.GetProData()
        },

        // 获取 来往单位 列表数据
        async GetUnitData() {
            const params = {}
            // const res = await getUnitData(params)
            // this.unitList = res.data
        },
        // 获取 工程名称 列表数据
        async GetProData() {
            const params = {
                id: this.queryParams.unitId,
            }
            // const res = await getProData(params)
            // this.proList = res.data
        },
        // 获取 工程名称 列表数据
        async InitGetAgreementInfoById() {
            const { unitId, proId } = this.queryParams
            if (!unitId || !proId) {
                return
            }
            const params = {
                unitId: unitId,
                projectId: proId,
            }
            // const res = await getAgreementInfoById(params) 
            // this.queryParams.agreementCode = res.data.agreementCode 
        },
        getAgreementByUnitAndProId() {
            this.InitGetAgreementInfoById()
        },
        getAgreementByProId() {
            this.InitGetAgreementInfoById()
        },
        getAgreementByUnit() {
            this.GetProData()
        },
        handleQuery1() {
            this.getDialogList()
        },
        resetQuery1() {
            this.dialogQueryParams.keyWord = ''
            this.getDialogList()
        },
        async initGetUseTypeTree(id) {
            // let params = {
            //     agreementId: id,
            // }
            // const res = await getUseTypeTree(params)
            // console.log('res-getUseTypeTree', res)
            // this.deptOptions = res.data
        },
    },
}
</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>
