<template>
  <section class="goods pagex">
    <div class="goods-con">
      <el-form :inline="true" :model="formInline" label-width="80px" ref="formInline">
        <el-form-item label="发货单号" prop="stock_in_sn">
          <el-input v-model="formInline.stock_in_sn" placeholder="请输入发货单号"></el-input>
        </el-form-item>
        <el-form-item label="型号" prop="goods_name">
          <el-autocomplete v-model="formInline.goods_name" :trigger-on-focus="false" @keyup.enter.native="onSubmit"
                           :fetch-suggestions="querySearchAsync" placeholder="请输入型号" clearable></el-autocomplete>
        </el-form-item>
        <el-form-item label="品牌" prop="brand_name">
          <el-autocomplete v-model="formInline.brand_name" :trigger-on-focus="false" @keyup.enter.native="onSubmit"
                           :fetch-suggestions="querySearchAsyncBrand" placeholder="请输入品牌" clearable></el-autocomplete>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="formInline.status" placeholder="请选择">
            <el-option label="全部" value=""></el-option>
            <el-option label="待处理" value="-1"></el-option>
            <el-option label="完成" value="2"></el-option>
            <el-option label="作废" value="-3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submit">查询</el-button>
          <el-button @click="resetForm('formInline')">重置</el-button>
        </el-form-item>
      </el-form>
      <div class="btn-nav clr">
        <el-button type="primary" @click="cancelSend">取消发货</el-button>
      </div>
      <div class="data-box th-all" v-if="tableData">
        <el-table :data="tableData" border max-height="600" @selection-change="handleSelectionChange">
          <el-table-column fixed type="selection" width="37"></el-table-column>
          <el-table-column prop="stock_in_sn" label="发货单号" width="160"></el-table-column>
          <el-table-column prop="goods_name" label="型号" min-width="150"></el-table-column>
          <el-table-column prop="brand_name" label="品牌" min-width="150"></el-table-column>
          <el-table-column prop="out_qty" label="发货数量" min-width="80"></el-table-column>
          <el-table-column prop="in_qty" label="到货数量" min-width="80"></el-table-column>
          <el-table-column prop="status_val" label="状态" min-width="60"></el-table-column>
          <el-table-column prop="shipping_name" label="物流公司" min-width="80"></el-table-column>
          <el-table-column prop="create_time" label="创建时间" width="150"></el-table-column>
          <el-table-column prop="create_name" label="创建人" min-width="100"></el-table-column>
          <el-table-column prop="purchase_name" label="订单人员" min-width="100"></el-table-column>
        </el-table>
        <el-pagination layout="total, sizes, prev, pager, next, jumper" :page-sizes="[10, 20, 50, 100, 200]" :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="page"></el-pagination>
      </div>
    </div>
    <Menu/>
  </section>
</template>
<script>
import Vue from 'vue';
import Menu from "@/components/menu.vue";
import {Autocomplete, Button, Dialog, Form, FormItem, Input, Message, Option, Pagination, Select, Table, TableColumn} from 'element-ui'

Vue.prototype.$message = Message;
Vue.use(Button).use(Form).use(Select).use(Option).use(Input).use(FormItem).use(Dialog).use(Pagination);
Vue.use(TableColumn).use(Table).use(Autocomplete);
export default {
  name: "orderTrackInvoice",
  data() {
    return {
      total: 0,
      limit: 10,
      page: 1,
      formInline: {
        stock_in_sn: "",
        goods_name: '',
        brand_name: '',
        status: ''
      },
      tableData: "",
      multipleSelection: [], //选择数据
    };
  },
  created() {
    this.getData();
  },
  computed: {},
  methods: {
    getData() {
      this.$http('post', "/api/purchase/getDeliveryList", {
        page: this.page,
        limit: this.limit,
        stock_in_sn: this.formInline.stock_in_sn,
        brand_name: this.formInline.brand_name,
        goods_name: this.formInline.goods_name,
        status: this.formInline.status,
      }).then(res => {
        if (res.code === 0) {
          this.tableData = res.data.list || [];
          this.total = Number(res.data.total) || 0;
        } else {
          this.$message(res.msg);
        }
      })
    },
    handleCurrentChange(val) {
      this.page = val;
      this.getData();
    },
    handleSizeChange(val) {
      this.limit = val;
      this.getData();
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    submit() {
      this.page = 1;
      this.getData();
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    //取消发货
    cancelSend() {
      var self = this;
      if (this.multipleSelection.length <= 0) {
        this.$message({
          message: "请勾选数据进行操作",
          type: 'warning'
        });
        return
      }

      let stock_in_ids = [];
      stock_in_ids = this.multipleSelection.map(obj => {
        return obj.stock_in_id;
      })

      let source_arr = [];
      let status_arr = [];
      source_arr = this.multipleSelection.map(obj => {
        return obj.source;
      })
      status_arr = this.multipleSelection.map(obj => {
        return obj.status;
      })
      let source = source_arr.every(ele => ele === 2)
      if (source) {
        let status = status_arr.every(ele => ele === 2)
        if (status) {
          this.$http('POST', "/api/purchase/cancelSupDelivery", {stock_in_ids: stock_in_ids.join(',')}).then(res => {
            if (res.code == 0) {
              this.$message({
                message: "操作成功",
                type: 'success',
                duration: 2000,
                onClose() {
                  self.getData();
                }
              });
            } else {
              this.$message({
                message: '请选择作废待入库状态',
                type: 'error'
              });
            }
          })
        } else {
          this.$message({
            message: '请选择云芯系统的发货单',
            type: 'warning'
          });
        }
      } else {
        this.$message({
          message: '请选择云芯系统的发货单',
          type: 'warning'
        });
      }
    },
    //型号
    querySearchAsync(queryString, cb) {
      this.$http('get', "/api/search/getspu", {
        spu_name: queryString
      }).then(res => {
        if (res.code == 0) {
          if (res.data.list.length > 0) {
            var arrlist_ = res.data.list || [];
            var arr_ = []
            for (var i = 0; i < arrlist_.length; i++) {
              arr_.push({
                value: arrlist_[i]['spu_name']
              })
            }
            cb(arr_);
          } else {
            cb([]);
          }
        }
      })
    },
    //品牌
    querySearchAsyncBrand(queryString, cb) {
      this.$http('get', "/api/brand/get_stand_brand", {
        brand: queryString
      }).then(res => {
        if (res.code == 0) {
          if (res.data.list.length > 0) {
            var arrlist_ = res.data.list || [];
            var arr_ = []
            for (var i = 0; i < arrlist_.length; i++) {
              arr_.push({
                value: arrlist_[i]
              })
            }
            cb(arr_);
          } else {
            cb([]);
          }
        }
      })
    },
  },
  components: {
    Menu
  }
};
</script>
<style scoped>
@import "../../assets/css/goods/goods.min.css";
</style>