<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="supplierName">
        <el-input v-model="search.supplierName" placeholder="请输入供应商名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="联系人姓名" prop="contactName">
        <el-input v-model="search.contactName" placeholder="请输入联系人姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="	联系人电话" prop="contactPhone">
        <el-input v-model="search.contactPhone" placeholder="请输入联系人电话" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="所在城市" prop="provinces">
        <el-cascader ref="refSubCat" v-model="search.provinces" placeholder="请选择所在城市" :options="areaTrea"
          :props="{ expandTrigger: 'hover' }" @change="handleChange" style="width: 100%"></el-cascader>
      </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" :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="supplierName" show-overflow-tooltip />
      <el-table-column label="联系人姓名" align="center" prop="contactName" show-overflow-tooltip />
      <el-table-column label="联系人电话" align="center" prop="contactPhone" />
      <el-table-column label="所在城市" align="center" prop="location" show-overflow-tooltip>
        <template slot-scope="scope">
          <div>
            {{ scope.row.provinceName + scope.row.cityName + scope.row.districtName }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="详细地址" align="center" prop="addressDetail" show-overflow-tooltip />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button :size="ELTableSize" type="text" icon="el-icon-edit" @click="handleAdd(scope.row)">修改</el-button>
          <el-button :size="ELTableSize" type="text" icon="el-icon-delete"
            @click="handleDelete(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" />

    <add-edit ref="addEditRef" :areaTrea="areaTrea" @queryTable="getList" />
  </div>
</template>

<script>
import getListApi from "@/mixins/getListApi";
import { supplierPage, supplierDel, supplierInfo } from "@/api/supplier";
import { cnAreaTree } from "@/api/shop.js";
import addEdit from "./addEdit.vue";
export default {
  mixins: [getListApi],

  components: { addEdit },
  data() {
    return {
      areaTrea: [],
      listURL: supplierPage,
    };
  },
  created() {
    this.getCnAreaTree();
  },
  methods: {
    getCnAreaTree() {
      cnAreaTree().then((res) => {
        if (res.code == 200 && res.data) {
          this.areaTrea = res.data?.children || [];
        }
      });
    },
    // 省市区选择
    handleChange(value) {
      if (value) {
        this.search.provinces = value;
        this.search.provinceCode = value[0];
        this.search.provinceName =
          this.$refs["refSubCat"].getCheckedNodes()[0].pathLabels[0];
        this.search.cityCode = value[1];
        this.search.cityName =
          this.$refs["refSubCat"].getCheckedNodes()[0].pathLabels[1];
        this.search.districtCode = value[2];
        this.search.districtName =
          this.$refs["refSubCat"].getCheckedNodes()[0].pathLabels[2];
      } else {
        this.search.provinces = null;
      }
    },
    /** 新增修改 **/
    handleAdd(row = null) {
      if (row) {
        supplierInfo(row.supplierId).then((res) => {
          console.log(res);
          if (res.code == 200) {
            this.$refs.addEditRef.open(res.data);
          }
        });
      } else {
        this.$refs.addEditRef.open();
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.supplierId || this.ids;
      this.$modal
        .confirm('是否确认删除供应商名称为"' + row.supplierName + '"的数据项？')
        .then(() => {
          this.loading = true;
          return supplierDel(ids);
        })
        .then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .finally(() => {
          this.loading = false;
        });
    },
  },
};
</script>

<style lang="scss" scoped></style>
