<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"
            >{{$t('w_0328')}}</el-button
          >
          <el-button size="mini" class="thebtn2"> {{ $t('MN_T_8') }}</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="$t('S_C_83')">
            <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="$t('MN_F_T_1240')">
          </el-table-column>
          <el-table-column
            align="center"
            prop="registerValue"
            :label="$t('N_I_169')+'($)'"
          >
          <template slot-scope="scope">
            {{ scope.row.registerValue|numberToCurrency }}
          </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="registerValueLocal"
            :label="$t('N_I_169')+'(￥)'"
          >
          <template slot-scope="scope">
            {{ scope.row.registerValueLocal|numberToCurrency }}
          </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="censusValue"
            :formatter="checkSticcal"
            :label="$t('MN_F_T_1230')"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="settleValue"
            :label="$t('MN_F_T_1241')"
          >
          <template slot-scope="scope">
            {{ scope.row.settleValue|numberToCurrency }}
          </template>
          </el-table-column>
          <el-table-column align="center" prop="deliveryFee" :label="$t('MN_F_T_1242') +'(%)' ">
          </el-table-column>
          <el-table-column align="center" prop="effectiveDate" :label="$t('w_0445')">
          </el-table-column>
          <el-table-column align="center" prop="creationTime" :label="$t('PER_DA_9')">
          </el-table-column>
          <el-table-column align="center" prop="time" :label="$t('MY_ORD_14')" fixed="right" >
            <template slot-scope="scope">
              <el-button
                @click.native.prevent="update(scope.row)"
                type="text"
                size="small"
                style="color: #4391FD"
                class="button-s"
              >
                {{$t('PER_DA_20')}}
              </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 ? $t('MN_F_T_372') : $t('MN_F_T_373') }}
              </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="$t('MN_F_T_1240')" 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="$t('N_I_169')+'($)'" 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="$t('MN_F_T_1230')" 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="$t('MN_F_T_1241')" 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="$t('MN_F_T_1242') +'(%)' " 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="$t('w_0445')" 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">{{$t('ENU_P_TYPE0')}}</el-button>
        <el-button class="submit" style="margin-left: 40px" @click="onSubmit('form')">{{$t('ENU_P_TYPE4')}}</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: this.$t('MN_F_T_1166'),
          path: "level",
        },
        {
          name: this.$t('MN_F_T_109'),
          path: "jxpz",
        },
        {
          name: this.$t('MN_F_T_110'),
          path: "fgjc",
        },
        {
          name: this.$t('MN_F_T_111'),
          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: this.$t('MN_F_T_691'),
            trigger: "change",
          },
        ],
      },
      dialogTitle:"",
      addEdit:""
    };
  },
  created() {
    this.getShopList();
    this.getStatistical();
    this.userCountryList()
  },
  methods: {
     //启用/禁用
     updateState(row) {
      this.$confirm(this.$t('MN_F_T_804'), this.$t('w_0034'), {
        confirmButtonText: this.$t('w_0035'),
        cancelButtonText: this.$t('ENU_P_TYPE0'),
        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.$t('w_0328')
      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.$t('PER_DA_20')
      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(this.$t('MN_F_T_431')).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>
