<template>
  <d2-container>
    <div class="providerOrderList-container">
      <div class="filter-box">
        <div class="filter-item">
          <div class="spana">客户名称：</div>
          <el-input v-model="conditionCollection.name"
                    placeholder="请输入"></el-input>
        </div>
        <div class="filter-item">
          <div class="spana">客户性质：</div>
          <el-select v-model="conditionCollection.nature"
                     placeholder="全部">
            <el-option v-for="item in customerNatureList"
                       :key="item.value"
                       :label="item.label"
                       :value="item.value">
            </el-option>
          </el-select>
        </div>
        <div class="filter-item">
          <div class="spana">客户类型：</div>
          <el-select v-model="conditionCollection.type"
                     placeholder="全部">
            <el-option v-for="item in customerTypeList"
                       :key="item.value"
                       :label="item.label"
                       :value="item.value">
            </el-option>
          </el-select>
        </div>
        <div class="filter-item">
          <div class="spana">账户状态：</div>
          <el-select v-model="conditionCollection.state"
                     placeholder="全部">
            <el-option v-for="item in statusList"
                       :key="item.value"
                       :label="item.label"
                       :value="item.value">
            </el-option>
          </el-select>
        </div>
        <!-- 城市 -->
        <el-form :model="conditionCollection">
          <el-form-item label="所在城市：">
            <el-row :gutter="10"
                    style="width: 500px">
              <el-col :span="9">
                <el-form-item label="">
                  <el-select v-model="conditionCollection.provinceId"
                             :loading="isLoadProvinceId"
                             loading-text="信息加载中"
                             @change="provinceIdChange"
                             placeholder="选择省"
                             style="margin-left: 35px">
                    <el-option v-for="item in provinceIdList"
                               :key="item.id"
                               :label="item.name"
                               :value="item.id">
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="7">
                <el-form-item label="">
                  <el-select v-model="conditionCollection.cityId"
                             placeholder="选择市">
                    <el-option v-for="item in cityIdList"
                               :key="item.id"
                               :label="item.name"
                               :value="item.id"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form-item>
        </el-form>
        <div class="filter-item">
          <div class="spana">负责人：</div>
          <el-input v-model="conditionCollection.contact"
                    placeholder="请输入"></el-input>
        </div>
        <div class="filter-item">
          <div class="spana">联系电话：</div>
          <el-input v-model="conditionCollection.contactPhone"
                    placeholder="请输入"></el-input>
        </div>
        <div class="filter-btn">
          <el-button type="primary"
                     size=""
                     @click="queryData()">查询</el-button>
          <el-button type=""
                     size=""
                     @click="reset()">重置</el-button>
          <el-button type="success"
                     icon="el-icon-circle-plus-outline"
                     @click="addBusiness()">新建客户</el-button>
        </div>
      </div>
      <div>
        <el-table :data="dataList"
                  size="medium"
                  style="width: 100%; text-align: center;"
                  :key="tablekey"
                  v-loading="loadings">
          <!-- <el-table-column prop="userCode" label="序号"> </el-table-column> -->
          <el-table-column prop="id"
                           label="客户编号"> </el-table-column>
          <el-table-column prop="name"
                           label="客户名称"></el-table-column>
          <el-table-column prop="natureValue"
                           label="客户性质"> </el-table-column>
          <el-table-column prop="typeValue"
                           label="客户类型"> </el-table-column>
          <el-table-column prop="contact"
                           label="联系人"></el-table-column>
          <el-table-column prop="contactPhone"
                           label="联系电话"> </el-table-column>
          <el-table-column prop="cityOfResidence"
                           label="所在城市"> </el-table-column>
          <el-table-column prop="createTime"
                           label="创建时间"> </el-table-column>
          <el-table-column prop="stateValue"
                           label="状态"> </el-table-column>
          <el-table-column fixed="right"
                           label="操作"
                           width="120">
            <template slot-scope="scope">
              <!-- 编辑 -->
              <el-button type="text"
                         style="font-size: 14px; margin-left: 0px"
                         @click="detils(scope.row.openId)">查看详情</el-button>
              <!-- <el-button type="text"
                         v-if="scope.row.status == 0"
                         style="font-size: 14px; width: 90px; margin-left: 0"
                         @click="edit(scope.row.openId)">收款设置</el-button> -->
              <el-button type="text"
                         v-if="scope.row.state == 1"
                         style="font-size: 14px; margin-left: 10px"
                         @click="enable(scope.row.openId)">启用</el-button>
              <el-button type="text"
                         v-if="scope.row.state == 0"
                         style="font-size: 14px; margin-left: 10px;color:red"
                         @click="disable(scope.row.openId)">停用</el-button>

            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pagination">
        <pagination v-show="total >= 0"
                    :total="total"
                    :page.sync="searchCriteria.pageNow"
                    :limit.sync="searchCriteria.pageSize"
                    @pagination="pagingEvent" />
      </div>
    </div>
  </d2-container>
</template>

<script>
import {
  jdAddressCascader,
  enableCustomer,
  disablesCustomer,
  getCustomerList,
  getCustomerNature,
  getCustomerType,
  getCustomerState,
} from "@/libs/common/constant";
import { selectListALL, selectList } from "@/utils/json";
export default {
  data() {
    return {
      conditionCollection: {
        name: "",
        nature: "",
        type: "",
        state: "",
        provinceId: "",
        cityId: "",
        contact: "",
        contactPhone: "",
      },
      dataList: [],
      searchCriteria: {
        pageNow: 1,
        pageSize: 10,
      },
      total: 0,
      tablekey: "",
      loadings: true,
      isLoadProvinceId: true,
      // 客户 性质
      customerNatureList: [],
      // 客户 类型
      customerTypeList: [],
      // 账户 状态
      statusList: [],
      // 一级地址
      provinceIdList: [],
      // 二级地址
      cityIdList: [],
      determineQueryData: {},
    };
  },
  async created() {
    // 获取 字典 数据
    await this.getConstant();
    // 获取 初始 数据
    await this.queryData();
  },
  methods: {
    // 查询
    async queryData() {
      this.determineQueryData = this.conditionCollection;
      var data = this.conditionCollection;
      data.currentPage = this.searchCriteria.pageNow;
      data.pageSize = this.searchCriteria.pageSize;
      await this.getListData(data);
    },
    //  重置
    reset() {
      var obj = this.conditionCollection;
      for (const key in obj) {
        obj[key] = "";
      }
    },
    // 获取 列表 参数
    async getListData(data) {
      this.loadings = true;
      const dataList = await this.getCustomerList(data);
      console.log(dataList);
      // cityOfResidence
      this.total = dataList.total;

      this.dataList = dataList.list;
      for (var i = 0; i < this.dataList.length; i++) {
        this.dataList[i].cityOfResidence =
          this.dataList[i].provinceName + " " + this.dataList[i].cityName;
      }
      this.loadings = false;
    },

    async getCustomerList(data) {
      const res = await this.$api.GET(`${getCustomerList.url}`, data);
      return res;
    },
    async pagingEvent() {
      // await this.queryData();
      var data = this.determineQueryData;
      data.currentPage = this.searchCriteria.pageNow;
      data.pageSize = this.searchCriteria.pageSize;
      await this.getListData(data);
    },

    // 添加 供应商
    addBusiness() {
      this.$router.push({
        path: "/addCustomer",
      });
    },
    // 编辑 供应商
    edit(openId) {
      this.$router.push({
        path: `/editBusiness/${openId}`,
      });
    },
    // 查看详情
    detils(openId) {
      this.$router.push({
        path: `/customerDetails/${openId}`,
      });
    },
    selectListALL(myJson) {
      return selectListALL(myJson);
    },
    selectList(myJson, a) {
      return selectList(myJson, a);
    },
    // 获取 常量
    async getConstant() {
      await this.getProvinceIdList();
      await this.getNatureList();
      await this.getCustomerTypeList();
      await this.getCustomerState();
    },
    // 获取 一级地址
    async getProvinceIdList() {
      const res = await this.$api.GET(`${jdAddressCascader.url}`);
      this.provinceIdList = res;
      this.isLoadProvinceId = false;
    },
    // 获取 客户 性质
    async getNatureList() {
      const res = await this.$api.GET(`${getCustomerNature.url}`);
      this.customerNatureList = this.selectList(res, true);
    },
    // 获取 客户类型
    async getCustomerTypeList() {
      const res = await this.$api.GET(`${getCustomerType.url}`);
      this.customerTypeList = this.selectList(res);
    },
    // 获取 客户 账户 状态
    async getCustomerState() {
      const res = await this.$api.GET(`${getCustomerState.url}`);
      this.statusList = this.selectList(res);
    },
    // 获取 二级地址
    async provinceIdChange() {
      this.cityIdList = await this.$api.GET(
        `${jdAddressCascader.url2}${this.conditionCollection.provinceId}`
      );
    },
    async enable(openId) {
      this.$confirm("确认启用此客户吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        // type: 'warning',
        customClass: "custom-confirm",
      })
        .then(() => {
          this.enableCustomer(openId);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消启用!",
          });
        });
    },
    async enableCustomer(openId) {
      const res = await this.$api.POST(`${enableCustomer.url}${openId}`);
      console.log(res);
      await this.queryData();
      this.$message({
        type: "success",
        message: "启用成功!",
      });
    },
    disable(openId) {
      this.$confirm("确认停用此客户吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        // type: 'warning',
        customClass: "custom-confirm",
      })
        .then(() => {
          this.disablesCustomer(openId);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消停用!",
          });
        });
    },
    async disablesCustomer(openId) {
      const res = await this.$api.POST(`${disablesCustomer.url}${openId}`);
      console.log(res);
      await this.queryData();
      this.$message({
        type: "success",
        message: "停用成功!",
      });
    },
  },
};
</script>

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