<template>
  <div class="app-container">
    <!-- 筛选条件 -->
    <div>
      <el-form
        :model="searchForm"
        ref="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="">
              <el-input
                v-model="searchForm.customerName"
                placeholder="请输入经销商名称"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="车牌" prop="">
              <el-input
                v-model="searchForm.carBrand"
                placeholder="请输入车牌"
              ></el-input>
            </el-form-item>
          </el-col>

          <el-col :xs="24" :sm="12" :lg="12" class="tr">
            <el-form-item label="">
              <el-button size="small" @click="resetScreenForm">清空</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="addDataList"
          >新增</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>
      </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
            type="selection"
            align="center"
            min-width="100"
          ></el-table-column>
          <el-table-column
            align="center"
            label="经销商名称"
            prop="customerName"
            min-width="160"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="center"
            label="车辆车牌号"
            prop="carBrand"
            min-width="160"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="center"
            label="创建者"
            prop="createUser"
            min-width="160"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="center"
            label="创建时间"
            prop="createDate"
            min-width="160"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="center"
            label="更新者"
            prop="updateUser"
            min-width="160"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="center"
            label="更新时间"
            prop="updateDate"
            min-width="160"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="center"
            label="操作"
            min-width="160"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <el-button type="text" class="textColor" @click="editFn"
                >编辑</el-button
              >
              <el-popconfirm
                @onConfirm="delFn(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
          :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="dialogForm"
      width="40%"
      :show-close="false"
      :close-on-click-modal="false"
    >
      <el-form ref="form" :model="addForm" label-width="120px">
        <el-form-item label="经销商名称">
          <el-select v-model="addForm.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-row>
          <el-col :span="12">
            <el-form-item label="车牌号">
              <el-input v-model="addForm.carBrand"></el-input> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="联系电话">
              <el-input v-model="addForm.mobile"></el-input> </el-form-item
          ></el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="车型">
              <el-input v-model="addForm.carType"></el-input> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="体积">
              <el-input v-model="addForm.volume"></el-input> </el-form-item
          ></el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogForm = false">取 消</el-button>
        <el-button type="primary" @click="addDataListFn">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getCarList, addDataListApi, delCarFn } from "@/api/basic_data/taker";
import { getDealerList } from "@/api/basic_data/dealer";

export default {
  data() {
    return {
      listLoading: false, // 列表加载loading
      currentPage: 1, // 当前页码
      pageSize: 10, // 每页数量
      listTotal: 0, // 列表总数
      addForm: {
        customerId: "",
        mobile: "",
        carType: "",
        volume: "",
        carBrand: "",
      },
      dialogForm: false,
      dataList: [],
      searchForm: {
        customerName: "",
        carBrand: "",
      },
      dealerList: [],
    };
  },
  async created() {
    await this.getDataList({ pageNum: 1, pageSize: 10 });
    await this.getDealerDataList({ pageNum: 1, pageSize: 10 });
  },
  methods: {
    //搜索
    async searchFn() {
      console.log(this.searchForm);
      await this.getDataList({ ...this.searchForm, pageNum: 1, pageSize: 10 });
    },
    //删除
    async delFn(ids) {
      console.log(ids);
      await delCarFn({ ids });
    },
    //获取经销商数据
    async getDealerDataList(data) {
      const res = await getDealerList(data);
      this.dealerList = res.data.records;
    },
    //新增
    addDataList() {
      this.dialogForm = true;
    },
    async addDataListFn() {
      await addDataListApi({ ...this.addForm, orgNumber: "1122" });
      this.$message.success("添加成功");
      await this.getDataList({ pageNum: 1, pageSize: 10 });
      this.dialogForm = false;
    },
    //获取列表数据
    async getDataList(data) {
      const res = await getCarList(data);
      console.log(res);
      this.dataList = res.data.records;
      this.listTotal = res.data.total;
    },
    editFn() {},
    // 提交筛选表单
    submitScreenForm() {
      this.currentPage = 1;
      this.getDataList();
    },
    // 重置筛选表单
    resetScreenForm() {
      this.$refs.screenForm.resetFields();
      this.currentPage = 1;
      this.getDataList();
    },
    // 更改每页数量
    handleSizeChange(val) {
      this.pageSize = val;
      this.currentPage = 1;
      this.getDataList();
    },
    // 更改当前页
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getDataList();
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .el-date-editor {
  width: 100%;
}
::v-deep .el-select {
  width: 100%;
}
::v-deep .el-col-9 .el-button {
  padding: 5px;
}
::v-deep .el-dialog__header {
  background-color: #dddddd;
}
</style>