<template>
  <div class="app-container">
    <el-row>
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="108px">
        <el-form-item label="快递公司编码" prop="company">
          <el-input
            v-model="queryParams.company"
            placeholder="请输入快递公司编码"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="店铺" prop="shopId">
          <el-select v-model="queryParams.shopId" placeholder="请选择店铺" clearable @change="handleQuery">
            <el-option
              v-for="item in shopList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>

      </el-form>

    </el-row>


    <el-row :gutter="10" class="mb8">

      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-time"
          size="mini"
          @click="updateWaybillAccount"
        >更新电子面单账户信息</el-button>
      </el-col>

<!--      <el-col :span="1.5">-->

<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-printer"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--        >手动添加</el-button>-->
<!--      </el-col>-->

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="deliverList" @selection-change="handleSelectionChange">
<!--       <el-table-column type="selection" width="55" align="center" />-->
<!--      <el-table-column label="ID" align="center" prop="id" />-->
      <el-table-column label="分配的供应商" align="left" width="211">
        <template slot-scope="scope">
          {{ scope.row.supplierIds ? supplierList.filter(item =>
          scope.row.supplierIds.replace(/^,|,$/g, '').split(',').map(Number).includes(item.id)
        ).map(item => item.name).join(', ') : '' }}
        </template>
      </el-table-column>
      <el-table-column label="网点" align="left" prop="netsiteName" width="200px">
      </el-table-column>
      <el-table-column label="网点编号" align="center" prop="netsiteCode" width="100px" />
      <el-table-column label="快递公司编码" align="center" prop="company" width="100px" />
      <el-table-column label="客户编码" align="center" prop="sellerId" width="100px" />
      <el-table-column label="店铺" align="left" prop="netsiteName" width="200px">
        <template slot-scope="scope">
          {{ shopList.find(x=>x.id === scope.row.shopId) ? shopList.find(x=>x.id === scope.row.shopId).name : '' }}
        </template>
      </el-table-column>
      <el-table-column label="已取单号" align="center" prop="allocatedQuantity" width="100px" />
      <el-table-column label="已取消单号" align="center" prop="cancelledQuantity" width="100px" />
      <el-table-column label="已回收单号" align="center" prop="recycledQuantity" width="100px" />
      <el-table-column label="余额" align="center" prop="amount" width="100px" />
      <el-table-column label="发货地址" align="left" prop="provinceName" width="200">
        <template slot-scope="scope">
          {{scope.row.provinceName}} {{scope.row.cityName}} {{scope.row.districtName}} {{scope.row.streetName}}<br/>
          {{scope.row.detailAddress}}
        </template>
      </el-table-column>
<!--      <el-table-column label="发货信息" align="left" prop="provinceName" width="200">-->
<!--        <template slot-scope="scope">-->
<!--          {{scope.row.name}} <br/>-->
<!--          {{scope.row.mobile}}-->
<!--        </template>-->
<!--      </el-table-column>-->

<!--      <el-table-column label="省" align="center" prop="provinceName"/>-->
<!--      <el-table-column label="市" align="center" prop="cityName"/>-->
<!--      <el-table-column label="区" align="center" prop="districtName"/>-->
      <el-table-column label="发货人" align="center" prop="name"/>
      <el-table-column label="发货手机号" align="center" prop="mobile"/>


      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-share"
            @click="handleShareSupplier(scope.row)"
          >分配给供应商</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 手动添加 -->
    <el-dialog title="手动添加" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="店铺" prop="shopId">
          <el-select v-model="queryParams.shopId" placeholder="请选择店铺" clearable @change="handleQuery">
            <el-option
              v-for="item in shopList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="卖家Id" prop="sellerId">
          <el-input v-model.number="form.sellerId" placeholder="请输入卖家Id名" />
        </el-form-item>
        <el-form-item label="appKey" prop="appKey" v-if="form.type===500">
          <el-input v-model="form.appKey" placeholder="请输入appKey" />
        </el-form-item>
        <el-form-item label="描述" prop="remark">
          <el-input type="textarea" v-model="form.remark" placeholder="请输入描述" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="状态">
            <el-option label="启用" value="1"></el-option>
            <el-option label="禁用" value="0"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addSubmit">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="修改" :visible.sync="updateOpen" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="网点名" prop="netsiteName">
          <el-input v-model="form.netsiteName" placeholder="请输入网点名" />
        </el-form-item>
        <el-form-item label="网点编码" prop="netsiteCode">
          <el-input v-model="form.netsiteCode" placeholder="请输入网编码" />
        </el-form-item>
        <el-form-item label="卖家Id" prop="sellerShopId">
          <el-input v-model.number="form.sellerShopId" placeholder="请输入卖家Id名" />
        </el-form-item>
        <el-form-item label="发货人" prop="name" >
          <el-input v-model="form.name" placeholder="请输入发货人" />
        </el-form-item>
        <el-form-item label="发货手机号" prop="mobile" >
          <el-input v-model="form.mobile" placeholder="请输入发货手机号" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="updateSubmit">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="分配供应商" :visible.sync="shareOpen" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="供应商" prop="supplierIds">
          <el-select v-model="form.supplierIds" placeholder="请选择供应商" clearable multiple >
            <el-option
              v-for="item in supplierList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="shareSupplierSubmit">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import {listSupplier} from "@/api/goods/supplier";
import {listShop} from "@/api/shop/shop";
import {listOrder, getOrder } from "@/api/order/order";
import {
  getWaybillAccountList,
  pullWaybillAccount,
  shareSupplier,
  updateAccount
} from "@/api/dou/ewaybill";

export default {
  name: "waybillAccountDou",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      shopList: [],
      deliverList: [],
      supplierList: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: "",
      open:false,
      shareOpen:false,
      updateOpen:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        refundStatus: 1,
        platformId:400,
        orderStatus: '1',
        shopId: null
      },
      // 打印参数
      printParams: {
        deliver: null,
        printer: null
      },
      // 表单参数
      form: {
        id: null,
        sellerShopId:null,
        netsiteName:null,
        netsiteCode:null,
        company:null,
        name:null,
        mobile:null,
        supplierIds: null
      },

      // 表单校验
      rules: {
        netsiteName: [{ required: true, message: '不能为空', trigger: "blur" }],
        netsiteCode: [{ required: true, message: '不能为空', trigger: "blur" }],
        // sellerShopId: [{ required: true, message: '不能为空', trigger: "blur" }],
        name: [{ required: true, message: '不能为空', trigger: "blur" }],
        mobile: [{ required: true, message: '不能为空', trigger: "blur" }],
      }
    };
  },
  created() {
    listShop({type: this.queryParams.platformId}).then(response => {
      this.shopList = response.rows;
      if (this.shopList && this.shopList.length > 0) {
        this.queryParams.shopId = this.shopList[0].id
      }
      this.getList();
    });
    listSupplier({pageNum: 1, pageSize: 100}).then(resp=>{
      this.supplierList = resp.rows

    })
  },
  methods: {
    /** 查询商品管理列表 */
    getList() {
      this.loading = true;
      getWaybillAccountList({shopId: this.queryParams.shopId}).then(response => {
        this.deliverList = response.data;
      });

      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.updateOpen = false
      this.shareOpen = false
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        erpSkuId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.orderNum)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    // 更新电子面单信息
    updateWaybillAccount() {
      pullWaybillAccount({shopId: this.queryParams.shopId}).then(response => {
        this.getList()
      });
    },
    handleAdd() {
      this.open = true
    },
    addSubmit() {
      this.$refs["form"].validate(valid => {
        if (valid) {

        }
      })
    },
    handleUpdate(row){
      this.form.id = row.id
      this.form.netsiteName = row.siteName
      this.form.netsiteCode = row.siteCode
      this.form.sellerShopId = row.sellerShopId
      this.form.name = row.name
      this.form.mobile = row.mobile
      this.updateOpen = true
    },
    updateSubmit(){
      this.$refs["form"].validate(valid => {
        if (valid) {
          updateAccount(this.form).then(resp=>{
            this.$modal.msgSuccess("保存成功")
            this.updateOpen =false
            this.getList()
          })
        }
      })

    },
    handleShareSupplier(row) {
      this.form.id = row.id
      if (row.supplierIds) {
        let numArray = row.supplierIds.replace(/^,|,$/g, '').split(',').map(Number);
        this.form.supplierIds = numArray
      } else this.form.supplierIds = null
      this.shareOpen = true
    },
    shareSupplierSubmit(){
      console.log("=======分配供应商ID=====",this.form.supplierIds)
      shareSupplier(this.form).then(resp=>{
        this.$modal.msgSuccess("保存成功")
        this.shareOpen =false
        this.getList()
      })
    }
  }
};
</script>
