<template>
    <div class="app-container">
        <!-- 搜索区域 -->
        <el-card class="search-card" shadow="never">
            <el-form :model="queryParams" ref="queryForm" @submit.native.prevent :inline="true" label-width="80px">
                <el-form-item label="门店名称" prop="name">
                    <el-input v-model="queryParams.name" placeholder="请输入门店名称" clearable prefix-icon="el-icon-office-building" style="width: 220px" @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item label="所属工厂" v-if="userInfoVO.userType == '00'">
                    <el-select v-model="queryParams.factoryId" placeholder="请选择所属工厂" clearable style="width: 220px">
                        <el-option v-for="item in factorys" :label="item.factoryName" :value="item.id" :key="item.factoryName" />
                    </el-select>
                </el-form-item>
              <el-form-item label="门店状态">
                <el-select v-model="queryParams.status" placeholder="请选择门店状态" clearable style="width: 220px">
                  <el-option :key="0" label="启用" value="0" />
                  <el-option :key="1" label="禁用" value="1" />
                </el-select>
              </el-form-item>
                <el-form-item label="门店类型">
                    <el-select v-model="queryParams.storeType" placeholder="请选择门店类型" clearable style="width: 220px">
                        <el-option v-for="dict in dict.type.sys_store_type" :key="dict.value" :label="dict.label" :value="dict.value" />
                    </el-select>
                </el-form-item>
                <el-form-item label="所在地区">
                    <el-select v-model="province" placeholder="请选择省份" @change="changeProvince" clearable style="width: 160px">
                        <el-option v-for="item in provinces" :label="item.areaName" :value="item.areaId" :key="item.areaName" />
                    </el-select>
                    <el-select v-model="city" placeholder="请选择城市" clearable style="width: 160px; margin-left: 8px">
                        <el-option v-for="item in citys" :label="item.areaName" :value="item.areaId" :key="item.areaName" />
                    </el-select>
                </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-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" @click="handleAdd" v-hasPermi="['system:store:add']">新增门店</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <!-- 门店列表 -->
        <Page uri="/system/store/list" :request-params="queryParams" ref="pagination">
            <el-table-column label="门店编号" align="center" prop="code" width="100" />
            <el-table-column label="门店信息" align="left" min-width="200">
                <template slot-scope="scope">
                    <div class="store-info">
                        <div class="store-name">{{ scope.row.name }}</div>
                        <div class="store-factory text-gray" v-if="scope.row.factoryName">
                            <i class="el-icon-office-building"></i>
                            {{ scope.row.factoryName }}
                        </div>
                    </div>
                </template>
            </el-table-column>
          <el-table-column label="门店类型" align="left" width="120">
            <template slot-scope="scope">
              <div class="store-info">
                <div class="store-type">
                  <dict-tag :options="dict.type.sys_store_type" :value="scope.row.storeType" />
                </div>
              </div>
            </template>
          </el-table-column>
            <el-table-column label="状态" align="left" width="100">
                <template slot-scope="scope">
                  <el-tag size="small" :type="scope.row.status === '0' ? 'success' : 'info'" style="margin-left: 8px">
                                {{ scope.row.status === '0' ? '正常' : '禁用' }}
                            </el-tag>
                </template>
            </el-table-column>
            <el-table-column label="负责人信息" align="left" width="200">
                <template slot-scope="scope">
                    <div class="contact-info">
                        <div class="contact-name">姓名：{{ scope.row.contactName }}</div>
                        <div class="contact-phone text-gray">
                            电话：{{ scope.row.contactWorkTel }}
                        </div>
                    </div>
                </template>
            </el-table-column>
<!--            <el-table-column label="所在地区" align="center" width="200">-->
<!--                <template slot-scope="scope">-->
<!--                    <div>{{ scope.row.provinceName }} {{ scope.row.cityName }}</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="200">
                <template slot-scope="scope">
                    <div class="text-gray">{{ scope.row.businessStartTime }} ~ {{ scope.row.businessEndTime }}</div>
                </template>
            </el-table-column>
            <el-table-column label="创建时间" align="center" width="200">
                <template slot-scope="scope">
                    <div>{{ parseTime(scope.row.openDate, '{y}-{m}-{d}') }}</div>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" width="180" fixed="right" v-if="userInfoVO.userType == '00'">
                <template slot-scope="scope">
                    <el-button type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:store:edit']">编辑</el-button>
                    <el-button type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['system:store:remove']">删除</el-button>
                </template>
            </el-table-column>
        </Page>

        <!-- 新增/编辑弹窗 -->
        <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body @opened="onDialogOpened"   :before-close="checkClose" :close-on-click-modal="false" destroy-on-close class="store-dialog">
            <Add_store ref="AddStore" @closeDialog="closeDialog" />
        </el-dialog>
    </div>
</template>

<script>
import { listStore, getStore, delStore, addStore, updateStore, changeStoreStatus } from '@/api/system/store'
import { listFactory } from '@/api/system/factory'
import { listArea } from '@/api/system/area'
import Add_store from '@/views/system/store/add_store.vue'

export default {
    name: 'Store',
    components: {
        Add_store
    },
    dicts: ['sys_store_type'],
    data() {
        return {
            dialogLoading: true,
            loadingText: '查询中',
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 门店表格数据
            storeList: [],
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                name: null,
                factoryId: null,
                storeType: null,
                bindAreaId: null,
                status: null
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {},
            factorys: [],

            province: '', // 省
            city: '', // 市

            provinces: [],
            citys: [],
            userInfoVO: null
        }
    },
    created() {
        this.userInfoVO = this.getUserInfo()
        if (this.userInfoVO.userType == '00') {
            this.getFactoryList()
        }
        this.getList()
        this.getProvince()
        // 监听自定义的 refreshList 事件
        this.$root.$on('refreshList', () => {
            this.getList()
        })
    },
    methods: {
        checkClose(done) {
            this.$confirm('是否关闭表单，关闭后数据将丢失？')
                .then(function () {
                    done()
                })
                .then(() => {})
                .catch(() => {})
        },
        /** 获取所属工厂 */
        getFactoryList() {
            const params = {
                pageNum: 1,
                pageSize: 99
            }
            listFactory(params).then((response) => {
                this.factorys = response.rows
            })
        },
        /** 获取省份 */
        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 = []
            }
        },
        // 门店状态修改
        handleStatusChange(row) {
            let text = row.status === '0' ? '启用' : '停用'
            this.$modal
                .confirm('确认要' + text + ' ' + row.name + ' 吗？')
                .then(function () {
                    return changeStoreStatus(row.id, row.status)
                })
                .then(() => {
                    this.$modal.msgSuccess(text + '成功')
                })
                .catch(function () {
                    row.status = row.status === '0' ? '1' : '0'
                })
        },
        /** 查询门店列表 */
        getList() {
            this.queryParams.provinceId = this.province
            this.queryParams.cityId = this.city
            this.$nextTick(() => {
                this.$refs.pagination.handleSearch(true)
            })
        },
        // 取消按钮
        cancel() {
            this.open = false
            this.reset()
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                name: null,
                factoryId: null,
                storeType: null,
                bindAreaId: null,
                isPrint: null,
                isSettlement: null,
                isCarCodePrintName: null,
                printNum: null,
                isPrintName: null,
                contactName: null,
                contactPost: null,
                contactIdcard: null,
                contactWorkTel: null,
                contactPhone: null,
                contactFax: null,
                contactZipCode: null,
                contactEmail: null,
                provinceId: null,
                provinceName: null,
                cityId: null,
                cityName: null,
                areaId: null,
                areaName: null,
                address: null,
                addressDetail: null,
                latitude: null,
                longitude: null,
                officeArea: null,
                openDate: null,
                businessStartTime: null,
                businessEndTime: null,
                companyType: null,
                companyDetail: null,
                contactQq: null,
                companyPic: null,
                companyLicencePic: null,
                createById: null,
                createBy: null,
                createTime: null,
                updateById: null,
                updateBy: null,
                updateTime: null,
                remark: null,
                status: '0'
            }
            this.resetForm('form')
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.getList()
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm('queryForm')
            this.province = ''
            this.city = ''
            this.citys = []
            this.handleQuery()
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset()
            this.open = true
            this.title = '添加门店'
            this.id = ''
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset()
            this.id = row.id
            this.open = true
            this.title = '修改门店'
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids
            this.$modal
                .confirm('是否确认删除门店名称为"' + row.name + '"的数据项？')
                .then(function () {
                    return delStore(ids)
                })
                .then(() => {
                    this.getList()
                    this.$modal.msgSuccess('删除成功')
                })
                .catch(() => {})
        },
        onDialogOpened() {
            this.$refs.AddStore.setInitData(this.id)
        },
        closeDialog() {
            this.open = false
        }
    }
}
</script>

<style lang="scss" scoped>
.app-container {
    .search-card {
        margin-bottom: 16px;
    }

    .store-info {
        padding: 8px 0;

        .store-name {
            font-size: 15px;
            font-weight: 500;
            margin-bottom: 8px;
        }

        .store-type {
            margin-bottom: 8px;
        }

        .store-factory {
            font-size: 13px;

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

    .contact-info {
        .contact-name {
            font-weight: 500;
            margin-bottom: 4px;
        }

        .contact-phone {
            font-size: 13px;

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

.text-gray {
    color: #909399;
}

.store-dialog {
    :deep(.el-dialog__body) {
        padding: 20px;
    }
}
</style>
