<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="placeName">
        <el-input
          v-model="queryParams.placeName"
          placeholder="请输入地点名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @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"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['canteen:place:add']"
        >新增
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="placeList"
      row-key="placeId"
      default-expand-all
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column label="父id" prop="parentId" v-if="false"/>
      <el-table-column label="祖级列表" align="center" prop="ancestors" v-if="false"/>
      <el-table-column label="地点" align="center" prop="placeName"/>
      <el-table-column label="位置类型" align="center" prop="placeType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.place_type" :value="scope.row.placeType"/>
        </template>
      </el-table-column>
      <el-table-column label="具体位置" align="center" prop="placeSite"/>
      <el-table-column label="位置编号" align="center" prop="placeNumber"/>
      <el-table-column label="经纬坐标" align="center" prop="coordinate"/>
      <el-table-column label="机构代码" align="center" prop="jgid"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['canteen:place:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
            v-hasPermi="['canteen:place:add']"
          >新增
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['canteen:place:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改地点对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="上级目录" prop="parentId">
          <treeselect v-model="form.parentId" :options="placeOptions" :normalizer="normalizer" placeholder="请选择父id"
                      disabled/>
        </el-form-item>
        <el-form-item label="位置名称" prop="placeName">
          <el-input v-model="form.placeName" placeholder="请输入地点名称"/>
        </el-form-item>
        <el-form-item label="具体位置" prop="placeSite">
          <el-input v-model="form.placeSite" placeholder="请输入具体位置"/>
        </el-form-item>
        <el-form-item label="位置图片" prop="placeSite">
          <ImageUpload v-model="form.placeImgUrl"></ImageUpload>
        </el-form-item>
        <el-form-item label="详情" prop="placeSite">
          <editor v-model="form.details" :min-height="192"/>
        </el-form-item>
        <el-form-item label="位置类型" prop="placeType">
          <el-select v-model="form.placeType" placeholder="请选择位置类型" :disabled="usFlag">
            <el-option
              v-for="dict in dict.type.place_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="经纬坐标" prop="coordinate">
          <el-input v-model="form.coordinate" placeholder="请输入经纬坐标"/>
        </el-form-item>
        <el-form-item label="机构代码" prop="jgid">
          <el-input v-model="form.jgid" placeholder="请输入机构代码" disabled/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPlace, getPlace, delPlace, addPlace, updatePlace, exportPlace } from '@/api/canteen/place'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import user from '@/store/modules/user'

export default {
  name: 'Place',
  dicts: ['place_type'],
  components: {
    Treeselect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 地点表格数据
      placeList: [],
      // 地点树选项
      placeOptions: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        placeName: null,
        jgid: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      usFlag: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询地点列表 */
    getList() {
      this.loading = true
      this.queryParams.jgid = user.state.organ
      listPlace(this.queryParams).then(response => {
        this.placeList = this.handleTree(response.data, 'placeId', 'parentId')
        this.loading = false
      })
    },
    /** 转换地点数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children
      }
      return {
        id: node.placeId,
        label: node.placeName,
        children: node.children
      }
    },
    /** 查询地点下拉树结构 */
    getTreeselect() {
      listPlace({ jgid: user.state.organ }).then(response => {
        this.placeOptions = []
        const data = { placeId: 0, placeName: '顶级节点', children: [] }
        data.children = this.handleTree(response.data, 'placeId', 'parentId')
        this.placeOptions.push(data)
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        placeId: null,
        parentId: null,
        placeName: null,
        jgid: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset()
      this.getTreeselect()
      if (row != null && row.placeId) {
        this.form.parentId = row.placeId
        this.usFlag = true
        this.form.placeType = row.placeType
      } else {
        this.form.parentId = 0
        this.usFlag = false
      }
      this.open = true
      this.title = '添加地点'
      this.form.jgid = user.state.organ
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      this.getTreeselect()
      if (row != null) {
        this.form.parentId = row.placeId
      }
      getPlace(row.placeId).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改地点'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.placeId != null) {
            updatePlace(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addPlace(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除地点编号为"' + row.placeId + '"的数据项？').then(function() {
        return delPlace(row.placeId)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    }
  }
}
</script>
