<template>
  <div class="page">
    <topBar
      v-if="topList.length > 0"
      :topList="topList"
      :moren="moren"
    ></topBar>
    <div class="thetopbox">
      <el-form label-width="100px">
        <el-row>
          <el-col :span="4">
            <el-form-item :label="'仓库名称'">
              <el-input clearable v-model="queryParams.name"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item :label="'产品名称'">
              <el-input clearable v-model="queryParams.productName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="'创建日期'">
              <el-date-picker
                clearable
                v-model="queryParams.creatTime"
                @change="checkTime"
                type="daterange"
                range-separator="——"
                :start-placeholder="'开始日期'"
                :end-placeholder="'结束日期'"
                value-format="yyyy-MM-dd"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :span="4">
            <div class="searchbox">
              <el-button class="my_search" @click="getSearch">
                {{ '搜索' }}</el-button
              >
              <el-button class="my_reset" @click="reChongzhi">
                {{ '重置' }}</el-button
              >
            </div>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="main">
      <div class="maintop">
        <div class="mainbtn">
          <el-button
            size="mini"
            v-has-buttons="['bindWarehouseConfigAdd']"
            class="thebtn1"
            @click="addData"
            >{{ '添加' }}</el-button
          >
          <el-button size="mini"   v-has-buttons="['bindWarehouseConfigExport']" class="thebtn2" @click="handleExport">
            {{ '导出' }}</el-button
          >
        </div>
      </div>
      <div class="maintable">
        <el-table
          :data="tableData"
          v-loading="loading"
          height="680px"
          style="width: 100%"
          :header-cell-style="{ background: '#EEEEEE' }"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55"> </el-table-column>
          <el-table-column
            align="center"
            prop="code"
            :label="'仓库编号'"
            min-width="120px"
          >
          </el-table-column>
          <el-table-column align="center" prop="name" :label="'仓库名称'">
          </el-table-column>
          <el-table-column
            align="center"
            width="150px"
            prop=""
            :label="'绑定地区'"
          >
            <template slot-scope="scope">
              <el-popover
                placement="top-start"
                title=""
                width="200"
                trigger="hover"
              >
                <div>{{ scope.row.samstrAll }}</div>
                <div slot="reference">{{ scope.row.samstr }}</div>
              </el-popover></template
            >
          </el-table-column>
          <el-table-column align="center" prop="contacts" :label="'联系人'">
          </el-table-column>
          <el-table-column
            align="center"
            prop="productCode"
            min-width="150px"
            :label="'产品编号'"
          >
          </el-table-column>
          <el-table-column
            min-width="120px"
            align="center"
            prop="productName"
            :label="'产品名称'"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="mobilePhone"
            :label="'联系方式'"
          >
          </el-table-column>
          <el-table-column
            min-width="120px"
            align="center"
            prop="provinceval"
            :label="'省'"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="cityval"
            :label="'市'"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="countyval"
            :label="'区/县'"
          >
          </el-table-column>
          <el-table-column
            align="center"
            min-width="150px"
            prop="wareAddress"
            :label="'详细地址'"
          >
          </el-table-column>

          <el-table-column
            align="center"
            min-width="120px"
            prop="creationTime"
            :label="'创建日期'"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="time"
            :label="'操作'"
            min-width="120px"
          >
            <template slot-scope="scope">
              <el-button
                @click.native.prevent="update(scope.row)"
                type="text"
                size="small"
                class="button-s"
                style="color: #009b58"
                v-has-buttons="['bindWarehouseConfigUpdate']"
              >
                {{ '修改' }}
              </el-button>
              <el-button
                class="button-s"
                @click.native.prevent="deleteRow(scope.row)"
                type="text"
                size="small"
                v-has-buttons="['bindWarehouseConfigDelete']"
                style="color: #e41b1b"
              >
                {{ '删除' }}
              </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="getSearch"
    />
    <el-dialog
      :title="dialogTitle"
      :close-on-click-modal="false"
      :visible.sync="dialogVisible"
      width="40%"
      center
      :before-close="handleClose"
    >
      <el-form ref="form" :rules="rules" :model="form" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item :label="'产品名称'" prop="pkProduct">
              <el-select
                clearable
                v-model="form.pkProduct"
                :placeholder="'请选择'"
              >
                <el-option
                  v-for="(item, index) in productList"
                  :key="index"
                  :label="item.productName"
                  :value="item.pkId"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="'发货仓库'" prop="pkStorehouse">
              <el-select
                clearable
                v-model="form.pkStorehouse"
                :placeholder="'请选择'"
              >
                <el-option
                  v-for="(item, index) in storehouseList"
                  :key="index"
                  :label="item.name"
                  :value="item.pkId"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item :label="'绑定地区'" prop="provinceList">
              <el-checkbox-group v-model="form.provinceList">
                <el-checkbox
                  v-for="item in areaData"
                  :key="item.id"
                  :label="item.id"
                  >{{ item.name }}</el-checkbox
                >
              </el-checkbox-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">{{ '取消' }}</el-button>
        <el-button type="primary" @click="onSubmit('form')">{{
          '确认'
        }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>
  
  <script>
import topBar from "@/components/topBar";
import * as api from "@/api/site.js";
import * as sys from "@/api/system";
import * as del from "@/api/delivery";

export default {
  name: "BindWarehouseConfig",
  components: {
    topBar,
  },
  data() {
    return {
      moren: "bindWarehouseConfig",
      topList: [
        {
          name: "产品绑定仓库配置",
          path: "bindWarehouseConfig",
        },
      ],
      tableData: [],
      loading:false,
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        creatTime: [],
      },
      total: 0,
      multipleSelection: [],
      dialogVisible: false,
      rules: {
        pkStorehouse: [
          { required: true, message: '请选择省', trigger: "change" },
        ],
        pkProduct: [
          { required: true, message: '请选择省', trigger: "change" },
        ],
        provinceList: [
          { required: true, message: '请选择省', trigger: "change" },
        ],
      },
      form: {
        pkStorehouse: "",
        pkProduct: "",
        provinceList: [],
      },
      ifadd: "", //是否添加 添加1 修改2
      dialogTitle: "",
      areaData: [],
      storehouseList: [], //仓库下拉选
      productList: [], //产品下拉选
    };
  },
  created() {
    this.getSearch();
    this.getData();
  },
  methods: {
    //列表展示考核
    update(row) {
      this.dialogTitle = '修改';
      sys.getBdProductStorehouse(row.pkId).then((res) => {
        if (!res.data.provinceList) {
          this.$set(this.form, "provinceList", []);
        }
        this.form = res.data;

        // let arr = [
        //   res.data.province,
        //   res.data.city + "",
        //   (res.data.county || "") + "",
        // ];
        // arr = arr.filter((item) => item != "");
        // this.$set(this.form, "shoppingAddress", arr);
        this.$forceUpdate();
        this.ifadd = 2;
        this.dialogVisible = true;
      });
    },
    handleExport() {
      this.$confirm('是否确认导出所有数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: "warning",
      }).then((_) => {
        this.download(
          "/system/manage/bd-product-storehouse/export",
          { ...this.queryParams },
          `产品绑定仓库配置${new Date().getTime()}.xlsx`
        );
      });
    },
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = "";
          if (this.ifadd == 1) {
            url = sys.saveBdProductStorehouse;
          } else if (this.ifadd == 2) {
            url = sys.updateBdProductStorehouse;
          }
          url(this.form).then((res) => {
            if (res.code == 200) {
              this.$message({
                message: res.msg,
                type: "success",
              });
              this.dialogVisible = false;
              this.getSearch();
            }
          });
        }
      });
    },

    handleClose() {
      this.dialogVisible = false;
      this.$refs["form"].clearValidate();
      this.$refs["form"].resetFields();
    },
    checkTime(val) {
      this.queryParams.startDate = val ? val[0] : "";
      this.queryParams.endDate = val ? val[1] : "";
    },

    getData() {
      del.getStorehouseList().then((res) => {
        this.storehouseList = res.rows;
      });
      del.getProductAll().then((res) => {
        this.productList = res.data;
      });
      api.areaList().then((res) => {
        this.areaData = res.data;
        this.areaData.forEach((ele, index) => {
          this.areaData[index].id = Number(ele.id);
        });
      });
    },
    addData() {
      this.ifadd = 1;
      this.dialogTitle = '添加';
      this.form = {
        provinceList: [],
      };
      sys.getStoreHouseCode().then((res) => {
        if (res.code == 200) {
          this.dialogVisible = true;
        }
      });
    },

    //删除
    deleteRow(row) {
      this.$confirm('确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: "warning",
      }).then(() => {
        sys.delBdProductStorehouse(row.pkId).then((res) => {
          if (res.code == 200) {
            this.$message({
              message: res.msg,
              type: "success",
            });
          }
          this.getSearch();
        });
      });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    reChongzhi() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 50,
        shoppingAddress: ["", "", ""],
      };
      this.getSearch();
    },
    getSearch() {
      this.loading = true
      sys.getBindProductStorehouseList(this.queryParams).then((res) => {
        this.loading = false
        if (res.code == 200) {
          res.rows.forEach((item) => {
            if (item.samList && item.samList.length > 0) {
              item.samstrAll = item.samList
                .map((obj) => obj.provinceVal)
                .join(",");
              if (item.samstrAll.length > 8) {
                item.samstr = item.samstrAll.substring(0, 8) + "...";
              } else {
                item.samstr = item.samstrAll;
              }
            }
          });
          this.tableData = res.rows;
          this.total = res.total;
        }
      });
    },
  },
};
</script>
  
  <style lang="scss" scoped>
::v-deep .el-range-editor.el-input__inner {
  width: 100%;
}

::v-deep .el-table thead {
  color: #000000;
}

::v-deep .el-table .warning-row {
  background: #f9f9f9;
}

.page {
  padding: 10px 20px;
  background: #f9f9f9;
  font-size: 14px;
  .thetopbox {
    padding: 5px 20px 0px 0;
    background: #ffffff;
    border-radius: 8px;
    .searchbox {
      display: flex;
      align-items: center;
      margin-left: 40px;
    }
    .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 {
          color: #ffffff;
          background: #48b2fd;
        }
        .thebtn2 {
          color: #ffffff;
          background: #ffad41;
        }
      }
    }
  }
}
</style>
  