<template>
  <section class="goods pagex">
    <div class="goods-con">
      <!--搜索区-->
      <el-form :inline="true" :model="formInline"  ref="formInline" label-width="100px">
        <el-form-item label="发货单号" prop="stock_in_sn">
          <el-input v-model="formInline.stock_in_sn" placeholder="请输入发货单号" clearable></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="请选择" clearable>
            <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 label="质检结果" prop="status">
          <el-select v-model="formInline.qc_result_type" placeholder="请选择" clearable>
            <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-option label="待处理" value="4"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="创建时间">
          <el-date-picker :unlink-panels=true style="width:193px" prefix-icon="prefix-icon-time-style"  v-model="create_time_val" value-format="yyyy-MM-dd" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
        </el-form-item>
        <el-form-item label="质检时间">
          <el-date-picker :unlink-panels=true style="width:193px" prefix-icon="prefix-icon-time-style"  v-model="qc_time_val" value-format="yyyy-MM-dd" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
        </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="operation-area row verCenter bothSide">
        <div class="operation-button row verCenter">
          <el-button type="primary" @click="cancelSend">取消发货</el-button>
        </div>
        <div class="text-tip row verCenter">
          <i class="el-icon-warning"></i>
          <span>该页面展示的数据为:贵司向猎芯客户发出的货品</span>
        </div>
      </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="40" align="center"></el-table-column>
          <el-table-column label="发货信息">
            <el-table-column prop="stock_in_sn" label="发货单号" width="160" :show-overflow-tooltip="true"></el-table-column>
            <el-table-column prop="goods_name" label="型号" min-width="150" :show-overflow-tooltip="true" align="center"></el-table-column>
            <el-table-column prop="brand_name" label="品牌" min-width="150" :show-overflow-tooltip="true" align="center"></el-table-column>
            <el-table-column prop="out_qty" label="发货数量" min-width="80" :show-overflow-tooltip="true" align="center"></el-table-column>
            <el-table-column prop="in_qty" label="到货数量" min-width="80" :show-overflow-tooltip="true" align="center"></el-table-column>
            <el-table-column prop="status_val" label="状态" min-width="100" :show-overflow-tooltip="true" align="center">
              <template slot-scope="scope">
                <el-tag type="success" v-if="scope.row.status == 2 " disable-transitions>{{ scope.row.status_val }}</el-tag>
                <el-tag type="primary" v-else-if="scope.row.status == 1 " disable-transitions>{{ scope.row.status_val }}</el-tag>
                <el-tag type="info" v-else-if="scope.row.status == -3 " disable-transitions>{{ scope.row.status_val }}</el-tag>
                <el-tag type="primary" v-else>{{ scope.row.status_val }}</el-tag>
                <el-tooltip :aa="scope" class="item" effect="dark" placement="top-start">
                  <i class="el-icon-question" style="color:#ff7e11;margin-left:5px;cursor:pointer;font-size:16px;"></i>
                  <div slot="content">{{ scope.row.status_tips }}</div>
                </el-tooltip>
              </template>
            </el-table-column>
            <el-table-column prop="shipping_name" label="物流公司" min-width="80" align="center"></el-table-column>
            <el-table-column prop="create_name" label="创建人" min-width="100" align="center"></el-table-column>
            <el-table-column prop="purchase_name" label="订单人员" min-width="100" align="center"></el-table-column>
            <el-table-column prop="create_time" label="创建时间" width="150" align="center"></el-table-column>
          </el-table-column>
          <el-table-column label="质检信息">
            <el-table-column prop="qc_type_val" label="质检结果" width="100">
              <template slot-scope="scope">
                <template v-if="scope.row.qc_type_val">
                  <span>{{ scope.row.qc_type_val }}</span>
                  <el-tooltip :aa="scope" class="item" effect="dark" placement="top-start">
                    <i class="el-icon-question" style="color:#ff7e11;margin-left:5px;cursor:pointer;font-size:16px;"></i>
                    <div slot="content">{{ scope.row.qc_type_tips }}</div>
                  </el-tooltip>
                </template>
              </template>
            </el-table-column>
            <el-table-column prop="package" label="封装" width="100" align="center"></el-table-column>
            <el-table-column prop="silk_screen" label="丝印" width="100" align="center"></el-table-column>
            <el-table-column prop="" label="质检图片" width="100" align="center">
              <template slot-scope="scope">
                <template v-if="scope.row.is_upload_image == 0">-</template>
                <template v-else>
                  <a href="javascript:;" class="alink" @click="viewPic(scope.row.qc_id)">查看图片</a>
                </template>
              </template>
            </el-table-column>
            <el-table-column prop="" label="质检附件" width="100" align="center">
              <template slot-scope="scope">
                <template v-if="scope.row.is_upload_attachment == 0">-</template>
                <template v-else>
                  <a href="javascript:;" class="alink" @click="viewPic(scope.row.qc_id,2)">查看附件</a>
                </template>
              </template>
            </el-table-column>
            <el-table-column prop="qc_time" label="质检时间" width="150" align="center"></el-table-column>
          </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>
      <!--质检图片-->
      <el-dialog title="质检图片" :close-on-click-modal="false" :visible.sync="dialogVisible" width="700px" class="xx">
        <div class="pic-list row">
          <template v-for="v in qcImage">
            <el-image style="width: 100px; height: 100px" :src="v.small_image_url" :preview-src-list="v.big_image_url.split(',')"></el-image>
          </template>
        </div>
      </el-dialog>
      <!--质检附件-->
      <el-dialog title="质检附件" :close-on-click-modal="false" :visible.sync="dialogFileVisible" width="700px" class="xx">
        <div class="pic-list row">
          <template v-for="v in qcAttachments">
            <el-link type="primary" :href="v.file_url" target="_blank" style="margin-right: 5px">{{ v.file_name }}</el-link>
          </template>
        </div>
      </el-dialog>
    </div>
    <Menu/>
  </section>
</template>
<script>
import Vue from 'vue';
import Menu from "@/components/menu.vue";
import {Autocomplete, Button, DatePicker, Dialog, Form, FormItem, Image, Input, Link, Message, MessageBox, Option, Pagination, Select, Table, TableColumn, Tag} 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).use(Tag).use(DatePicker).use(Image).use(Link);
export default {
  name: "orderTrackInvoice",
  data() {
    return {
      total: 0,
      limit: 10,
      page: 1,
      dialogVisible: false,
      dialogFileVisible: false,
      logs: [],
      create_time_val: '',//创建时间
      qc_time_val: '',//质检时间
      qcImage: [],//质检图片
      qcAttachments: [],//质检附件
      purchase_id: '',//记录从已采购订单页面跳转来的
      stock_in_sn: '',//记录从退货单页面跳转来的
      formInline: {
        create_time: '',//创建时间
        qc_time: '',//质检时间
        stock_in_sn: "",
        goods_name: '',
        brand_name: '',
        status: ''
      },
      tableData: "",
      multipleSelection: [], //选择数据
    };
  },
  created() {
    this.purchase_id = this.$route.query.purchase_id || '';
    this.stock_in_sn = this.$route.query.stock_in_sn || '';
    this.getData();
  },
  watch: {
    $route(to, from) {
      if (to.path == '/orderTrackInvoice') {
        this.purchase_id = this.$route.query.purchase_id || '';
        this.stock_in_sn = this.$route.query.stock_in_sn || '';
        this.getData();
      }
    }
  },
  methods: {
    getData() {
      //格式化时间
      if (this.create_time_val) {
        this.formInline.create_time = this.create_time_val[0] + '~' + this.create_time_val[1];
      } else {
        this.formInline.create_time = '';
      }
      if (this.qc_time_val) {
        this.formInline.qc_time = this.qc_time_val[0] + '~' + this.qc_time_val[1];
      } else {
        this.formInline.qc_time = '';
      }
      var params = Object.assign({}, {page: this.page}, {limit: this.limit}, this.formInline, {purchase_id: this.purchase_id}, {stock_in_sn: this.stock_in_sn});
      this.$http('post', "/api/purchase/getDeliveryList", params).then(res => {
        if (res.code === 0) {
          this.tableData = res.data.list || [];
          this.total = Number(res.data.total) || 0;
        } else {
          this.$message(res.msg);
        }
      })
    },
    /**
     * 查看图片
     */
    viewPic(qc_id, type) {
      if (type == 2) {
        this.$http('POST', "/api/purchase/getQcAttachments", {qc_id: qc_id}).then(res => {
          if (res.code === 0) {
            this.dialogFileVisible = true;
            this.qcAttachments = res.data.list;
          } else {
            this.$message(res.msg);
          }
        })
      } else {
        this.$http('POST', "/api/purchase/getQcImages", {qc_id: qc_id}).then(res => {
          if (res.code === 0) {
            this.dialogVisible = true;
            this.qcImage = res.data.list;
          } 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.create_time_val = '';
      this.qc_time_val = '';
      this.$refs[formName].resetFields();
    },
    //取消发货
    cancelSend() {
      var self = this;
      if (this.multipleSelection.length <= 0) {
        this.$message({
          message: "请勾选数据进行操作",
          type: 'warning'
        });
        return false;
      }
      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 === 1)
        if (status) {
          MessageBox.confirm('确定取消发货吗?', {
            type: 'error'
          }).then(() => {
            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: res.msg,
                  type: 'error'
                });
              }
            })
          }).catch(() => {

          })
        } 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";

.pic-list {
  flex-wrap: wrap;
}

.pic-list .el-image {
  margin-right: 10px;
  margin-bottom: 10px;
}
</style>