<template>
  <div class="page">
    <topBar
      v-if="topList.length > 0"
      :topList="topList"
      :moren="moren"
    ></topBar>
    <div class="thetopbox">
      <el-form label-width="100px">
        <el-row>
          <el-col :span="4">
            <el-form-item :label="'银行名称'" prop="status">
              <el-input clearable v-model="queryParams.bankName"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="4">
            <div class="searchbox" style="margin-left: 30px">
              <el-button
                class="my_search"
                style="margin-top: 2px"
                @click="getBankList"
                > {{ '搜索' }}</el-button
              >
              <el-button
                class="my_reset"
                style="margin-top: 2px"
                @click="reChongzhi"
                > {{ '重置' }}</el-button
              >
            </div>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <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="700px"
          :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="bankName"
            :label="'银行名称'"
            width="180"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="backgroundImg"
            :label="'背景图'"
            width="180"
          >
            <template slot-scope="scope">
              <div class="tem">
                <img :src="scope.row.backgroundImg" alt="" />
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="logo" label="logo" width="180">
            <template slot-scope="scope">
              <div class="tem">
                <img :src="scope.row.logo" alt="" />
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="enableState" :label="'启用状态'">
            <template slot-scope="scope">
              <div
                :class="scope.row.enableState == 0 ? 'greencolor' : 'redcolor'"
              >
                {{ scope.row.enableState | isQiyong(that) }}
              </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, tableData)"
                type="text"
                size="small"
                class="button-s"
                style="color: #6962f6"
              >
                {{ '修改' }}
              </el-button>
              <el-button
                class="button-s"
                @click.native.prevent="deleteRow(scope.row, tableData)"
                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="getBankList"
    />
    <el-dialog
      :title="updateTitle"
      :visible.sync="updateShow"
      width="37%"
      center
      :before-close="handleClose"
    >
      <el-form
        ref="form"
        :rules="rules"
        :model="form"
        label-width="100px"
        style="padding-bottom: 150px"
      >
        <el-row>
          <el-col :span="10">
            <el-form-item :label="'银行名称'" prop="bankName">
              <el-input
                clearable
                v-model="form.bankName"
                :placeholder="'请输入银行名称'"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="10" style="margin-left: 60px">
            <el-form-item :label="'启用状态'" prop="enableState">
              <el-select
                clearable
                v-model="form.enableState"
                :placeholder="'请选择启用状态'"
              >
                <el-option
                  v-for="item in qiyongList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="10">
            <el-form-item :label="'上传背景'" prop="backgroundImg">
              <imageUpload v-model="form.backgroundImg" />
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="logo" prop="logo">
              <imageUpload v-model="form.logo" />
            </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" type="primary" @click="onSubmit('form')"
          >{{'确认'}}</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

  <script>
import topBar from "@/components/topBar";
import {
  getBankList,
  deleteBank,
  saveBank,
  updateBank,
  getOneBankList,
} from "@/api/bank";
import { getToken } from "@/utils/auth";
import ImageUpload from "@/components/ImageUpload";
export default {
  name: "Yhpz",
  components: {
    topBar,
    ImageUpload,
  },
  filters: {
    isQiyong(val,that) {
      if (!val) {
        return that.$t('ENU_E_S_0');
      } else {
        return that.$t('ENU_E_S_1');
      }
    },
  },
  data() {
    return {
      that: this,
      moren: "yhpz",
      topList: [
        {
          name: '银行配置',
          path: "yhpz",
        },
      ],
      headers: {
        Authorization: getToken(),
      },
      showImg: true, //默认展示
      noneUploadImg: false, //
      uploadImgUrl: process.env.VUE_APP_BASE_API + "/system/upload",

      cjsj: "",
      tableData: [],
      loading:false,
      multipleSelection: [],
      qiyongList: [
        { value: 0, label: '启用' },
        { value: 1, label: '禁用' },
      ],
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        bankName: "",
      },
      form: {
        bankName: "", //银行名称
        enableState: "", //启用状态
        backgroundImg: "", //图片
        logo: "",
      },
      rules: {
        enableState: [
          { required: true, message: '请选择启用状态', trigger: "change" },
        ],
        bankName: [
          { required: true, message: '请输入银行名称', trigger: "blur" },
        ],
        backgroundImg: [
          { required: true, message: '请上传背景', trigger: "change" },
        ],
        logo: [{ required: true, message: '请上传logo', trigger: "change" }],
      },
      updateShow: false,
      addOrEdit: "", //添加还是修改 true添加 false修改
      updateTitle: "",
    };
  },
  created() {
    this.getBankList();
  },
  methods: {
    //图片上传成功后的回调
    handlePictureSuccess(res, file) {
      this.form.backgroundImg = res.data.url;
      console.log(
        "%c [ res ]-345",
        "font-size:13px; background:#490cce; color:#8d50ff;",
        res
      );
    },
    uploadImgChange(file, fileList) {
      console.log(
        "%c [ fileList ]-346",
        "font-size:13px; background:#b903fe; color:#fd47ff;",
        fileList
      );
      this.noneUploadImg = fileList.length > 0;
      console.log(
        "%c [ this.noneUploadImg ]-348",
        "font-size:13px; background:#f0bbaa; color:#ffffee;",
        this.noneUploadImg
      );
    },
    //图片上传删除
    handleRemove(file, fileList) {
      this.form.backgroundImg = "";
      this.noneUploadImg = fileList.length > 0;
    },
    //点击保存
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = "";
          if (this.addOrEdit == true) {
            url = saveBank;
          } else {
            url = updateBank;
          }
          url(this.form).then((res) => {
            if (res.code == 200) {
              this.updateShow = false;
              this.$message({
                message: res.msg,
                type: "success",
              });
              this.getBankList();
            } else {
              this.$message({
                message: res.msg,
                type: "warning",
              });
            }
          });
        }
      });
    },
    //修改
    update(row) {
      this.updateShow = true;
      this.updateTitle = '修改';
      this.addOrEdit = false;
      getOneBankList({ pkId: row.pkId }).then((res) => {
        this.form = res.data;
      });
    },
    //添加
    adddate() {
      this.updateShow = true;
      this.updateTitle = '添加';
      this.addOrEdit = true;
      if (this.$refs.form) {
        this.form.pkId = null;
        this.$refs.form.clearValidate();
        this.$refs.form.resetFields();
      }
      this.handleRemove();
    },
    handleClose() {
      this.updateShow = false;
      this.$refs["form"].clearValidate();
      this.$refs["form"].resetFields();
    },
    //重置
    reChongzhi() {
      this.queryParams.bankName = "";
    },

    //删除
    deleteRow(row) {
      this.$confirm('确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: "warning",
      }).then(() => {
        deleteBank({ pkId: row.pkId }).then((res) => {
          this.getBankList();
        });
      });
    },

    //获取银行配置列表
    getBankList() {
      this.loading = true
      getBankList(this.queryParams).then((res) => {
        this.tableData = res.rows;
        this.total = res.total;
        this.loading = false
      });
    },
    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-upload--picture-card {
  min-width: 44px !important;
  min-height: 44px !important;
  border: 1px solid #ccc;
  border-radius: 4px;
}
.button-s {
  padding: 0;
}
.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;
}
.uploadIcon ::v-deep .el-upload--picture-card {
  display: none !important; /* 上传按钮隐藏 */
}
::v-deep .el-table .warning-row {
  background: #f9f9f9;
}

::v-deep .el-table .success-row {
  background: #ffffff;
}
::v-deep .el-table thead {
  color: #000000;
}
::v-deep .el-row {
  margin-top: 20px;
}
.tem {
  display: flex;
  justify-content: center;
  img {
    width: 44px;
    height: 44px;
    border-radius: 8px;
  }
}
.redcolor {
  color: #ed1d25;
}
.greencolor {
  color: #37bf46;
}
.page {
  padding: 20px;
  background: #f9f9f9;
  font-size: 14px;
  .thetopbox {
    padding: 0 20px 0 0;
    background: #ffffff;
    border-radius: 8px;

    .searchbox {
      display: flex;
      align-items: center;
      .searchtitle {
        margin-right: 10px;
      }
      .searchbtn {
        background: #08143f;
        color: #ffffff;
      }
    }
  }
  .main {
    //margin-top: 20px;
    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 {
          background: #3181e5;
          color: #ffffff;
        }
        .thebtn2 {
          background: #ffad41;
          color: #ffffff;
        }
      }
    }
  }
}
</style>
