<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
            <el-form-item label="工厂名称" label-width="120px" prop="orgIndex" v-if="userInfoVO.userType == '00'">
                <el-select v-model="queryParams.orgIndex" filterable placeholder="请选择" clearable>
                    <el-option-group label="工厂">
                        <template>
                            <el-option v-for="factory in factoryList" :key="factory.id + factory.sourceType" :label="factory.name" :value="factory.id + ',' + factory.sourceType"> </el-option>
                        </template>
                    </el-option-group>
                </el-select>
            </el-form-item>
            <el-form-item label="订单编号" prop="orderNo">
                <el-input v-model="queryParams.orderNo" placeholder="请输入订单编号" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="预约时间" prop="appointmentTime">
                <el-date-picker clearable v-model="queryParams.appointmentTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择创建时间"> </el-date-picker>
            </el-form-item>
            <el-form-item label="客户姓名" prop="contactName">
                <el-input v-model="queryParams.contactName" placeholder="请输入客户姓名" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="客户手机号" prop="contactPhone" label-width="90px">
                <el-input v-model="queryParams.contactPhone" placeholder="请输入客户手机号" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
                <el-button icon="el-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="warning" plain icon="el-icon-download" @click="handleExport" v-hasPermi="['order:cloth:export']">导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" fit highlight-current-row border stripe :data="clothList">
            <el-table-column label="预约单号" align="center" prop="orderNo" width="150px" />
            <el-table-column label="预约时间" align="center" prop="appointmentTime" width="100px" />
            <el-table-column label="分配工厂" align="center" prop="orgName" width="160px">
                <template slot-scope="scope">
                    <span v-if="scope.row.orgName == null || scope.row.orgName == ''"> - - </span>
                    <span v-else>
                        {{ scope.row.orgName }}
                    </span>
                </template>
            </el-table-column>
            <el-table-column label="是否大件" align="center" width="100px" v-if="orderStatus == '0' || orderStatus == '1' || orderStatus == '2'">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.isLargeItem" />
                </template>
            </el-table-column>
            <el-table-column label="是否拆装" align="center" width="100px" v-if="orderStatus == '0' || orderStatus == '1' || orderStatus == '2'">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.isDisassemblyAndAssembly" />
                </template>
            </el-table-column>
            <el-table-column label="件数" align="center" prop="appointmentCount" width="80px" />
            <el-table-column label="客户姓名" align="center" prop="contactName" width="100px" />
            <el-table-column label="客户手机号" align="center" prop="contactPhone" width="120px" />
            <el-table-column label="地址明细" align="center" show-overflow-tooltip>
                <template slot-scope="scope">
                    <span>{{ scope.row.province }}{{ scope.row.city }}{{ scope.row.area }}{{ scope.row.address }}{{ scope.row.addressDetail }}</span>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="200">
                <template slot-scope="scope">
                    <el-button type="text" icon="el-icon-edit" @click="handleSelectFactory(scope.row)" v-if="orderStatus == '0' && scope.row.orgId == null">分配工厂</el-button>
                    <el-button type="text" icon="el-icon-edit" @click="handleSelectFactory(scope.row)" v-if="orderStatus == '0' && scope.row.orgId != null">修改工厂</el-button>
                    <el-button type="text" icon="el-icon-delete" @click="handleCancel(scope.row)" v-if="orderStatus == '0' || orderStatus == '1'">取消订单</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="分配工厂" :visible.sync="selectFactoryOpen" width="20%" append-to-body>
            <el-row :gutter="15">
                <el-form ref="form" :model="form" :rules="rules" label-width="70px">
                    <el-col :span="24">
                        <el-form-item label="工厂" prop="factoryId">
                            <el-select v-model="form.factoryId" placeholder="请选择工厂" clearable>
                                <el-option v-for="item in factoryList" :key="item.id" :label="item.name" :value="item.id" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-form>
            </el-row>

            <div>
                <el-button type="primary" @click="submitSelectFactoryForm" style="margin-left: 40%">提交</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { getAppointmentClothOrderList, setDeliveryManByAppointment, cancelAppointmentOrderByAppointment, orderClothSelectOrg } from '@/api/order/cloth'
import { getDeliveryManListByOrg } from '../../../api/app/delivery'
import { allOrg } from '../../../api/system/store'

export default {
    name: 'WorkAppointmentOrder',
    dicts: ['cloth_order_status', 'sys_normal_disable', 'sys_yes_no', 'cloth_take_type'],
    data() {
        return {
            orderType: '0', //0普通订单，1：云洗订单，2：第三方订单
            orderStatus: '0',
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 预约订单表格数据
            clothList: [],
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                orderNo: null,
                contactName: null,
                contactPhone: null,
                orderStatus: null,
                orgIndex: null
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                takeClothAppUserId: [
                    {
                        required: true,
                        message: '派送员不能为空',
                        trigger: 'blur'
                    }
                ]
            },
            deliveryManList: [],
            factoryList: [],
            selectFactoryOpen: false,
            userInfoVO: null
        }
    },
    created() {
        this.userInfoVO = this.getUserInfo()
        var orderType = this.getUrlParam('orderType')
        if (orderType) {
            this.orderType = orderType
        }
        this.getFactoryList()

        this.getList()
    },
    methods: {
        /** 获取所属工厂 */
        getFactoryList() {
            allOrg({ sourceType: '01' }).then((response) => {
                this.factoryList = response.data
            })
        },
        changeOrderStatus() {
            this.queryParams.orderStatus = this.orderStatus
            this.handleQuery()
        },
        /** 查询洗衣订单列表 */
        getList() {
            this.loading = true
            this.queryParams.orderStatus = this.orderStatus
            this.queryParams.orderType = this.orderType

            if (this.queryParams.orgIndex != null) {
                this.queryParams.orgId = this.queryParams.orgIndex.split(',')[0]
                this.queryParams.sourceType = this.queryParams.orgIndex.split(',')[1]
            } else {
                delete this.queryParams.orgId
                delete this.queryParams.sourceType
            }
            getAppointmentClothOrderList(this.queryParams).then((response) => {
                this.clothList = response.rows
                this.total = response.total
                this.loading = false
            })
        },
        // 取消按钮
        cancel() {
            this.open = false
            this.reset()
        },
        // 表单重置
        reset() {
            this.form = {}
            this.resetForm('form')
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1
            this.getList()
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm('queryForm')
            this.handleQuery()
        },
        handleSelectFactory(row) {
            this.reset()
            this.selectFactoryOpen = true
            this.form.orderNo = row.orderNo
        },
        submitSelectFactoryForm() {
            this.$refs['form'].validate((valid) => {
                if (valid) {
                    orderClothSelectOrg({ orgId: this.form.factoryId, sourceType: '01', orderNo: this.form.orderNo }).then((response) => {
                        this.$modal.msgSuccess('分配成功')
                        this.selectFactoryOpen = false
                        this.getList()
                    })
                }
            })
        },
        handleCancel(row) {
            this.$confirm('是否确认取消该预约订单？')
                .then(() => {
                    cancelAppointmentOrderByAppointment({ orderNo: row.orderNo }).then((response) => {
                        this.$modal.msgSuccess('取消完成')
                        this.getList()
                    })
                })
                .catch(() => {})
        },
        checkClose(done) {
            this.$confirm('是否关闭表单，关闭后数据将丢失？')
                .then(function () {
                    done()
                })
                .then(() => {})
                .catch(() => {})
        },
        /** 导出按钮操作 */
        handleExport() {
            if (null != this.createTimeRange && '' != this.createTimeRange) {
                this.queryParams['beginCreateTime'] = this.createTimeRange[0]
                this.queryParams['endCreateTime'] = this.createTimeRange[1]
            } else {
                delete this.queryParams.beginCreateTime
                delete this.queryParams.endCreateTime
            }
            if (this.queryParams.orgIndex != null) {
                this.queryParams.orgId = this.queryParams.orgIndex.split(',')[0]
                this.queryParams.sourceType = this.queryParams.orgIndex.split(',')[1]
            } else {
                delete this.queryParams.orgId
                delete this.queryParams.sourceType
            }
            this.download(
                'mapi/order/cloth/exportByCloud',
                {
                    ...this.queryParams
                },
                `cloth_order_${new Date().getTime()}.xlsx`
            )
        }
    }
}
</script>
