<template>
  <div class="page">
    <topBar
      v-if="topList.length > 0"
      :topList="topList"
      :moren="moren"
    ></topBar>
    <div class="thetopbox">
      <el-form ref="queryParams" :model="queryParams" label-width="auto">
        <el-row>
          <el-col :span="4">
            <el-form-item :label="'货币名称'" prop="name">
              <el-input clearable v-model="queryParams.name"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item :label="'货币编号'" prop="currencyCode">
              <el-input clearable v-model="queryParams.currencyCode"></el-input>
            </el-form-item>
          </el-col>
<!--          <el-col :span="4">-->
<!--            <el-form-item :label="'国家'" prop="currencyCode">-->
<!--              <el-select clearable  placeholder=""-->
<!--                         v-model="queryParams.pkCountry">-->
<!--                <el-option v-for="item in countryList"-->
<!--                           :key="item.value"-->
<!--                           :label="item.name"-->
<!--                           :value="item.pkId"></el-option>-->
<!--              </el-select>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
         <el-col :span="4">
           <el-form-item :label="'国家'" prop="enableState">
             <el-select clearable  v-model="queryParams.pkCountry">
               <el-option
                 v-for="item in countryList"
                 :key="item.pkId"
                 :label="item.shortName"
                 :value="item.pkId"
               ></el-option>
             </el-select>
           </el-form-item>
        </el-col>
          <el-col :span="4">
            <el-form-item :label="'启用状态'" prop="enableState">
              <el-select clearable v-model="queryParams.enableState">
                <el-option
                  v-for="item in stateList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="'生效日期'" prop="sxsj" class="el-check_s">
              <el-date-picker
                v-model="sxsj"
                type="datetimerange"
                range-separator="——"
                :start-placeholder="'开始日期'"
                :end-placeholder="'结束日期'"
                value-format="yyyy-MM-dd HH:mm:ss"
                :unlink-panels="true"
              ></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="'创建日期'" prop="cjsj" class="el-check_s">
              <el-date-picker
                v-model="cjsj"
                type="datetimerange"
                range-separator="——"
                :start-placeholder="'开始日期'"
                :end-placeholder="'结束日期'"
                value-format="yyyy-MM-dd HH:mm:ss"
                :unlink-panels="true"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :span="4">
            <div class="searchbox">
              <el-button   class="my_search" @click="getexchangerateList"
              > {{ '搜索' }}</el-button
              >
              <el-button class="my_reset" @click="reChongzhi"> {{ '重置' }}</el-button>
            </div>
          </el-col>
        </el-row>
        <el-row>

        </el-row>
      </el-form>
    </div>
    <div class="main">
      <div class="maintop">
        <div class="mainbtn">
          <el-button size="mini" class="thebtn1" @click="adddate()"
                     v-has-buttons="['ExchangerateAdd']"
            >{{'添加'}}</el-button
          >
          <el-button size="mini" @click="handleExport" class="thebtn2"  v-has-buttons="['ExchangerateExport']"> {{ '导出' }}</el-button>
        </div>
      </div>
      <div class="maintable">
        <el-table
          :data="tableData"
          v-loading="loading"
          style="width: 100%"
          height="680px"
          :header-cell-style="{ background: '#EEEEEE' }"
          :row-class-name="tableRowClassName"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="45"> </el-table-column>
          <el-table-column align="center" prop="jxmc" :label="'国家'" width="180">
            <template slot-scope="scope">
              <div class="tem">
                <img :src="scope.row.countryNationalFlag" alt="" />
                <div>{{ scope.row.countryName }}</div>
              </div>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="name"
            :label="'货币名称'"
            width="180"
          >
          </el-table-column>
          <el-table-column align="center" prop="currencyCode" :label="'货币编号'">
          </el-table-column>
          <el-table-column align="center" prop="symbol" :label="'货币符号'">
          </el-table-column>
          <el-table-column align="center" prop="inExchangeRate" :label="'入汇率'">
          </el-table-column>
          <el-table-column align="center" prop="outExchangeRate" :label="'出汇率'">
          </el-table-column>

          <el-table-column
            align="center"
            prop="enableState"
            :label="'启用状态'"
            :formatter="checkQiyong"
          >
          </el-table-column>
          <el-table-column align="center" prop="effectiveDate" :label="'生效日期'" width="200">
          </el-table-column>
          <el-table-column align="center" prop="creationTime" :label="'创建日期'" width="200">
          </el-table-column>
          <el-table-column
            align="center"
            prop="time"
            :label="'操作'"
            fixed="right"
            width="240"
          >
            <template slot-scope="scope">
              <el-button
                class="button-s"
                @click.native.prevent="update(scope.row, tableData)"
                type="text"
                size="small"
                style="color:#6962F6;"
                v-has-buttons="['ExchangerateUpdate']"
              >
                {{ '修改' }}
              </el-button>
              <el-button
                v-if="scope.row.enableState==0"
                class="button-s redcolor"
                v-has-buttons="['ExchangerateDisabled']"
                @click.native.prevent="updateState(scope.row, tableData)"
                type="text"
                size="small"
              >
                {{'禁用'}}
              </el-button>
              <el-button
                v-else
                v-has-buttons="['ExchangerateEnable']"
                class="button-s"
                @click.native.prevent="updateState(scope.row, tableData)"
                type="text"
                size="small"
              >
                {{'启用'}}
              </el-button>
              <el-button
                class="button-s"
                @click.native.prevent="deleteRow(scope.row, tableData)"
                type="text"
                size="small"
                v-has-buttons="['ExchangerateDelete']"
                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="getexchangerateList"
    />
    <el-dialog
      :title="updateTitle"
      :visible.sync="updateShow"
      width="40%"
      center
      :before-close="handleClose"
    >
      <el-form ref="form" :rules="rules" :model="form" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item :label="'货币名称'" prop="name">
              <el-input
                clearable
                v-model="form.name"
                :placeholder="'请输入货币名称'"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="'货币编码'" prop="currencyCode">
              <el-input
                clearable
                v-model="form.currencyCode"
                :placeholder="'请输入货币编码'"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item :label="'货币符号'" prop="symbol">
              <el-input
                clearable
                v-model="form.symbol"
                :placeholder="'请输入货币符号'"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="'入汇率'" prop="inExchangeRate">
              <el-input
                clearable
                v-model="form.inExchangeRate"
                :placeholder="'请输入入汇率'"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item :label="'出汇率'" prop="outExchangeRate">
              <el-input
                clearable
                v-model="form.outExchangeRate"
                :placeholder="'请输入出汇率'"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="'启用状态'" prop="enableState">
              <el-select clearable v-model="form.enableState">
                <el-option
                  v-for="item in stateList"
                  :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="12">
            <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 @click="handleClose">{{'取消'}}</el-button>
        <el-button type="primary" @click="onSubmit('form')">{{'确认'}}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import topBar from "@/components/topBar";
import {
  getexchangerateList,
  getcountryAllList,
  updatecurrencyEnable,
  deleteCurrency,
  getCurrencyDetail,
  updateCurrency,
  saveCurrency,
} from "@/api/exchangerate";
import {userCountryList} from "@/api/user";
export default {
  name: "Hlpz",
  components: {
    topBar,
  },
  data() {
    return {
      moren: "hlpz",
      topList: [
        {
          name: '汇率配置',
          path: "hlpz",
        },
      ],
      sxsj: "",
      cjsj: "",
      tableData: [],
      loading:false,
      multipleSelection: [],
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        name: "", //货币名称
        currencyCode: "", //货币编码
        pkCountry: "", //国家下拉
        enableState: "", //启用状态
        createStartDate: "", //创建时间起始
        createEndDate: "", //创建时间结束
        effectiveStartDate: "", //生效时间起始
        effectiveEndDate: "", //生效时间结束
      },
      stateList: [
        //启用状态列表
        {
          value: 0,
          label: '启用',
        },
        {
          value: 1,
          label: '禁用',
        },
      ],
      countryList: [], //国家
      form: {
        name: "", //货币名称
        currencyCode: "", //货币编码
        symbol: "", //货币符号
        inExchangeRate: "", //入汇率
        outExchangeRate: "", //出汇率
        enableState: "", //启用状态
        effectiveDate: "", //生效时间
      },
      rules: {
        name: [{ required: true, message: '请输入货币名称', trigger: "blur" }],
        currencyCode: [
          { required: true, message: '请输入货币编码', trigger: "blur" },
        ],

        symbol: [
          { required: true, message: '请输入货币符号', trigger: "blur" },
        ],
        inExchangeRate: [
          { required: true, message: '请输入入汇率', trigger: "blur" },
        ],
        outExchangeRate: [
          { required: true, message: '请输入出汇率', trigger: "blur" },
        ],
        enableState: [
          { required: true, message: '请选择启用状态', trigger: "change" },
        ],
        effectiveDate: [
          { required: true, message: '请选择生效时间', trigger: "change" },
        ],
      },
      updateShow: false,
      addOrEdit: "", //添加还是修改 true添加 false修改
      updateTitle: "",
    };
  },
  created() {
    this.getexchangerateList();
    this.getcountryAllList();
    // this.userCountryList();
  },
  methods: {
    //获取国家
    userCountryList() {
      userCountryList().then((res) => {
        console.error(111)
        this.countryList = res.data;
      });
    },
    //点击保存
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = "";
          if (this.addOrEdit == true) {
            url = saveCurrency;
          } else {
            url = updateCurrency;
          }
          url(this.form).then((res) => {
            if (res.code == 200) {
              this.updateShow = false;
              this.$message({
                message: res.msg,
                type: "success",
              });
              this.$refs["form"].clearValidate();
              this.$refs["form"].resetFields();
              this.getexchangerateList();
            } else {
              this.$message({
                message: res.msg,
                type: "warning",
              });
            }
          });
        }
      });
    },
    //修改
    update(row) {
      this.updateShow = true;
      this.updateTitle = '修改';
      this.addOrEdit = false;
      getCurrencyDetail({ pkId: row.pkId }).then((res) => {
        this.form = res.data;
      });
    },
    //添加
    adddate() {
      this.updateShow = true;
      this.updateTitle = '添加';
      this.addOrEdit = true;
    },
    handleClose() {
      this.updateShow = false;
      this.$refs["form"].clearValidate();
      this.$refs["form"].resetFields();
    },
    //重置
    reChongzhi() {
      this.queryParams.name = "";
      this.queryParams.currencyCode = "";
      this.queryParams.pkCountry = "";
      this.queryParams.enableState = "";
      this.queryParams.languages = "";
      this.queryParams.createStartDate = "";
      this.queryParams.createEndDate = "";
      this.queryParams.effectiveStartDate = "";
      this.queryParams.effectiveEndDate = "";
      this.cjsj = "";
      this.sxsj = "";
    },

    //删除
    deleteRow(row) {
      this.$confirm('确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: "warning",
      }).then(() => {
        deleteCurrency(row.pkId).then((res) => {
          this.getexchangerateList();
        });
      });
    },
    //修改汇率状态禁用/启用
    updateState(row) {
      this.$confirm('确认更改状态?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: "warning",
      }).then(() => {
        var thestate = "";
        if (row.enableState == 0) {
          thestate = 1;
        } else if (row.enableState == 1) {
          thestate = 0;
        }
        updatecurrencyEnable(row.pkId, thestate).then((res) => {
          if (res.code == 200) {
            this.$message({
              message: res.msg,
              type: "success",
            });
            // 获取列表
          } else {
            this.$message({
              message: res.msg,
              type: "warning",
            });
          }
          this.getexchangerateList();
        });
      });
    },
    //获取国家下拉列表
    getcountryAllList() {
      getcountryAllList().then((res) => {
        this.countryList = res.data;
      });
    },
     /** 导出按钮操作 */
     handleExport() {
      this.$confirm( '是否确认导出所有数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then((_) => {
        this.download(
          '/system/manage/currency/export',
          Object.assign({}, this.queryParams),
          `${'汇率配置'}${new Date().getTime()}.xlsx`
        )
      })
    },
    //获取汇率配置列表
    getexchangerateList() {
      if (this.sxsj) {
        this.queryParams.effectiveStartDate = this.sxsj[0];
        this.queryParams.effectiveEndDate = this.sxsj[1];
      }
      if (this.cjsj) {
        this.queryParams.createStartDate = this.cjsj[0];
        this.queryParams.createEndDate = this.cjsj[1];
      }
      this.loading = true
      getexchangerateList(this.queryParams).then((res) => {
        this.tableData = res.rows;
        this.total = res.total;
        this.loading = false
      });
    },
    checkQiyong(row) {
      if (row.enableState == 0) {
        return '启用';
      } else {
        return '禁用';
      }
    },
    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>
.button-s{
  padding: 3px 0;
}
  ::v-deep .el-select .el-input .el-select__caret{
  color: #000;
}
::v-deep .el-check_s  .el-input__inner{
  width: 100%;
}
::v-deep .el-form-item{
  margin-bottom: 10px;
}
::v-deep .div1 .el-form-item__content{
  margin-left: 10px!important;
}
::v-deep .el-date-editor .el-range__close-icon{
  margin-top: -10px;
}
::v-deep .el-date-editor .el-range-separator{
  margin-top: -10px;
  color: #CCCCCC;
}
::v-deep .el-date-editor .el-range__icon{
  margin-top: -10px;
}
::v-deep .el-input__inner{
  //height:28px;
  //line-height: 28px;
}
::v-deep .el-form-item__label{
  color: #333;
  font-weight: inherit;
}
::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 {
}
.tem {
  display: flex;
  justify-content: center;
  img {
    width: 28px;
    height: 19px;
    margin-right: 3px;
  }
}
.page {
  padding:10px 20px;
  background: #f9f9f9;
  font-size: 14px;
  .thetopbox {
     padding: 10px 10px 10px 20px;
    background: #ffffff;
    border-radius: 8px;

    .searchbox {
      display: flex;
      align-items: center;
      margin-left: 25px;
      .searchtitle {
        margin-right: 10px;
      }
      .searchbtn {
        background: #08143f;
        color: #ffffff;
      }
    }
  }
  .main {
    .maintop {
      display: flex;
      // padding: 10px 0;
      justify-content: space-between;
      align-items: center;
      .mainbtn {
        .thebtn1 {
          background: #3181e5;
          color: #ffffff;
          border-color: #3181e5;
        }
        .thebtn2 {
          background: #ffad41;
          color: #ffffff;
          border-color: #ffad41;
        }
      }
    }
  }
}
::v-deep .el-date-editor--datetimerange{
  width: 100%;
}
::v-deep .el-col{
  padding-right: 10px;
}
</style>
