<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item prop="name">
                <el-input v-model="queryParams.name" placeholder="请输入门店名称" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item style="margin-left: 10px" v-if="userInfoVO.userType == '00'">
                <el-select size="small" style="width: 200px" v-model="queryParams.factoryId" placeholder="请选择所属工厂" clearable>
                    <el-option v-for="item in factorys" :label="item.factoryName" :value="item.id" :key="item.factoryName"> </el-option>
                </el-select>
            </el-form-item>
            <el-form-item style="margin-left: 10px">
                <el-select v-model="queryParams.storeType" placeholder="请选择门店类型" clearable>
                    <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>
                <el-select size="small" style="width: 200px" v-model="province" placeholder="请选择省份（直辖市）" v-on:change="changeProvince()">
                    <el-option v-for="item in provinces" :label="item.areaName" :value="item.areaId" :key="item.areaName"> </el-option>
                </el-select>
            </el-form-item>

            <el-form-item>
                <el-select size="small" style="width: 200px" v-model="city" placeholder="请选择城市">
                    <el-option v-for="item in citys" :label="item.areaName" :value="item.areaId" :key="item.areaName"> </el-option>
                </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-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="id" v-if="false" />
            <el-table-column label="门店编号" align="center" prop="code" />
            <el-table-column label="门店名称" align="center" prop="name" />
            <el-table-column label="所属工厂" align="center" prop="factoryName" />
            <el-table-column label="门店类型" align="center" prop="storeType">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.sys_store_type" :value="scope.row.storeType" />
                </template>
            </el-table-column>
            <!--			<el-table-column label="所属区域" align="center" prop="bindAreaId" />-->
            <el-table-column label="负责人姓名" align="center" prop="contactName" />
            <el-table-column label="负责人电话" align="center" prop="contactWorkTel" />
            <el-table-column label="省份名" align="center" prop="provinceName" />
            <el-table-column label="城市名" align="center" prop="cityName" />
            <el-table-column label="开店时间" align="center" prop="openDate" width="180">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.openDate, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="开始营业时间" align="center" style="width: 100px" prop="businessStartTime" />
            <el-table-column label="结束营业时间" align="center" prop="businessEndTime" />
            <el-table-column label="状态" align="center" key="status">
                <template slot-scope="scope">
                    <el-switch v-model="scope.row.status" active-value="0" inactive-value="1" @change="handleStatusChange(scope.row)"></el-switch>
                </template>
            </el-table-column>

            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" 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" direction="rtl" size="50%" append-to-body @opened="onDialogOpened" destroy-on-close>
            <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>
