<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>
        </div>
      </div>
      <div class="maintable">
        <el-table
          height="760px"
          :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="specialAreaVal"
            :label="'所属专区'"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="freeAmount"
            :label="'免邮金额'"
            :formatter="stateFormat"
          >
          </el-table-column>

          <el-table-column
            align="center"
            prop="enableState"
            :label="'状态'"
          >
            <template slot-scope="scope">
              <div :class="scope.row.enableState == 1 ? 'isRed' : 'isGreen'">
                {{ scope.row.enableStateVal }}
              </div>
            </template>
          </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"
                class="button-s"
                style="color: #6962f6"
              >
                {{ '修改' }}
              </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="getSearch"
    />

    <el-dialog
      :title="updateTitle"
      :visible.sync="updateShow"
      width="37%"
      center
      :before-close="handleClose"
    >
      <el-form ref="form" :rules="rules" :model="form" label-width="auto">
       
        <el-row style="display: flex; justify-content: space-between">
            <el-col :span="10">
                <el-form-item :label="'所属专区'"
                              prop="specialArea">
                  <el-select clearable
                             v-model="form.specialArea"
                             @change="selSpecial">
                    <el-option v-for="item in areaList"
                               :key="item.value"
                               :label="item.label"
                               :value="item.value"></el-option>
                  </el-select>
                </el-form-item>
          </el-col>
          <el-col :span="10" style="margin-left: 50px">
            <el-form-item :label="'免邮金额'" prop="freeAmount">
              <el-input
                clearable
                v-model="form.freeAmount"
                type="number"
                placeholder=""
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row >
          <el-col :span="10">
            <el-form-item :label="'状态'" prop="enableState">
              <el-select clearable v-model="form.enableState" placeholder="">
                <el-option
                  v-for="item in agreeList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
         
        </el-row>
        
       
      </el-form>
      <div style="width: 100%; text-align: center">
        <span slot="footer" class="dialog-footer">
          <el-button class="cancels" @click="handleClose">{{
            '取消'
          }}</el-button>
          <el-button
            style="margin-left: 40px"
            class="submit"
            @click="onSubmit('form')"
            >{{ '确认' }}</el-button
          >
        </span>
      </div>
    </el-dialog>
  </div>
</template>
  
    <script>
import topBar from "@/components/topBar";
import { mapGetters } from "vuex";
import * as con from '@/api/configManage.js'
import * as api from '@/api/product.js'
import { stateFormat } from "@/utils/numberToCurrency";
export default {
  name: "FreePostageAreas",
  components: {
    topBar,
  },
  computed: {
    ...mapGetters(["getUser"]),
  },
  data() {
    return {
      that: this,
      moren: "freePostageAreas",
      topList: [
        {
          name: '免邮专区配置',
          path: "freePostageAreas",
        },
      ],
      loading:false,
      tableData: [],
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 50,
      },
      updateShow: false,
      addOrEdit: "", //添加还是修改 true添加 false修改
      updateTitle: "",
      agreeList: [
        {
          label: '允许',
          value: 0,
        },
        {
          label: '禁止',
          value: 1,
        },
      ],
      form: {
        specialArea: "", 
        freeAmount: "", 
        enableState: "",
      },
      areaList:[],
      rules: {
        freeAmount: [
          { required: true, message: "请输入", trigger: "blur" },
        ],
        specialArea: [
          { required: true, message: '请选择', trigger: 'change' },
        ],
        enableState: [
          { required: true, message: '请选择', trigger: "change" },
        ],     
      },
    };
  },
  mounted() {
    this.getSearch();
    this.getArea()
  },
  methods: {
    stateFormat,
     // 获取专区
     getArea() {
      con.specialArea().then((res) => {
        this.areaList = res.data
      })
    },
    selSpecial(e) {
      this.get_hierarchy_list(e)
      if (e == 3 || e == 26 || e == 12 || e == 22 || e == 14) {
        this.$set(this, 'topList', [
          {
            name: '基础设置',
            id: 0,
          },
          {
            name: '产品明细',
            id: 1,
          },
          {
            name: '规格明细',
            id: 2,
          },
          {
            name: '团队配置',
            id: 3,
          },
          {
            name: '级差配置',
            id: 4,
          },
          {
            name: '等级配置',
            id: 5,
          },
          {
            name: '奖衔配置',
            id: 6,
          },
          {
          name: '商品权限',
          id: 7,
        },
        ])
      } else {
        this.$set(this, 'topList', [
          {
            name: '基础设置',
            id: 0,
          },
          {
            name: '产品明细',
            id: 1,
          },
          {
            name: '规格明细',
            id: 2,
          },
          {
            name: '团队配置',
            id: 3,
          },

          {
            name: '等级配置',
            id: 5,
          },
          {
            name: '奖衔配置',
            id: 6,
          },
          {
          name: '商品权限',
          id: 7,
        },
        ])
      }
    },
    // 查询分类父子级关系
    get_hierarchy_list(val) {
      api
        .get_hierarchy_list({
          specialArea: val || null,
        })
        .then((res) => {
          this.hierarchyList = res.data
        })
    },
    //点击保存
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = "";
          if (this.addOrEdit == true) {
            url = con.saveAresFreePostage;
          } else {
            url = con.updateAresFreePostage;
          }
          url(this.form).then((res) => {
            if (res.code == 200) {
              this.updateShow = false;
              this.$message({
                message: res.msg,
                type: "success",
              });
              this.getSearch();
            } else {
              this.$message({
                message: res.msg,
                type: "warning",
              });
            }
          });
        }
      });
    },
    getSearch() {
      this.loading = true
      con.getAresFreePostage(this.queryParams)
        .then((res) => {
          
          this.tableData = res.rows;
          this.total = res.total;
          this.loading = false
        })
    },
    //添加
    adddate() {
      this.updateShow = true;
      this.updateTitle = '添加';
      this.addOrEdit = true;
    },
   
    //修改
    update(row) {
      this.updateTitle = '修改';
      this.addOrEdit = false;
      this.updateShow = true;
      con.getAresFreePostageRow(row.pkId).then((res) => {
        this.form = res.data;
      });
    },
    //删除
    deleteRow(row) {
      this.$confirm('确定删除?').then((_) => {
        con.deleteAresFreePostage(row.pkId).then((res) => {
          if (res.code == 200) {
            this.$message({
              message: res.msg,
              type: "success",
            });
            this.getSearch();
          }
        });
      });
    },
    handleClose() {
      this.updateShow = false;
      this.$refs["form"].clearValidate();
      this.$refs["form"].resetFields();
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    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-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-date-editor.el-input,
.el-date-editor.el-input__inner {
  width: 100%;
}
::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;
  }
}
.button-s {
  padding: 0 3px;
}
.page {
  padding: 20px;
  background: #f9f9f9;
  font-size: 14px;
  .main {
    background: #f9f9f9;
    //border-radius: 8px;
    //box-shadow: 0px 2px 20px 0px rgba(238, 238, 238, 0.5);
    .maintop {
      display: flex;
      // padding: 10px 0;
      justify-content: space-between;
      align-items: center;
      .mainbtn {
        .thebtn1 {
          color: #ffffff;
          background: #3181e5;
        }
        .thebtn2 {
          color: #ffffff;
          background: #ffad41;
        }
      }
      .maintitle {
        font-size: 10px;
        font-family: MicrosoftYaHei;
        color: #999999;
      }
    }
  }
  .isRed {
    color: #ed1d25;
  }
  .isGreen {
    color: #1ab62b;
  }
}
</style>
  