<template>
  <div>
    <el-form :inline="true" :model="query" class="query-form" size="mini">
      <el-form-item>
        <el-button-group>
          <el-button type="primary" icon="el-icon-refresh" @click="onReset">刷新</el-button>
          <el-button type="primary" icon="search" @click="deleteDialog">合并单号</el-button>
        </el-button-group>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="list"
      style="width: 100%;"
      ref="multipleTable"
      tooltip-effect="dark"
      @selection-change="handleSelectionChange"
      @row-click="intentDetail"
    >
      <el-table-column type="selection" width="55"></el-table-column>
      <!-- <el-table-column label="ID" prop="id" fixed></el-table-column> -->
            <el-table-column label="序号" type="index" fixed></el-table-column>
      <el-table-column label="业务类型" prop="action" fixed>
        <template slot-scope="scope">
          <span>{{ geActionName(scope.row.action) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单号" prop="corpOrderId" fixed></el-table-column>
      <el-table-column label="往来单位" prop="fromCorp" with="300" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column label="创建时间" prop="actDate">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span>{{ scope.row.actDate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作员" prop="actor" fixed></el-table-column>
      <el-table-column label="操作" fixed="right">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click.native.stop="handleErrorDetail(scope.$index)"
          >详情</el-button>
          <el-button type="text" size="small" @click.native.stop="exportXml(scope.$index)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="错误信息" :visible.sync="dialogVisible"
               :close-on-click-modal="false"
               :close-on-press-escape="false"
               width="30%"
               :before-close="handleClose">
      <span>{{errorDetail}}</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>

    <el-pagination
      :page-size="query.limit"
      @current-change="handleCurrentChange"
      layout="prev, pager, next"
      :total="total"
    ></el-pagination>
  </div>
</template>

<script>
import {
    orderListRepeat,
    codeList,
    mergeOrder
} from "../../api/inout/order";
import draggable from "vuedraggable";
import {getBussinessType} from "../../api/basic/bussinessType";
import { saveAs } from "file-saver";
const formJson = {
    site_id: "",
    site_name: "",
    describe: "",
    ads: []
};

export default {
    data() {
        return {
            query: {
                page: 1,
                limit: 20
            },
            list: [],
            adId: [],
            busTypes: [],
            adSelectList: [],
            multipleSelection: [],
            adList: {},
            adListNoDataText: "无数据",
            queryAdIdAsyncLoading: false,
            total: 0,
            loading: true,
            index: null,
            formName: null,
            errorDetail: "1111111111111",
            formMap: {
                add: "新增",
                edit: "编辑"
            },
            formLoading: false,
            dialogVisible: false,
            formData: formJson,
            deleteLoading: false
        };
    },
    components: {
        draggable
    },
    methods: {
        onReset() {
            this.$router.push({
                path: ""
            });
            this.query = {
                site_id: "",
                page: 1,
                limit: 20
            };
            this.getList();
        },
        onSubmit() {
            this.$router.push({
                path: "",
                query: this.query
            });
            this.getList();
        },
        handleSizeChange(val) {
            this.query.limit = val;
            this.getList();
        },
        handleCurrentChange(val) {
            this.query.page = val;
            this.getList();
        },
        deleteDialog() {
            this.$confirm("是否合并单号?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            })
                .then(() => {
                    this.mergeOrder();
                })
                .catch(() => {});
        },
        mergeOrder() {
            var repeatData = this.multipleSelection;
            var orderid = repeatData[0].corpOrderId;
            var orderList = [];
            repeatData.forEach((obj, index) => {
                if (orderid != obj.corpOrderId) {
                    this.$message({
                        type: "warning",
                        message: "所选单号不一致"
                    });

                    return;
                } else {
                    orderList.push(obj.id);
                }
            });
            var tquery = {
                orderList: orderList
            };
            mergeOrder(tquery).then(response => {
                this.getList();
                this.$message({
                    type: "success",
                    message: "合并成功"
                });
            });
        },

        handleSelectionChange(val) {
            console.log(val);
            this.multipleSelection = val;
        },

        //获取订单列表
        getList() {
            this.loading = true;
            orderListRepeat(this.query)
                .then(response => {
                    this.loading = false;
                    this.list = response.data.list || [];
                    this.total = response.data.total || 0;
                })
                .catch(() => {
                    this.loading = false;
                    this.list = [];
                    this.total = 0;
                });
        },
        getBusType() {
            let query = {
                enabled: true,
            };
            getBussinessType(query)
                .then((response) => {
                    this.busTypes = response.data.list || [];
                })
                .catch(() => {
                });
        },
        geActionName(action) {
            for (let i = 0; i < this.busTypes.length; i++) {
                if (this.busTypes[i].action === action) {
                    return this.busTypes[i].name;
                }
            }
        },

        handleErrorDetail(poistion) {
            var mOrder = this.list[poistion];
            var corpOrderId = mOrder.remark;

            this.errorDetail = mOrder.remark;
            console.log(corpOrderId + "  " + mOrder + "  " + mOrder.remark);
            this.dialogVisible = true;
        },

        intentDetail(row) {
            this.$router.push({
                path: "../inout/detail",
                query: { id: row.id }
            });
        }
    },
    filters: {},
    mounted() {
        document.body.ondrop = function(event) {
            event.preventDefault();
            event.stopPropagation();
        };
    },
    created() {
        // 将参数拷贝进查询对象
        let query = this.$route.query;
        this.query = Object.assign(this.query, query);
        this.query.limit = parseInt(this.query.limit);
        // 加载表格数据
        this.getList();
    }
};
</script>

<style type="text/scss" lang="scss">
</style>
