<template>
  <div class="app-container">
    <!-- <el-card class="box-card">
      <el-row>
        <h4>通用经销商工程机押金比例配置</h4>
        <h4>
          工程机押金比例配置
          <input
            type="text"
            class="inp"
          />%通用工程押金比例配置适用于,没有单独配置工程机押金比例的经销商.
        </h4>
      </el-row>
    </el-card> -->
    <!-- 筛选条件 -->
    <div>
      <el-form
        ref="searchForm"
        :model="searchForm"
        label-width="100px"
        size="small"
        label-position="left"
      >
        <el-row :gutter="20">
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="经销商名称" prop="customerName">
              <el-input
                v-model="searchForm.customerName"
                placeholder="请输入经销商名称"
              ></el-input>
            </el-form-item>
          </el-col>

          <el-col :xs="24" :sm="12" :lg="18">
            <el-form-item label="" class="fr">
              <el-button size="small" @click="clearFn">清空</el-button>
              <el-button size="small" type="primary" @click="searchFn"
                >搜索</el-button
              >
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!-- 按钮 -->
    <div class="btn-group clearfix">
      <div class="fl">
        <el-button type="primary" size="small" @click="addFn">新增</el-button>
      </div>
      <div class="fr">
        <el-button type="primary" size="small">打印</el-button>
      </div>
    </div>

    <div class="mymain-container">
      <!-- 列表 -->
      <div class="table">
        <el-table
          v-loading="listLoading"
          :data="dataList"
          element-loading-text="Loading"
          border
          fit
          highlight-current-row
          stripe
        >
          <el-table-column
            align="center"
            label="经销商编码"
            prop="customerNumber"
            min-width="160"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="经销商名称"
            prop="customerName"
            min-width="160"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="押金比例"
            prop="rate"
            min-width="160"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="备注"
            prop="remark"
            min-width="160"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="创建人"
            prop="createBy"
            min-width="160"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="创建时间"
            prop="createTime"
            min-width="160"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="更新人"
            prop="updateBy"
            min-width="160"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="更新时间"
            prop="updateTime"
            min-width="160"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="操作"
            min-width="160"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <el-button
                type="text"
                class="textColor"
                @click="editFn(scope.row.id)"
                >编辑</el-button
              >
              <el-popconfirm
                @onConfirm="deleFn(scope.row.id)"
                title="这是一段内容确定删除吗？"
              >
                <el-button type="text" class="textColor" slot="reference"
                  >删除</el-button
                >
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 分页 -->
      <div class="fr">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :total="listTotal"
        >
        </el-pagination>
      </div>
    </div>

    <!-- 弹窗 -->
    <el-dialog
      :title="title"
      :visible.sync="showDialog"
      width="30%"
      :show-close="false"
      :close-on-click-modal="false"
    >
      <el-form
        ref="dialogForm"
        :model="dialogForm"
        :rules="rules"
        label-width="110px"
        label-position="right"
      >
        <el-form-item label="经销商名称" prop="customerId">
          <el-select
            class="inputStyle"
            v-model="dialogForm.customerId"
            placeholder="请选择"
          >
            <el-option
              v-for="item in dealerList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="押金比例" prop="rate">
          <el-input
            placeholder="请输入押金比例"
            @focus="rateFn"
            v-model.number="dialogForm.rate"
            class="inputStyle"
          >
            <i class="el-input__icon" slot="suffix">% </i>
          </el-input>
        </el-form-item>
        <el-form-item label="押金定额金额" prop="amount">
          <el-input
            placeholder="请输入押金定额"
            @focus="amountFn"
            v-model.number="dialogForm.amount"
            class="inputStyle"
          ></el-input>
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker
            class="inputStyle"
            v-model="dialogForm.startTime"
            type="datetime"
            placeholder="选择日期时间"
            default-time="00:00:00"
            value-format="yyyy-MM-dd HH:mm:ss"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker
            class="inputStyle"
            v-model="dialogForm.endTime"
            type="datetime"
            placeholder="选择日期时间"
            default-time="23:59:59"
            value-format="yyyy-MM-dd HH:mm:ss"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            type="textarea"
            v-model="dialogForm.remark"
            placeholder="请输入备注信息"
            class="inputStyle"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelFn">取 消</el-button>
        <el-button type="primary" @click="addDataListFn">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getDealerDepositList,
  getDealerDepositAdd,
  getDealerList,
  deleDealerDeposit,
  editDealerDeposit,
  infoDealerDeposit,
} from "@/api/basic_data/dealer";
import { number } from "echarts";

export default {
  data() {
    return {
      title: "",
      currentPage: 1, // 当前页码
      pageSize: 10, // 每页数量
      listTotal: 0, // 列表
      listLoading: false, // 列表加载loading
      searchForm: {
        customerName: "",
      },
      dialogForm: {
        amount: "",
        customerId: "",
        customerName: "",
        customerNumber: "",
        endTime: "",

        rate: "",
        remark: "",
        startTime: "",
      },
      rules: {
        rate: [
          {
            pattern: /^(?:[1-9]?\d|100)$/,
            message: "请输入0-100的数字",
            trigger: "blur",
            type: "number",
            required: true,
          },
          // {
          //   validator: rate,
          //   trigger: "blur",
          // },
        ],
        amount: [
          {
            trigger: "blur",
            message: "请输入定额押金",
            type: "number",
            required: true,
          },
          // {
          //   validator: amount,
          //   trigger: "blur",
          // },
        ],
        customerId: [
          {
            message: "请选择经销商名称",
            trigger: "blur",
            required: true,
          },
        ],
        startTime: [
          {
            message: "请选择开始时间",
            trigger: "blur",
            required: true,
          },
        ],
        endTime: [
          {
            message: "请选择结束时间",
            trigger: "blur",
            required: true,
          },
        ],
      },
      showDialog: false,
      dataList: [],
      dealerList: [],
    };
  },
  async created() {
    await this.getList({ pageNum: 1, pageSize: 10 });
    await this.getDealerDataList({ pageNum: 1, pageSize: 10 });
  },

  methods: {
    //押金
    amountFn(e) {
      if (this.dialogForm.rate != 0 || this.dialogForm.rate != "") {
        this.$message.warning("已选押金比例,如需选择定额押金,请把押金比例归零");
      }
    },
    rateFn(e) {
      if (this.dialogForm.amount != 0 || this.dialogForm.amount != "") {
        this.$message.warning("已选定额押金,如需选择押金比例,请把定额押金归零");
      }
    },
    //取消
    async cancelFn() {
      await this.$refs.dialogForm.resetFields();
      this.showDialog = false;
    },
    //编辑
    async editFn(id) {
      console.log(id);
      this.title = "编辑经销商工程机配置";
      const res = await infoDealerDeposit({ id });
      this.dialogForm = res.data;
      this.dialogForm.rate = parseInt(res.data.rate * 100);
      this.showDialog = true;
    },
    //删除
    async deleFn(id) {
      await deleDealerDeposit({ id });
      this.$message.success("删除成功");
      this.getList({ pageNum: 1, pageSize: 10 });
    },
    //获取经销商数据
    async getDealerDataList(data) {
      const res = await getDealerList(data);
      this.dealerList = res.data.records;
    },

    // 更改每页数量
    handleSizeChange(val) {
      this.pageSize = val;
      this.currentPage = 1;
      this.getList({ pageNum: 1, pageSize: this.pageSize });
    },
    // 更改当前页
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getList({ pageNum: val, pageSize: 10 });
    },
    //搜索功能
    async searchFn() {
      console.log(this.searchForm);
      await this.getList({
        keyword: this.searchForm.customerName,
        pageNum: 1,
        pageSize: 10,
      });
    },
    //重置
    clearFn() {
      console.log(this.$refs.searchForm);
      this.$refs.searchForm.resetFields();
    },
    //获取列表数据
    async getList(data) {
      const res = await getDealerDepositList(data);
      console.log(res);
      this.dataList = res.data.records;
      this.listTotal = res.data.total;
    },
    async addDataListFn() {
      await this.$refs.dialogForm.validate();

      // if (this.dialogForm.rate !== 0) {
      //   this.dialogForm.amount = 0;
      // }
      // if (this.dialogForm.amount !== 0) {
      //   this.dialogForm.rate = 0;
      // }

      let res = this.dealerList.filter(
        (v) => v.id === this.dialogForm.customerId
      )[0];

      this.dialogForm.customerName = res.name;
      this.dialogForm.customerNumber = res.number;

      if (this.dialogForm.id) {
        await editDealerDeposit({
          ...this.dialogForm,
          rate: this.dialogForm.rate / 100,
        });
        this.$message.success("编辑成功");
      } else {
        await getDealerDepositAdd({
          ...this.dialogForm,
          rate: Number(this.dialogForm.rate / 100),
          amount: Number(this.dialogForm.amount),
        });
        this.$message.success("添加成功");
      }
      this.dialogForm = {
        amount: 0,
        customerId: "",
        customerName: "",
        customerNumber: "",
        endTime: "",

        rate: 0,
        remark: "",
        startTime: "",
      };

      this.getList({ pageNum: 1, pageSize: 10 });
      this.showDialog = false;
    },
    addFn() {
      this.title = "新增经销商工程机配置";
      this.showDialog = true;
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .el-textarea__inner {
  resize: none;
}
::v-deep .el-form {
  .inputStyle {
    width: 80%;
  }
}
::v-deep .dialog-footer {
  display: flex;
  justify-content: center;
}
::v-deep .el-dialog__header {
  background-color: #dddddd;
}
.inp {
  margin: 0 12px;
}

.right {
  margin-top: 12px;
  float: right;
}

.table {
  margin-top: 12px;
}
.search {
  display: flex;
  margin-top: 12px;
  ::v-deep .el-input {
    width: 50%;
    margin-right: 12px;
  }
}
// .main {
//   padding: 12px;
// }
</style>>

