<template>
  <div class="page">
    <topBar
      v-if="topList.length > 0"
      :topList="topList"
      :moren="moren"
    ></topBar>
    <div class="main">
      <div class="maintop">
        <div class="mainbtn">
          <el-button size="mini" class="thebtn1" @click="adddate"
            >{{'添加'}}</el-button
          >
          <el-button size="mini" class="thebtn2"> {{ '导出' }}</el-button>
        </div>
      </div>
      <div class="maintable">
        <el-table
          height="750px"
          :data="tableData"
          v-loading="loading"
          style="width: 100%"
          :header-cell-style="{ background: '#EEEEEE' }"
          :row-class-name="tableRowClassName"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55"> </el-table-column>
          <el-table-column align="center" prop="jxmc" :label="'国家'">
            <template slot-scope="scope">
              <div class="tem">
                <img :src="scope.row.nationalFlag2" alt="" />
                <!-- <div>{{ scope.row.pkCountryName }}</div> -->
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="shopName" :label="'配送等级'">
          </el-table-column>
          <el-table-column
            align="center"
            prop="registerValue"
            :label="'注册金额'+'($)'"
          >
          <template slot-scope="scope">
            {{ scope.row.registerValue|numberToCurrency }}
          </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="registerValueLocal"
            :label="'注册金额'+'(￥)'"
          >
          <template slot-scope="scope">
            {{ scope.row.registerValueLocal|numberToCurrency }}
          </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="censusValue"
            :formatter="checkSticcal"
            :label="'统计类型'"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="settleValue"
            :label="'结算金额/业绩'"
          >
          <template slot-scope="scope">
            {{ scope.row.settleValue|numberToCurrency }}
          </template>
          </el-table-column>
          <el-table-column align="center" prop="deliveryFee" :label="'配送费' +'(%)' ">
          </el-table-column>
          <el-table-column align="center" prop="effectiveDate" :label="'生效日期'">
          </el-table-column>
          <el-table-column align="center" prop="creationTime" :label="'创建日期'">
          </el-table-column>
          <el-table-column align="center" prop="time" :label="'操作'" fixed="right" >
            <template slot-scope="scope">
              <el-button
                @click.native.prevent="update(scope.row)"
                type="text"
                size="small"
                style="color: #4391FD"
                class="button-s"
              >
                {{'修改'}}
              </el-button>
              <el-button
                @click.native.prevent="updateState(scope.row, tableData)"
                type="text"
                size="small"
                class="button-s"
                style="color: #c73030"

              >
                <!-- 0启用1禁用 -->
                {{ scope.row.enableState == 1 ? '启用' : '禁用' }}
              </el-button>
              <!-- <el-button
                class="button-s"
                @click.native.prevent="deleteRow(scope.row)"
                type="text"
                size="small"
                style="color: #c73030"
              >
                删除
              </el-button> -->
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getShopList"
    />
    <el-dialog
    :close-on-click-modal="false"
      :title="dialogTitle"
      :visible.sync="updateShow"
      width="37%"
      center
      :before-close="handleClose"
    >
      <el-form ref="form" :rules="rules" :model="form" label-width="120px" style="padding-bottom: 60px">
        <el-row style="display: flex;justify-content: space-between">
          <el-col :span="11" style="margin-left: -50px">
            <el-form-item :label="'配送等级'" prop="shopName">
              <el-input clearable
                v-model="form.shopName"
                placeholder=""
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item :label="'注册金额'+'($)'" prop="registerValue">
              <el-input clearable
                v-model="form.registerValue"
                placeholder=""
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row style="display: flex;justify-content: space-between">
          <el-col :span="11" style="margin-left: -50px">
            <el-form-item :label="'统计类型'" prop="censusValue">
              <el-select clearable
                v-model="form.censusValue"
                placeholder=""
              >
                <el-option
                  v-for="item in statisticalList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item :label="'结算金额/业绩'" prop="settleValue">
              <el-input clearable
                v-model="form.settleValue"
                placeholder=""
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11" style="margin-left:-15px">
            <el-form-item :label="'配送费' +'(%)' " prop="deliveryFee">
              <el-input clearable
                v-model="form.deliveryFee"
                placeholder=""
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="23" style="margin-left: -15px">
            <el-form-item :label="'生效日期'" prop="effectiveDate">
              <el-date-picker
                v-model="form.effectiveDate"
                type="datetime"
                placeholder=""
                value-format="yyyy-MM-dd HH:mm:ss"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button class="cancels" @click="handleClose">{{'取消'}}</el-button>
        <el-button class="submit" style="margin-left: 40px" @click="onSubmit('form')">{{'确认'}}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import topBar from "@/components/topBar";
import {
  getShopList,
  getshopOnelist,
  deleteShop,
  scShopGenerate,
  getStatistical,
  updateShop,
  updateStatusShop
} from "@/api/level";
import { userCountryList } from "@/api/user";
export default {
  name: "Pszx",
  components: {
    topBar,
  },
  data() {
    return {
      moren: "pszx",
      topList: [
        {
          name: '等级配置',
          path: "level",
        },
        {
          name: '奖衔配置',
          path: "jxpz",
        },
        {
          name: '复购极差',
          path: "fgjc",
        },
        {
          name: '配送中心',
          path: "pszx",
        },
      ],
      multipleSelection: [],
      tableData: [],
      loading:false,
      queryParams: {
        pageNum: 1,
        pageSize: 50,
      },
      total: 0,
      updateShow: false,
      form: {
        shopName: "", //配送等级
        registerValue: "", //注册金额
        censusValue: "", //统计类型
        settleValue: "", //结算金额/业绩
        deliveryFee: "", //配送费
        effectiveDate: "", //生效时间
      },
      statisticalList: [], //统计值列表
      countryList: [], //国家列表
      rules: {
        shopName: [
          { required: true, message: "请输入配送等级", trigger: "blur" },
        ],
        registerValue: [
          { required: true, message: "请输入注册金额", trigger: "blur" },
        ],

        censusValue: [
          { required: true, message: "请选择统计类型", trigger: "change" },
        ],
        settleValue: [
          { required: true, message: "请输入结算金额/业绩", trigger: "blur" },
        ],
        deliveryFee: [
          { required: true, message: "请输入配送费", trigger: "blur" },
        ],

        effectiveDate: [
          {
            type: "string",
            required: true,
            message: '请选择时间',
            trigger: "change",
          },
        ],
      },
      dialogTitle:"",
      addEdit:""
    };
  },
  created() {
    this.getShopList();
    this.getStatistical();
    this.userCountryList()
  },
  methods: {
     //启用/禁用
     updateState(row) {
      this.$confirm('确认更改状态?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: "warning",
      }).then(() => {
        var thestate = "";
        if (row.enableState == 0) {
          thestate = 1;
        } else if (row.enableState == 1) {
          thestate = 0;
        }
        updateStatusShop({pkId:row.pkId, enableState:thestate}).then((res) => {
          if (res.code == 200) {
            this.$message({
              message: res.msg,
              type: "success",
            });
            // 获取列表
            this.getShopList()
          }

        });
      });
    },
     //获取国家
     userCountryList() {
      userCountryList().then((res) => {
        this.countryList = res.data;
      });
    },
    //添加
    adddate() {
      this.dialogTitle = '添加'
      this.updateShow = true;
      this.addEdit = 1
      this.$refs["form"].clearValidate();
      this.$refs["form"].resetFields();
      // scShopGenerate({}).then((res) => {
      //   if (res.code == 200) {
      //     this.getShopList();
      //   }
      // });
    },
    //统计枚举列表
    getStatistical() {
      getStatistical().then((res) => {
        this.statisticalList = res.data;
      });
    },
    //枚举替换
    checkSticcal(row) {

      let stical = "";
      if (this.statisticalList) {
        this.statisticalList.map((element) => {
          if (row.censusValue == element.value) {
            stical = element.label;
          }
        });
      }
      return stical;
    },
    //修改
    update(row) {
      this.dialogTitle = '修改'
      this.addEdit = 2
      this.updateShow = true;
      getshopOnelist(row.pkId).then((res) => {
        this.form = res.data;
      });
    },
    handleClose() {
      this.updateShow = false;
      this.$refs["form"].clearValidate();
      this.$refs["form"].resetFields();
    },
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = ""
          if (this.addEdit == 1) {
            //添加
            url = scShopGenerate
          }else{
            //修改
            url = updateShop
          }
          url(this.form).then((res) => {
            if (res.code == 200) {
              this.updateShow = false;
              this.$message({
                message: res.msg,
                type: "success",
              });
              this.getShopList();
              this.addEdit = ""
            }
          });
        } else {
          return false;
        }
      });
    },
    //获取配送中心列表
    getShopList() {
      this.loading = true
      getShopList(this.queryParams).then((res) => {

        this.total = res.total;
        res.rows.forEach((element) => {
          this.countryList.forEach((child) => {
            if (element.pkCountry == child.pkCountry) {
              element.pkCountryName = child.name;
              element.pkCountryIcon = child.nationalFlag2;
            }
          });
        })
        this.tableData = res.rows;
        this.loading = false
      });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    //删除
    deleteRow(row) {
      this.$confirm('确定删除?').then((_) => {
        deleteShop(row.pkId).then((res) => {
          if (res.code == 200) {
            this.$message({
              message: res.msg,
              type: "success",
            });
            this.getShopList();
          }
        });
      });
    },
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2 == 1) {
        return "warning-row";
      } else if (rowIndex % 2 == 0) {
        return "success-row";
      }
      return "";
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .el-date-editor.el-input, .el-date-editor.el-input__inner{
  width: 100%;
}
.button-s{
  padding: 3px 0;
}
::v-deep .el-select{
  width: 100%;
}
.submit{
  width: 209px;
  height: 48px;
  background: #c8161d;
  border-radius: 5px;
  color: #fff;
  font-size: 18px;
  border-color: #c8161d;
  margin-left: 20px;
}
.cancels{
  width: 209px;
  height: 48px;
  background: #CCCCCC;
  border-radius: 5px;
  color: #fff;
  font-size: 18px;
  border-color: #ccc;
}
::v-deep .el-table .warning-row {
  background: #f9f9f9;
}

::v-deep .el-table .success-row {
  background: #ffffff;
}
::v-deep .el-table thead {
  color: #000000;
}
.tem {
  display: flex;
  align-items: center;
  justify-content: center;
  img {
    width: 28px;
    height: 20px;
    margin-right: 3px;
  }
}
.page {
  padding: 10px 20px;
  background: #f9f9f9;
  font-size: 14px;
  .main {
    //margin-top: 20px;
    //background: #ffffff;
    //border-radius: 8px;
    //box-shadow: 0px 2px 20px 0px rgba(238, 238, 238, 0.5);
    background: #f9f9f9;
    .maintop {
      display: flex;
      // padding: 10px 0;
      justify-content: space-between;
      align-items: center;
      .mainbtn {
        .thebtn1 {
        color: #ffffff;
        background: #3181e5;
      }
      .thebtn2 {
        color: #ffffff;
        background: #ffad41;
      }
      }
    }
  }
}
</style>
