<template>
  <div class="app-container">
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item>
        <el-select
          v-model="pagerDto._Eq_Id"
          placeholder="客户名称"
          clearable
          filterable
        >
          <el-option
            v-for="item in customers"
            :key="item.Id"
            :label="item.CompanyName"
            :value="item.Id"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select
          v-model="pagerDto._Eq_SalesmanUserId"
          placeholder="业务员"
          clearable
          filterable
        >
          <el-option
            v-for="item in salesmanUser"
            :key="item.Id"
            :label="item.NickName"
            :value="item.Id"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select
          v-model="pagerDto._Eq_IsActive"
          placeholder="启用状态"
          clearable
          filterable
        >
          <el-option label="启用" value="True" />
          <el-option label="停用" value="False" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select
          v-model="pagerDto._Eq_Receiving"
          placeholder="收货状态"
          clearable
          filterable
        >
          <el-option label="收货" value="True" />
          <el-option label="停止收货" value="False" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          icon="el-icon-search"
          size="small"
          type="primary"
          :loading="ladding"
          @click="pagerSerach()"
          >搜索</el-button
        >
        <el-button
          icon="el-icon-edit"
          size="small"
          type="primary"
          :loading="ladding"
          @click="showDialogEdit()"
          >批量调整</el-button
        >
      </el-form-item>
    </el-form>

    <el-table :data="sData" border height="660" @selection-change="onSelection">
      <el-table-column label="操作" type="selection" />
      <el-table-column label="客户编码" prop="CustomerNumber">
        <template slot-scope="r">
          {{ r.row.CustomerNumber }}
        </template>
      </el-table-column>
      <el-table-column label="客户名称" prop="CompanyName">
        <template slot-scope="r">
          {{ r.row.CompanyName }}
        </template>
      </el-table-column>
      <el-table-column label="抛重系数" prop="WeightCoefficient" />
    </el-table>
    <el-pagination
      :page-sizes="[30, 50, 100, 200]"
      :current-page="pagerDto.PageIndex"
      :page-size="pagerDto.PageSize"
      background
      layout="prev, pager, next ,total ,jumper,sizes,slot "
      :total="pagerDto.total"
      @current-change="pagerSerach"
      @size-change="sizeChange"
    />

    <el-dialog title="修改抛重系数" :visible.sync="dialogEdit">
      <el-form>
        <el-input type="number" min=1 step=1 v-model="WeightCoefficient" onkeyup="this.value=this.value.replace(/\D|^0/g,'')" />
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogEdit = false">取 消</el-button>
        <el-button type="primary" @click="updateWeightCoefficient()">提 交</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  _queryPager,
  _getSalesmanUsers,
  _updateWeightCoefficient,
} from "@/api/customer";
import { _getCustomers } from "@/api/baseInfo";

import { mapGetters } from "vuex";
export default {
  name: 'WeightCoefficientManagement',
  computed: {
    ...mapGetters(["roles"]),
  },
  data() {
    return {
      ladding: false,
      dialogEdit: false,
      sData: [],
      customers: [],
      WeightCoefficient: 0,
      changeFees: [],
      selectIds: "",
      row: {},
      pagerDto: {
        PageIndex: 1,
        PageSize: 30,
        total: 0,
        _In_TrackingNumber: "",
        QuerySort: [{ Field: "Id", IsDesc: true }],
        _Eq_Id: "",
        _Eq_IsActive: "True",
        _Eq_SalesmanUserId: "",
      },
      salesmanUser: [],
    };
  },
  created() {
    _getCustomers().then((response) => {
      this.customers = response;
    });

    _getSalesmanUsers().then((response) => {
      this.salesmanUser = response;
      this.pagerSerach();
    });
  },
  methods: {
    showDialogEdit() {
      var ids = this.selectIds;
      if (ids == "") {
        this.$alert("请勾选需要操作的客户", "错误", { type: "warning" });
        return;
      }
      this.dialogEdit = true;
    },
    onSelection(s) {
      var ids = [];
      for (let i = 0; i < s.length; i++) {
        ids.push(s[i].Id);
      }
      this.selectIds = ids.join(",");
    },
    updateWeightCoefficient(){
        _updateWeightCoefficient(this.WeightCoefficient,this.selectIds).then((res)=>{
            this.dialogEdit=false;
            this.$alert('操作成功', '成功', { type: 'scuesss' })
            this.pagerSerach(1)
        })
    },
    sizeChange(PageSize) {
      this.pagerDto.PageSize = PageSize;
      this.pagerSerach(1);
    },
    IsCMS() {
      for (var i = 0; i < this.roles.length; i++) {
        if (this.roles[i] == "客服经理") {
          return true;
        }
      }
      return false;
    },
    IsFS() {
      for (var i = 0; i < this.roles.length; i++) {
        if (this.roles[i] == "财务") {
          return true;
        }
      }
    },
    IsAdmin() {
      for (var i = 0; i < this.roles.length; i++) {
        if (this.roles[i] == "销售主管") {
          return true;
        }
      }
      return false;
    },
    pagerSerach(index) {
      if (index) {
        this.pagerDto.PageIndex = index;
      }
      this.ladding = true;
      _queryPager(this.pagerDto)
        .then((response) => {
          this.sData = response.Result;
          this.ladding = false;
          this.pagerDto.total = response.TotalCount;
          for (var j = 0; j < this.salesmanUser.length; j++) {
            for (var i = 0; i < response.Result.length; i++) {
              if (
                response.Result[i].SalesmanUserId == this.salesmanUser[j].Id
              ) {
                response.Result[i].SalesmanUserName =
                  this.salesmanUser[j].NickName;
              }
            }
          }
        })
        .catch((response) => {
          this.ladding = false;
        });
    },
  },
};
</script>
