<template>
  <div class="page">
    <topBar v-if="topList.length > 0" :top-list="topList" :moren="moren" />
    <div class="thetopbox">
      <el-form>
        <el-row>
          <el-col :span="4">
            <el-form-item :label="'业务名称'" prop="type">
              <el-autocomplete
                class="inline-input"
                v-model="queryParams.typeName"
                :fetch-suggestions="querySearch"
                placeholder="请输入内容或选择"
                @select="handleSelect"
              >
                <template slot-scope="{ item }">
                  <div class="name">{{ item.label }}</div>
                </template>
              </el-autocomplete>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-button class="my_search" @click="getHandelWorkList()">
              {{ '搜索' }}</el-button
            >
            <el-button class="my_reset" @click="reChongzhi">
              {{ '重置' }}</el-button
            >
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="main">
      <div class="maintop">
        <div class="mainbtn">
          <el-button class="thebtn1" @click="adddate()">{{
            '添加'
          }}</el-button>
          <el-button   @click="handleExport" class="thebtn2"> {{ '导出' }}</el-button>
        </div>
      </div>
      <div class="maintable">
        <el-table
          ref="table1"
          height="700px"
          v-loading="loading"
          style="width: 100%"
          :header-cell-style="{ background: '#EEEEEE' }"
          :row-class-name="tableRowClassName"
          :data="tableData"
          row-key="pkId"
          lazy
          :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
        >
          >
          <el-table-column type="selection" width="55" />

          <el-table-column
            align="center"
            prop="typeName"
            :label="'业务名称'"
          />
          <el-table-column
            align="center"
            prop="accountName"
            :label="'扣款币种'"
          />
          <el-table-column
            align="center"
            prop="workCost"
            :label="'办理次数'"
          >
            <template slot-scope="scope">
              <div
                v-if="scope.row.serviceChargeDetailsList.length > 0"
                style="
                  display: flex;
                  justify-content: center;
                  margin-right: 10px;
                  position: relative;
                  width: fit-content;
                  margin: 0 auto;
                "
              >
                <div
                  style="
                    position: absolute;
                    width: 100%;
                    height: 1px;
                    background: rgba(204, 204, 204, 0.5);
                    top: 21px;
                  "
                />
                <div>
                  <div>{{ '次数' }}</div>
                  <div>{{ '费用' }}</div>
                </div>
                <div
                  v-for="(item, index) in scope.row.serviceChargeDetailsList"
                  :key="index"
                  style="margin-right: 10px"
                >
                  <div>{{ item.quantity }}</div>
                  <div>{{ item.cost }}</div>
                </div>
              </div>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="creationTime"
            :label="'创建日期'"
          />
          <el-table-column
            align="center"
            prop="time"
            :label="'操作'"
            fixed="right"
          >
            <template slot-scope="scope">
              <el-button
                class="button-s"
                style="color: #6962f6"
                type="text"
                size="small"
                @click.native.prevent="update(scope.row)"
              >
                {{ '修改' }}
              </el-button>
              <el-button
                class="button-s"
                type="text"
                size="small"
                style="color: #c73030"
                @click.native.prevent="deleteRow(scope.row)"
              >
                {{ '删除' }}
              </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="getHandelWorkList"
    />
    <el-dialog
      :title="updateTitle"
      :visible.sync="updateShow"
      width="40%"
      center
      :before-close="handleClose"
    >
      <el-form ref="form" :rules="rules" :model="form" label-width="140px">
        <el-row>
          <el-col :span="11">
            <el-form-item :label="'业务名称'" prop="type">
              <el-select
                v-model="form.type"
                clearable
                :placeholder="'请选择'"
              >
                <el-option
                  v-for="item in approbalBusinessList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item :label="'扣款币种'" prop="pkAccount">
              <el-select
                v-model="form.pkAccount"
                clearable
                :placeholder="'请选择'"
              >
                <el-option
                  v-for="item in pubEnumsAccountList"
                  :key="item.pkId"
                  :label="item.accountName"
                  :value="item.pkId"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- <el-row>
          <el-col>
            <el-form-item label="办理次数" prop="handleCount">
              <el-input
                clearable
                v-model="form.handleCount"
                placeholder="请输入办理次数"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row> -->
        <el-row style="margin-top: 0">
          <el-col
            v-for="(item, i) in form.serviceChargeDetailsList"
            :key="i"
            :span="11"
          >
            <el-form-item :label="`第${i + 1}次手续费(￥)`" class="flex-s">
              <el-input
                v-model="item.cost"
                clearable
                :placeholder="'请输入'"
                @change="changeQuantity(item, i)"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-col :span="8" style="margin-left: 20px">
          <el-button
            class="button-style"
            circle
            icon="el-icon-plus"
            @click="addList(i)"
        /></el-col>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button class="cancels" @click="handleClose">{{
          '取消'
        }}</el-button>
        <el-button class="submit" @click="onSubmit('form')">{{
          '确认'
        }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import topBar from "@/components/topBar";
import {
  getApprobalBusiness,
  getPubEnumsAccount,
  getHandelWorkList,
  deletHandelWork,
  getOneHandelWorkList,
  saveHandleWork,
  updateHandleWork,
} from "@/api/businesshandling";
export default {
  name: "Ywblsxf",
  components: {
    topBar,
  },
  data() {
    return {
      moren: "ywblsxf",
      topList: [
        {
          name: '业务办理手续费',
          path: "ywblsxf",
        },
      ],
      tableData: [],
      loading:false,
      multipleSelection: [],
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        type: "", // 业务名称
      },
      updateShow: false,
      addOrEdit: "", // 添加还是修改 true添加 false修改
      updateTitle: "",
      form: {
        type: "", // 业务名称
        // handleCount: "", //办理次数
        pkAccount: "", // 币种
        serviceChargeDetailsList: [
          {
            quantity: "",
            cost: "",
          },
        ],
      },
      rules: {
        type: [
          {
            required: true,
            message: '请选择业务',
            trigger: "change",
          },
        ],
        pkAccount: [
          {
            required: true,
            message: '请选择币种',
            trigger: "change",
          },
        ],
      },
      approbalBusinessList: [],
      pubEnumsAccountList: [],
    };
  },
  created() {
    this.getHandelWorkList();
    this.getApprobalBusiness();
    this.getPubEnumsAccount();
  },
  methods: {
    //带输入建议的产品名称
    querySearch(queryString, cb) {
      var restaurants = this.approbalBusinessList;
      var results = queryString
        ? restaurants.filter(this.createFilter(queryString))
        : restaurants;
      // 调用 callback 返回建议列表的数据

      cb(results);
    },
    createFilter(queryString) {
      return (restaurant) => {
        return (
          restaurant.label.toLowerCase().indexOf(queryString.toLowerCase()) ===
          0
        );
      };
    },
    handleSelect(item) {
      this.queryParams.type = item.value;
      this.queryParams.typeName = item.label
    },
    changeQuantity(item, i) {
      item.quantity = i + 1;
    },
    addList(i) {
      this.form.serviceChargeDetailsList.push({
        quantity: "",
        cost: "",
      });
    },
    getApprobalBusiness() {
      // 业务名称下拉选
      getApprobalBusiness().then((res) => {
        this.approbalBusinessList = res.data;
      });
    },
    getPubEnumsAccount() {
      // 币种下拉选
      getPubEnumsAccount().then((res) => {
        this.pubEnumsAccountList = res.rows;
      });
    },
    // 点击保存
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = "";
          if (this.addOrEdit == true) {
            url = saveHandleWork;
          } else {
            url = updateHandleWork;
          }
          url(this.form).then((res) => {
            if (res.code == 200) {
              this.updateShow = false;
              this.$message({
                message: res.msg,
                type: "success",
              });
              this.getHandelWorkList();
            }
          });
        }
      });
    },
    // 重置
    reChongzhi() {
      this.queryParams.type = "";
      this.queryParams.typeName = ""
      this.getHandelWorkList();
    },
    // 添加
    adddate() {
      this.updateShow = true;
      this.updateTitle = '添加';
      this.addOrEdit = true;
      this.form.pkId = null;
      this.form = {
        type: "", // 业务名称
        // handleCount: "", //办理次数
        pkAccount: "", // 币种
        serviceChargeDetailsList: [
          {
            quantity: "",
            cost: "",
          },
        ],
      };
    },
    // 修改
    update(row) {
      this.updateTitle = '修改';
      this.updateShow = true;
      this.addOrEdit = false;
      getOneHandelWorkList(row.pkId).then((res) => {
        this.form = res.data;
      });
    },
    // 删除
    deleteRow(row) {
      this.$confirm('确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: "warning",
      }).then(() => {
        deletHandelWork(row.pkId).then((res) => {
          if (res.code == 200) {
            this.$message({
              message: res.msg,
              type: "success",
            });
            this.getHandelWorkList();
          }
        });
      });
    },
    // 获取业务办理手续费列表
    getHandelWorkList() {
      this.loading = true
      getHandelWorkList(this.queryParams).then((res) => {
        this.tableData = res.rows;
        this.total = res.total;
        this.loading = false
      });
    },
      /** 导出按钮操作 */
      handleExport() {
      this.$confirm( '是否确认导出所有数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then((_) => {
        this.download(
          'system/manage/service-charge/export',
          Object.assign({}, this.queryParams, this.select),
          `业务办理手续费${new Date().getTime()}.xlsx`
        )
      })
    },
    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>
.button-style {
  position: absolute;
  left: 50%;
  bottom: 80px;
  margin-left: -20px;
}
.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;
}
.button-s {
  padding: 0 2px !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;
}
.page {
  padding: 20px;
  background: #f9f9f9;
  font-size: 14px;
  .thetopbox {
    background: #ffffff;
    border-radius: 8px;

    .searchbox {
      margin-top: 0;
      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;
          border-color: #3181e5;
        }
        .thebtn2 {
          background: #ffad41;
          color: #ffffff;
          border-color: #ffad41;
        }
      }
    }
    .maintable {
      // max-height: 550px;
      overflow: auto;
    }
  }
}
</style>
