<template>
  <div class='app-container'>
    <el-form inline>
      <el-form-item label="门店名称或编码：">
        <el-input v-model="data.form.name" placeholder="请输入门店名称或编码" clearable />
      </el-form-item>
      <el-form-item label="门店分组：">
        <el-select v-model="data.form.groupId" placeholder="请选择门店分组" filterable remote :remote-method="groupRemote"
          remote-show-suffix>
          <el-option v-for="item in groupData.options" :key="item.id" :value="item.id" :label="item.name" />
        </el-select>
      </el-form-item>
      <el-form-item label="所属企业：">
        <el-select v-model="data.form.tenantId" placeholder="请选择所属企业" style="width: 200px" filterable remote
          :remote-method="businessRemote" remote-show-suffix clearable>
          <el-option v-for="item in businessData.options" :key="item.id" :value="item.id" :label="item.name" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="getSearch">搜索</el-button>
        <el-button icon="Refresh" @click="getRefresh">重置</el-button>
        <el-button type="primary" plain icon="Plus" @click="getDialog()">新增</el-button>
      </el-form-item>
    </el-form>
    <!-- 列表 -->
    <el-table :data="data.tableData" v-loading="loading">
      <el-table-column label="门店名称" prop="name" align="center"></el-table-column>
      <el-table-column label="门店编码" prop="code" align="center"></el-table-column>
      <el-table-column label="门店地区" align="center">
        <template #default="scope">
          {{ scope.row.provinceName }} - {{ scope.row.cityName }} - {{ scope.row.areaName }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button type="primary" link @click="getDialog(scope.row.id)">编辑</el-button>
          <el-button type="danger" link @click="getDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" v-model:page="data.form.pageNum" v-model:limit="data.form.pageSize"
      @pagination="getList" />
    <!-- 弹窗 -->
    <el-dialog v-model="dialogVisible" :title="title" width="500" :before-close="getClose">
      <el-form label-width="100px" :model="data.dialogForm" ref="dialogForm" :rules="data.rules">
        <el-form-item label="门店名称：" prop="name">
          <el-input v-model="data.dialogForm.name" placeholder="请输入门店名称" />
        </el-form-item>
        <el-form-item label="门店编码：" prop="code">
          <el-input v-model="data.dialogForm.code" placeholder="请输入门店编码" />
        </el-form-item>
        <el-form-item label="所属账号：" prop="userId">
          <el-select v-model="data.dialogForm.userId" placeholder="请选择所属账号" filterable remote
            :remote-method="userRemote" remote-show-suffix>
            <el-option v-for="item in userData.options" :key="item.userId" :value="item.userId"
              :label="item.userName" />
          </el-select>
        </el-form-item>
        <el-form-item label="所属企业：" prop="tenantId">
          <el-select v-model="data.dialogForm.tenantId" placeholder="请选择所属企业" filterable remote
            :remote-method="businessRemote" remote-show-suffix>
            <el-option v-for="item in businessData.options" :key="item.id" :value="item.id" :label="item.name" />
          </el-select>
        </el-form-item>
        <el-form-item label="所属城市：" prop="provinceId">
          <el-cascader v-model="data.areaList" :options="areaData.options" placeholder="请选择所属城市"
            :props="{ value: 'id', label: 'name', children: 'childList', expandTrigger: 'hover' }"
            style="width: 100%;" />
        </el-form-item>
        <el-form-item label="门店分组：" prop="groupId">
          <el-select v-model="data.dialogForm.groupId" placeholder="请选择门店分组" filterable remote
            :remote-method="groupRemote" remote-show-suffix>
            <el-option v-for="item in groupData.options" :key="item.id" :value="item.id" :label="item.name" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="getClose">取消</el-button>
        <el-button type="primary" @click="getSubmit">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { list, detail, remove, submit, edit } from '@/api/store/list'

import { useBusinessSelect, useUserSelect, useGroupSelect, useAreaSelect } from '@/hooks/index'

const { proxy } = getCurrentInstance()

const { businessData, getBusiness, businessRemote } = useBusinessSelect(true)
getBusiness()
const { userData, getUser, userRemote } = useUserSelect()
getUser()
const { groupData, getGroup, groupRemote } = useGroupSelect()
getGroup()
const { areaData, getArea } = useAreaSelect()
getArea()

// 遮罩层
const loading = ref(false)

const data = reactive({
  // 表单
  form: {
    pageNum: 1,
    pageSize: 10
  },
  // 列表
  tableData: [],
  // 弹窗表单
  dialogForm: {},
  // 地区
  areaList: [],
  // 校验
  rules: {
    name: [{
      required: true, message: '请输入门店名称', trigger: 'blur'
    }],
    code: [{
      required: true, message: '请输入门店编码', trigger: 'blur'
    }],
    userId: [{
      required: true, message: '请选择所属账号', trigger: 'change'
    }],
    tenantId: [{
      required: true, message: '请选择所属企业', trigger: 'change'
    }],
    provinceId: [{
      required: true, message: '请选择所属城市', trigger: 'blur'
    }],
    groupId: [{
      required: true, message: '请选择门店分组', trigger: 'change'
    }]
  }
})

// 总数据
const total = ref(0)

// 列表
const getList = () => {
  loading.value = true
  list(data.form).then(res => {
    if (res.code === 0) {
      data.tableData = res.data
      total.value = res.total
      loading.value = false
    }
  })
}
getList()

// 搜索
const getSearch = () => {
  data.form.pageNum = 1
  getList()
}

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

// 弹窗
const dialogVisible = ref(false)
// 弹窗标题
const title = ref('')
// 打开弹窗
function getDialog(id) {
  dialogVisible.value = true
  title.value = '新增'
  if (id) {
    getDetail(id)
  }
}

// 详情
const getDetail = (id) => {
  title.value = '编辑'
  detail({ id: id }).then(res => {
    if (res.code === 0) {
      data.dialogForm = res.data
      data.areaList = [res.data.provinceId, res.data.cityId, res.data.areaId]
    }
  })
}

// 删除
const getDelete = (row) => {
  proxy.$modal.confirm(`是否删除门店名称为：${row.name}的数据?`).then(() => {
    remove({ id: row.id }).then(res => {
      if (res.code === 0) {
        proxy.$modal.msgSuccess('删除成功！')
        getList()
      }
    })
  })
}

// 提交
const getSubmit = () => {
  proxy.$refs.dialogForm.validate(valid => {
    if (valid) {
      if (data.dialogForm.id) {
        edit(data.dialogForm).then(res => {
          if (res.code === 0) {
            proxy.$modal.msgSuccess('编辑成功！')
            getClose()
            getList()
          }
        })
      } else {
        submit(data.dialogForm).then(res => {
          if (res.code === 0) {
            proxy.$modal.msgSuccess('新增成功！')
            getClose()
            getList()
          }
        })
      }
    } else {
      return false
    }
  })
}

// 取消
const getClose = () => {
  dialogVisible.value = false
  data.dialogForm = {}
  data.areaList = []
  proxy.$refs.dialogForm.resetFields()
}

watch(() => data.areaList, (val) => {
  if (val) {
    data.dialogForm.provinceId = val[0]
    data.dialogForm.cityId = val[1]
    data.dialogForm.areaId = val[2]
  }
})
</script>