<template>
  <div class="app-container">
    <!-- 筛选条件 -->
    <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>

        <el-popconfirm @onConfirm="deleFn" title="这是一段内容确定删除吗？">
          <el-button
            type="primary"
            size="small"
            class="textColor"
            slot="reference"
            >批量删除</el-button
          >
        </el-popconfirm>
      </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"
          @select="hanleSelect"
          @select-all="hanleSelect"
          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="customerNumber"
            min-width="160"
            show-overflow-tooltip
          ></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="mainName"
            min-width="160"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="center"
            label="业务员"
            prop="serviceName"
            min-width="160"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="center"
            label="创建人"
            prop="createBy"
            min-width="160"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="center"
            label="创建时间"
            prop="createTime"
            min-width="160"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="center"
            label="更新人"
            prop="updateBy"
            min-width="160"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="center"
            label="更新时间"
            prop="updateTime"
            min-width="160"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="center"
            label="操作"
            prop="caozuo"
            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="dialogForm"
      width="30%"
      :show-close="false"
      :close-on-click-modal="false"
    >
      <el-form
        ref="addForm"
        :model="addForm"
        :rules="rules"
        label-width="100px"
        label-position="right"
      >
        <el-form-item label="经销商名称" prop="customerId">
          <el-select
            class="selectStyle"
            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-form-item label="产品大类" prop="mainId">
          <el-select
            class="selectStyle"
            v-model="addForm.mainId"
            placeholder="请选择"
          >
            <el-option
              v-for="item in categoryList"
              :key="item.productCategoryId"
              :label="item.productCategoryName"
              :value="item.productCategoryId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="一级区域" prop="oneParentId">
          <el-select
            class="selectStyle"
            v-model="addForm.oneParentId"
            placeholder="请选择"
          >
            <el-option
              v-for="item in oneList"
              :key="item.adminWebsitId"
              :label="item.name"
              :value="item.adminWebsitId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="二级区域" prop="twoParentId">
          <el-select
            class="selectStyle"
            v-model="addForm.twoParentId"
            placeholder="请选择"
          >
            <el-option
              v-for="item in twoList"
              :key="item.adminWebsitId"
              :label="item.name"
              :value="item.adminWebsitId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="三级区域" prop="threeParentId"
          ><el-select
            class="selectStyle"
            v-model="addForm.threeParentId"
            placeholder="请选择"
          >
            <el-option
              v-for="item in threeList"
              :key="item.adminWebsitId"
              :label="item.name"
              :value="item.adminWebsitId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="对应业务员" prop="serviceId">
          <el-select
            class="selectStyle"
            v-model="addForm.serviceId"
            placeholder="请选择"
          >
            <el-option
              v-for="(item, i) in userList"
              :key="i"
              :label="item.nickName"
              :value="item.adminUserId"
            >
            </el-option>
          </el-select>
        </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 {
  getDealerStockList,
  getDealerStockAdd,
  deleDealerStockList,
  getDictionaries,
  getProductCategoryList,
  getAdminWebsitByparent,
  getAdminUserList,
  getCustomerPtDetail,
  getCustomerPtUpdate,
} from "@/api/basic_data/dealer";
import { getDealerList } from "@/api/basic_data/dealer";

export default {
  data() {
    return {
      title: "",
      dialogForm: false,
      addForm: {
        customerId: "",
        oneParentId: "",
        twoParentId: "",
        threeParentId: "",
        serviceId: "",
        mainId: "",
      },
      rules: {
        mainId: [
          { required: true, message: "请选择存货分类", trigger: "blur" },
        ],
        customerId: [
          { required: true, message: "请选择经销商", trigger: "blur" },
        ],
        oneParentId: [
          { required: true, message: "请选择一级区域", trigger: "blur" },
        ],
        twoParentId: [
          { required: true, message: "请选择二级区域", trigger: "blur" },
        ],
        threeParentId: [
          { required: true, message: "请选择三级区域", trigger: "blur" },
        ],
        serviceId: [
          { required: true, message: "请选择业务员", trigger: "blur" },
        ],
      },

      currentPage: 1, // 当前页码
      pageSize: 10, // 每页数量
      listTotal: 0, // 列表总数

      searchForm: {
        customerName: "",
      },
      dataList: [], // 列表数据
      listLoading: false, // 列表加载loading
      dealerList: [],
      selectList: [],
      categoryList: [], //产品大类列表
      oneList: [], //一级区域数据
      twoList: [], //二级区域数据
      threeList: [], //三级区域数据
      userList: [], //业务员
      ids: [],
      rowID: null, //编辑ID
    };
  },
  async created() {
    this.getList({ pageNum: 1, pageSize: 10 });
    this.getDealerDataList({ pageNum: -1, pageSize: 10 });
    this.getSelectList({ sysDictEnum: "PRODUCT_TYPE" });
    this.getCategoryList();
    this.getAdminWebsit(1);
  },
  computed: {},
  watch: {
    "addForm.oneParentId": async function (newValue) {
      if (newValue) {
        let res = await getAdminWebsitByparent({ parentId: newValue });
        this.twoList = res.data;
      }
    },
    "addForm.twoParentId": async function (newValue) {
      if (newValue) {
        let res = await getAdminWebsitByparent({ parentId: newValue });
        this.threeList = res.data;
      }
    },
    "addForm.threeParentId": async function (newValue) {
      if (newValue) {
        let res = await getAdminUserList({
          pageNum: 1,
          pageSize: 10,
          isCustomer: 0,
          adminWebsitId: newValue,
        });
        this.userList = res.data.records;
      }
    },
  },
  methods: {
    //编辑

    async editFn(id) {
      this.title = "编辑";
      this.rowID = id;
      let res = await getCustomerPtDetail({ id });
      console.log(res);
      this.addForm.customerId = res.data.customerId;
      this.addForm.mainId = res.data.mainId;
      this.addForm.oneParentId = res.data.adminWebsitId2;
      this.addForm.twoParentId = res.data.adminWebsitId3;
      this.addForm.threeParentId = res.data.adminCompanyId;
      this.addForm.serviceId = res.data.serviceId;

      this.dialogForm = true;
    },
    //获取业务员数据
    async getUserList() {
      // await getAdminUserList({ pageNum: 1 });
    },
    //根据父级查询部门
    async getAdminWebsit(data) {
      let res = await getAdminWebsitByparent({ parentId: data });
      this.oneList = res.data;
    },
    //获取产品品类数据
    async getCategoryList() {
      let res = await getProductCategoryList();
      console.log(res);
      this.categoryList = res.data;
    },
    // 筛选部分数据或者单个
    hanleSelect(selection) {
      // this.ids = selection.map((k) => {
      //   return k.id;
      // });
      console.log(selection);
      this.ids = selection.map((v) => v.id);
    },
    //存货分类
    async getSelectList(data) {
      const res = await getDictionaries(data);
      console.log(res);
      this.selectList = res.data;
    },
    //删除
    async deleFn(id) {
      this.ids.push(id);
      let res = this.ids.toString();
      await deleDealerStockList({ ids: res });
      this.getList({ pageNum: 1, pageSize: 10 });
      this.$message.success("删除成功");
    },
    addFn() {
      this.title = "新增";
      this.dialogForm = true;
    },
    async addDataListFn() {
      await this.$refs.addForm.validate();
      console.log(this.addForm);
      let data = this.dealerList.filter((v) => {
        return v.id === this.addForm.customerId;
      });

      let data2 = this.categoryList.filter((v) => {
        return v.productCategoryId == this.addForm.mainId;
      });
      let data3 = this.userList.filter((v) => {
        return v.adminUserId == this.addForm.serviceId;
      });

      let value = {
        customerId: this.addForm.customerId,
        customerName: data[0].name,
        customerNumber: data[0].number,
        mainId: this.addForm.mainId,
        mainName: data2[0].productCategoryName,
        serviceId: this.addForm.serviceId,
        serviceName: data3[0].nickName,
      };

      if (this.title == "编辑") {
        await getCustomerPtUpdate({ ...value, id: this.rowID });

        this.$message.success("编辑成功");
      } else {
        await getDealerStockAdd(value);
        this.$message.success("添加成功");
      }
      this.addForm.customerId = "";
      this.addForm.mainId = "";
      this.addForm.oneParentId = "";
      this.addForm.twoParentId = "";
      this.addForm.threeParentId = "";
      this.addForm.serviceId = "";
      this.userList = [];
      this.twoList = [];
      this.threeList = [];
      this.getList({ pageNum: 1, pageSize: 10 });
      this.dialogForm = false;
    },
    //取消
    async cancelFn() {
      await this.$refs.addForm.clearValidate();
      this.addForm.customerId = "";
      this.addForm.mainId = "";
      this.addForm.oneParentId = "";
      this.addForm.twoParentId = "";
      this.addForm.threeParentId = "";
      this.addForm.serviceId = "";
      this.userList = [];
      this.twoList = [];
      this.threeList = [];
      this.dialogForm = false;
    },
    //获取经销商数据
    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({
        cusotmerName: this.searchForm.customerName,
        pageNum: 1,
        pageSize: 10,
      });
    },
    //重置
    clearFn() {
      console.log(this.$refs.searchForm);
      this.$refs.searchForm.resetFields();
    },
    //获取列表数据
    async getList(data) {
      const res = await getDealerStockList(data);
      console.log(res);
      this.dataList = res.data.records;
      this.listTotal = res.data.total;
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .el-popover__reference {
  margin-left: 10px;
}
// ::v-deep .selectStyle .el-input--suffix {
//   width: 200%;
// }

// ::v-deep .el-input--suffix {
//   width: 300px;
// }
// ::v-deep .el-dialog__header {
//   background-color: #dddddd;
// }
// ::v-deep .dialog-footer {
//   display: flex;
//   justify-content: center;
// }

// .formWidth {
//   width: 70%;
//   margin-right: 20px;
// }
.selectStyle {
  width: 100%;
}
</style>
