<template>
    <div class="app-container">
        <el-card class="box-card" shadow="never">
            <el-form :model="queryParams" ref="queryForm" @submit.native.prevent :inline="true" v-show="showSearch" label-width="85px">
                <el-form-item label="工厂名称" prop="factoryName">
                    <el-input v-model="queryParams.factoryName" placeholder="请输入工厂名称" clearable style="width: 240px">
                        <i slot="prefix" class="el-icon-office-building"></i>
                    </el-input>
                </el-form-item>

                <el-form-item label="所在地区">
                    <div class="area-select">
                        <el-select size="medium" v-model="province" placeholder="请选择省份" v-on:change="changeProvince()">
                            <el-option v-for="item in provinces" :label="item.areaName" :value="item.areaId" :key="item.areaName">
                                <i class="el-icon-location"></i>
                                <span>{{ item.areaName }}</span>
                            </el-option>
                        </el-select>
                        <el-select size="medium" v-model="city" placeholder="请选择城市" v-on:change="changeCity">
                            <el-option v-for="item in citys" :label="item.areaName" :value="item.areaId" :key="item.areaName">
                                <i class="el-icon-map-location"></i>
                                <span>{{ item.areaName }}</span>
                            </el-option>
                        </el-select>
                        <el-select size="medium" v-model="area" placeholder="请选择区县">
                            <el-option v-for="item in areas" :label="item.areaName" :value="item.areaId" :key="item.areaName">
                                <i class="el-icon-position"></i>
                                <span>{{ item.areaName }}</span>
                            </el-option>
                        </el-select>
                    </div>
                </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-card>

        <el-card class="box-card table-card" shadow="never">
            <div slot="header" class="clearfix">
                <el-row :gutter="10" class="mb8">
                    <el-col :span="1.5">
                        <el-button type="primary" plain icon="el-icon-plus" @click="handleAdd" v-hasPermi="['system:factory:add']">新增工厂</el-button>
                    </el-col>
                    <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
                </el-row>
            </div>

            <Page uri="/system/factory/list" :request-params="queryParams" ref="pagination">
              <el-table-column label="ID" align="center" prop="id" width="100" />
              <el-table-column label="工厂名称" align="center" prop="factoryName" width="100" />
              <el-table-column label="负责人姓名" align="center" prop="applyName" width="100" />
              <el-table-column label="负责人电话" align="center" prop="applyPhone" width="100" />
              <el-table-column label="工厂地区" align="left" min-width="200">
                <template slot-scope="scope">
                  <div class="factory-info">
                    <div class="factory-address">
                      <i class="el-icon-location"></i>
                      <span>{{ scope.row.province }} {{ scope.row.city }} {{ scope.row.area }}</span>
                    </div>
                  </div>
                </template>
              </el-table-column>
                <el-table-column label="详细地址" align="left" min-width="240">
                    <template slot-scope="scope">
                        <el-tooltip :content="scope.row.address" placement="top" effect="light">
                            <div class="address-info">
                                <i class="el-icon-map-location"></i>
                                <span>{{ scope.row.address || "暂未设置" }}</span>
                            </div>
                        </el-tooltip>
                    </template>
                </el-table-column>
                <el-table-column label="状态" align="center" width="100">
                    <template slot-scope="scope">
                        <el-switch v-model="scope.row.status" active-value="0" inactive-value="1" active-color="#13ce66" inactive-color="#ff4949" @change="handleStatusChange(scope.row)" />
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center" width="160" class-name="small-padding fixed-width">
                    <template slot-scope="scope">
                        <el-button type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:factory:edit']">修改</el-button>
                        <el-divider direction="vertical" />
                        <el-button type="text" class="delete-btn" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['system:factory:remove']" v-if="scope.row.id !== 1">删除</el-button>
                    </template>
                </el-table-column>
            </Page>
        </el-card>

        <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body @opened="onDialogOpened" destroy-on-close :before-close="checkClose" :close-on-click-modal="false">
            <Add_factory ref="AddFactory" @closeDialog="closeDialog" />
        </el-dialog>
    </div>
</template>

<style lang="scss" scoped>
.app-container {
    padding: 20px;
    background-color: #f5f7fa;

    .box-card {
        margin-bottom: 20px;
        border-radius: 8px;

        &.table-card {
            margin-bottom: 0;
        }

        :deep(.el-card__header) {
            padding: 15px 20px;
            border-bottom: 1px solid #ebeef5;
        }

        :deep(.el-card__body) {
            padding: 20px;
        }
    }

    .area-select {
        display: flex;
        gap: 8px;

        .el-select {
            width: 160px;
        }
    }

    .factory-info {
        .factory-main {
            margin-bottom: 8px;

            .factory-id {
                color: #909399;
                margin-right: 12px;
                font-size: 13px;
            }

            .factory-name {
                font-size: 15px;
                font-weight: 500;
                color: #303133;
            }
        }

        .factory-address {
            color: #606266;
            font-size: 13px;

            i {
                margin-right: 4px;
                color: #909399;
            }
        }
    }

    .contact-info {
        .name {
            font-weight: 500;
            margin-bottom: 6px;
            color: #303133;

            i {
                margin-right: 4px;
                color: #409eff;
            }
        }

        .phone {
            color: #409eff;
            font-size: 13px;

            i {
                margin-right: 4px;
            }
        }
    }

    .address-info {
        display: flex;
        align-items: center;
        color: #606266;

        i {
            margin-right: 6px;
            color: #909399;
        }

        span {
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
        }
    }

    :deep(.el-table) {
        .el-button {
            padding: 2px 0;
            margin: 0 5px;

            &.delete-btn {
                color: #f56c6c;

                &:hover {
                    color: #f89898;
                }
            }
        }
    }

    :deep(.el-form-item) {
        margin-bottom: 18px;

        &:last-child {
            margin-bottom: 0;
        }
    }
}

// 弹窗样式优化
:deep(.el-dialog) {
    border-radius: 8px;

    .el-dialog__header {
        padding: 20px;
        border-bottom: 1px solid #ebeef5;
    }

    .el-dialog__body {
        padding: 20px;
    }

    .el-dialog__footer {
        padding: 15px 20px;
        border-top: 1px solid #ebeef5;
    }
}
</style>

<script>
import request from '@/utils/request'
import Add_factory from '@/views/system/factory/add_factory.vue'

export function listArea(query) {
    return request({
        url: '/system/area/list',
        method: 'get',
        params: query
    })
}

import { listFactory, getFactory, delFactory, addFactory, updateFactory, changeFactoryStatus } from '@/api/system/factory'

export default {
    name: 'Factory',
    components: {
        Add_factory
    },
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 工厂 基本信息表格数据
            factoryList: [],
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                factoryName: null,
                applyName: null,
                applyIdCard: null,
                applyPhone: null,
                address: null,
                orgLon: null,
                orgLat: null,
                province: null,
                city: null,
                area: null,
                content: null,
                businessLicensePic: null,
                factoryPic: null,
                status: null
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                factoryName: [
                    {
                        required: true,
                        message: '请输入工厂名称',
                        trigger: 'blur'
                    }
                ]
            },
            province: '', // 省
            city: '', // 市
            area: '', // 区
            provinces: [],
            citys: [],
            areas: []
        }
    },
    created() {
        this.getList()
        this.getProvince()
        // 监听自定义的 refreshList 事件
        this.$root.$on('refreshList', () => {
            this.getList()
        })
    },
    methods: {
        checkClose(done) {
            this.$confirm('是否关闭表单，关闭后数据将丢失？')
                .then(function () {
                    done()
                })
                .then(() => {})
                .catch(() => {})
        },
        /** 查询工厂 基本信息列表 */
        getList() {
            this.queryParams.provinceId = this.province
            this.queryParams.cityId = this.city
            this.queryParams.areaId = this.area
            this.$nextTick(() => {
                this.$refs.pagination.handleSearch(true)
            })
        },
        // 工厂状态修改
        handleStatusChange(row) {
            let text = row.status === '0' ? '启用' : '停用'
            this.$modal
                .confirm('确认要' + text + ' ' + row.factoryName + ' 吗？')
                .then(function () {
                    return changeFactoryStatus(row.id, row.status)
                })
                .then(() => {
                    this.$modal.msgSuccess(text + '成功')
                })
                .catch(function () {
                    row.status = row.status === '0' ? '1' : '0'
                })
        },
        /** 获取省份 */
        getProvince() {
            let provinceParams = {
                level: 1
            }
            listArea(provinceParams).then((response) => {
                this.provinces = response.data
            })
        },
        /** 获取城市 */
        changeProvince() {
            let cityParams = {
                parentId: this.province
            }
            listArea(cityParams).then((response) => {
                this.citys = response.data
            })
            if (this.city != null) {
                this.city = null
                this.area = null
                this.areas = []
            }
        },
        /** 获取区县 */
        changeCity() {
            let cityParams = {
                parentId: this.city
            }
            listArea(cityParams).then((response) => {
                this.areas = response.data
            })
            if (this.area != null) {
                this.area = null
            }
        },
        // 取消按钮
        cancel() {
            this.open = false
            this.reset()
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                factoryName: null,
                applyName: null,
                applyIdCard: null,
                applyPhone: null,
                address: null,
                orgLon: null,
                orgLat: null,
                province: null,
                city: null,
                area: null,
                content: null,
                businessLicensePic: null,
                factoryPic: null,
                status: null,
                delFlag: null,
                createBy: null,
                createTime: null,
                updateBy: null,
                updateTime: null,
                remark: null
            }
            this.resetForm('form')
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.getList()
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.province = ''
            this.city = ''
            this.citys = []
            this.area = ''
            this.areas = []
            this.resetForm('queryForm')
            this.handleQuery()
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset()
            this.open = true
            this.id = ''
            this.title = '新增工厂'
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset()
            this.open = true
            this.title = '修改工厂'
            this.id = row.id
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids
            this.$modal
                .confirm('是否确认删除工厂名称为"' + row.factoryName + '"的数据项？')
                .then(function () {
                    return delFactory(ids)
                })
                .then(() => {
                    this.getList()
                    this.$modal.msgSuccess('删除成功')
                })
                .catch(() => {})
        },
        onDialogOpened() {
            this.$refs.AddFactory.setInitData(this.id)
        },
        closeDialog() {
            this.open = false
        }
    },
    destroyed() {
        // 取消事件监听
        this.$root.$off('getList')
    }
}
</script>
