<template>
  <div class="app-container">
    <el-form :model="search" ref="queryForm" :inline="true" v-show="showSearch" :size="ELsize" label-width="auto">
      <el-form-item label="自提点名称" prop="pointName">
        <el-input v-model="search.pointName" placeholder="请输入自提点名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="自提点负责人" prop="receiver">
        <el-input v-model="search.receiver" placeholder="请输入自提点负责人" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="负责人电话" prop="phone">
        <el-input v-model="search.phone" placeholder="请输入负责人电话" clearable @keyup.enter.native="handleQuery" />
      </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" icon="el-icon-plus" :size="ELsize" @click="handleAdd()">新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="dataList" :border="TableBorder" :size="ELTableSize">
      <el-table-column type="index" width="55" label="序号" align="center">
        <template slot-scope="{$index}">
          {{ $getTableIndex(queryParams, $index + 1) }}
        </template>
      </el-table-column>
      <el-table-column label="自提点名称" align="center" prop="pointName" show-overflow-tooltip></el-table-column>
      <el-table-column label="自提点负责人" align="center" prop="receiver"></el-table-column>
      <el-table-column label="负责人电话" align="center" prop="phone"></el-table-column>
      <el-table-column label="详细地址" align="center" prop="address" show-overflow-tooltip></el-table-column>
      <el-table-column label="邮编" align="center" prop="postalCode"></el-table-column>
      <el-table-column label="默认" align="center" prop="openStatus">
        <template slot-scope="scope">
          {{ scope.row.defaultStatus ? "是" : "否" }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button :size="ELTableSize" type="text" @click="handleAdd(scope.row)">编辑</el-button>
          <el-button :size="ELTableSize" type="text" @click="handleDelete(scope.row)">删除</el-button>
          <el-button v-if="!scope.row.defaultStatus" :size="ELTableSize" type="text"
            @click="handleSetDefault(scope.row)">设为默认</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <addEdit ref="addEditRef" :areaTrea="areaTrea" @queryTable="getList" />
  </div>
</template>
<script>
import getListApi from "@/mixins/getListApi";
import {
  businessSelfPickupAddressPage,
  businessSelfPickupAddressDelete,
  businessSelfPickupAddressInfo,
  businessSetDefaultStatus,
  businessSetStatus,
} from "@/api/pickupAddress";
import { cnAreaTree } from "@/api/shop.js";
import addEdit from "./addEdit.vue"; // 导入组件

export default {
  mixins: [getListApi],
  components: { addEdit },
  data() {
    return {
      listURL: businessSelfPickupAddressPage,
      areaTrea: [],
    };
  },
  created() {
    this.getCnAreaTree();
  },
  methods: {
    getCnAreaTree() {
      cnAreaTree().then((res) => {
        this.areaTrea = res.data?.children;
      });
    },
    handleAdd(row) {
      if (row) {
        businessSelfPickupAddressInfo(row.id).then((res) => {
          if (res.code == 200) {
            this.$refs.addEditRef.open(res.data);
          }
        });
      } else {
        this.$refs.addEditRef.open();
      }
    },
    handleDelete(row) {
      this.$confirm(
        '是否确认删除自提点名称为"' + row.pointName + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return businessSelfPickupAddressDelete(row.id);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function () { });
    },
    handleSetDefault(row) {
      this.$confirm(
        '是否确认将自提点名称为"' + row.pointName + '"设为默认?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return businessSetDefaultStatus(row.id);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("设为默认成功");
        })
        .catch(function () { });
    },
  },
};
</script>
<style scoped lang="scss"></style>
