<template>
  <div class='app-container'>
    <!-- 搜索 -->
    <el-form inline size="mini">
      <el-form-item label="门店名称：">
        <el-input v-model="form.name" placeholder="请输入门店名称" clearable />
      </el-form-item>
      <el-form-item label="城市筛选：">
        <el-cascader v-model="form.list" :options="options" placeholder="请选择省市区" clearable
          :props="{ value: 'id', label: 'name', children: 'childList', checkStrictly: true }" />
      </el-form-item>
      <el-form-item label="当前状态：">
        <el-select v-model="form.status" placeholder="请选择当前状态" clearable>
          <el-option v-for="item in statusOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="getSearch">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="getRefresh">重置</el-button>
        <el-button type="primary" icon="el-icon-plus" plain @click="getDetail()"
          v-hasPermi="['operation:map:add']">新增</el-button>
      </el-form-item>
    </el-form>
    <!-- 列表 -->
    <el-table :data="tableData" v-loading="loading">
      <el-table-column label="序号" type="index" align="center" />
      <el-table-column label="门店名称" prop="name" align="center" show-overflow-tooltip />
      <el-table-column label="联系电话" prop="linkPhone" align="center">
        <template slot-scope="scope">
          {{ scope.row.linkPhone ? scope.row.linkPhone : '暂无联系电话' }}
        </template>
      </el-table-column>
      <el-table-column label="门店图片" align="center" width="100px">
        <template slot-scope="scope">
          <el-image :src="scope.row.icon" />
        </template>
      </el-table-column>
      <el-table-column label="地区" align="center" :formatter="areaFormatter" />
      <el-table-column label="详细地址" prop="address" align="center" show-overflow-tooltip />
      <el-table-column label="当前状态" prop="status" align="center" :formatter="statusFormatter" />
      <el-table-column label="创建时间" prop="createTime" align="center" />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="text" @click="getDetail(scope.row.id, true)">查看</el-button>
          <el-button v-if="scope.row.status === 0" type="text" @click="getChange(scope.row.id, 1, '禁用')"
            v-hasPermi="['operation:map:down']">禁用</el-button>
          <span v-else style="margin-left: 10px;">
            <el-button type="text" @click="getDetail(scope.row.id)" v-hasPermi="['operation:map:edit']">编辑</el-button>
            <el-button type="text" @click="getChange(scope.row.id, 0, '启用')"
              v-hasPermi="['operation:map:up']">启用</el-button>
            <el-button type="delete" @click="getDelete(scope.row)" v-hasPermi="['operation:map:delete']">删除</el-button>
          </span>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="form.pageNum" :limit.sync="form.pageSize"
      @pagination="getList" />
  </div>
</template>

<script>
import { list, options, change, remove } from '@/api/operation/map'
export default {
  data() {
    return {
      // 遮罩层
      loading: false,
      // 表单
      form: {
        pageNum: 1,
        pageSize: 10,
        list: []
      },
      // 总数据
      total: 0,
      // 省市区
      options: [],
      // 列表
      tableData: [],
      // 当前状态
      statusOptions: [{
        value: 0,
        label: '启用'
      }, {
        value: 1,
        label: '禁用'
      }]
    }
  },
  watch: {
    'form.list'(val) {
      this.form.province = val[0]
      this.form.city = val[1]
      this.form.area = val[2]
    }
  },
  async mounted() {
    await this.getOptions()
    this.getList()
  },
  methods: {
    // 列表
    getList() {
      this.loading = true
      list(this.form).then(res => {
        if (res.code === 0) {
          this.tableData = res.data.records
          this.total = res.data.total
          this.loading = false
        }
      })
    },

    // 搜索
    getSearch() {
      this.form.pageNum = 1
      this.getList()
    },

    // 重置
    getRefresh() {
      this.form = {
        pageNum: 1,
        pageSize: 10
      }
      this.getList()
    },

    // 省市区
    getOptions() {
      return new Promise((reslove, reject) => {
        options().then(res => {
          if (res.code === 0) {
            reslove(this.options = res.data)
          }
        })
      })
    },

    // 新增、 编辑、 查看
    getDetail(id, boolean) {
      this.$router.push({
        path: `/operation/map/detail`,
        query: {
          id: id,
          boolean: boolean
        }
      })
    },

    // 启用 禁用
    getChange(id, status, title) {
      change(id, status).then(res => {
        if (res.code === 0) {
          this.$message.success(`${title}成功！`)
          this.getList()
        }
      })
    },

    // 删除
    getDelete(row) {
      this.$confirm(`是否删除${row.name}?`, '提示', {
        type: 'warning'
      }).then(() => {
        remove(row.id).then(res => {
          if (res.code === 0) {
            this.$message.success('删除成功！')
            this.getList()
          }
        })
      })
    },

    // 字典翻译
    areaFormatter(row) {
      let province = this.options.find(i => i.id == row.province)
      let city = province.childList.find(i => i.id == row.city)
      let area = city.childList.find(i => i.id == row.area)
      return `${province.name}-${city.name}-${area.name}`
    },

    statusFormatter(row) {
      return this.selectDictLabel(this.statusOptions, row.status)
    }
  }
}
</script>