<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 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="account">
              <el-input 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">清空</el-button>
              <el-button size="small" type="primary">搜索</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>
        <el-button type="primary" size="small">编辑</el-button>
        <el-button type="primary" size="small">删除</el-button>
      </div>
      <div class="fr">
        <el-button type="primary" size="small">导入</el-button>
        <el-button type="primary" size="small">导出</el-button>
        <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"
            type="index"
            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="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="createMan"
            min-width="160"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="创建时间"
            prop="createDate"
            min-width="160"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="更新人"
            prop="updateMan"
            min-width="160"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="更新时间"
            prop="updateDate"
            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">编辑</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="经销商工程机押金比例设置"
      :visible.sync="showDialog"
      width="30%"
      :show-close="false"
      :close-on-click-modal="false"
    >
      <el-form :model="dialogForm" label-width="100px" label-position="right">
        <el-form-item label="经销商名称">
          <el-select 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="押金比例">
          <el-input v-model="dialogForm.rate" class="inputStyle"></el-input>
        </el-form-item>
        <el-form-item label="押金定额金额">
          <el-input v-model="dialogForm.amount" class="inputStyle"></el-input>
        </el-form-item>
        <el-form-item label="开始时间">
          <el-date-picker
            v-model="dialogForm.startTime"
            type="date"
            placeholder="选择日期"
            format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd HH:mm:ss"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间">
          <el-date-picker
            v-model="dialogForm.endTime"
            type="date"
            placeholder="选择日期"
            format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd HH:mm:ss"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注">
          <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="showDialog = false">取 消</el-button>
        <el-button type="primary" @click="addDataListFn">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

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

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

        rate: 0,
        remark: "",
        startTime: "",
      },
      showDialog: false,
      dataList: [],
      dealerList: [],
    };
  },
  async created() {
    await this.getList({ pageNum: 1, pageSize: 10 });
    await this.getDealerDataList({ pageNum: 1, pageSize: 10 });
  },
  methods: {
    //删除
    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;
    },
    // 提交筛选表单
    submitScreenForm() {
      this.currentPage = 1;
      this.getList({ pageNum: 1, pageSize: 10 });
    },
    // 重置筛选表单
    resetScreenForm() {
      this.$refs.screenForm.resetFields();
      this.currentPage = 1;
      this.getList({ pageNum: 1, pageSize: 10 });
    },
    // 更改每页数量
    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({ ...this.searchForm, 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() {
      let res = this.dealerList.filter(
        (v) => v.id === this.dialogForm.customerId
      )[0];
      console.log(res);
      this.dialogForm.customerName = res.name;
      this.dialogForm.customerNumber = res.number;

      console.log(this.dialogForm);
      await getDealerDepositAdd({
        ...this.dialogForm,
        rate: Number(this.dialogForm.rate),
        amount: Number(this.dialogForm.amount),
      });
      this.$message.success("添加成功");
      await this.getList({ pageNum: 1, pageSize: 10 });
      this.showDialog = false;
    },
    addFn() {
      this.showDialog = true;
    },
  },
};
</script>

<style lang="scss" scoped>
::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>>

