<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item prop="factoryName">
                <el-input v-model="queryParams.factoryName" placeholder="请输入工厂名称" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>

            <el-form-item style="margin-left: 10px">
                <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 style="margin-left: 10px">
                <el-select size="small" style="width: 200px" v-model="city" placeholder="请选择城市" v-on:change="changeCity">
                    <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 prop="area" style="margin-left: 10px">
                <el-select size="small" style="width: 200px" v-model="area" placeholder="请选择区（县）">
                    <el-option v-for="item in areas" :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:factory:add']">新增 </el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
        <Page uri="/system/factory/list" :request-params="queryParams" ref="pagination">
            <el-table-column label="id" align="center" prop="id" />
            <el-table-column label="工厂名称" align="center" prop="factoryName" />
            <el-table-column label="负责人名称" align="center" prop="applyName" />
            <el-table-column label="负责人-身份证号码" align="center" prop="applyIdCard" v-if="false" />
            <el-table-column label="负责人-联系方式" align="center" prop="applyPhone" />
            <el-table-column label="经度" align="center" prop="orgLon" v-if="false" />
            <el-table-column label="纬度" align="center" prop="orgLat" v-if="false" />
            <el-table-column label="所属省份" align="center" prop="province" />
            <el-table-column label="所属城市" align="center" prop="city" />
            <el-table-column label="所属地区" align="center" prop="area" />
            <el-table-column label="地址" align="center" prop="address" :show-overflow-tooltip="true" />

            <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">
                <template slot-scope="scope">
                    <el-button type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:factory:edit']">修改 </el-button>
                    <el-button type="text" 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-dialog :title="title" :visible.sync="open" direction="rtl" size="50%" append-to-body @opened="onDialogOpened" destroy-on-close>
            <Add_factory ref="AddFactory" @closeDialog="closeDialog" />
        </el-dialog>
    </div>
</template>

<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>
