<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page.sync="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @on-load="onLoad">
      <template slot="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   v-if="permission.order_delete"
                   @click="handleDelete">删 除
        </el-button>
        <el-button type="primary"
                   size="small"
                   icon="el-icon-plus"
                   v-if="permission.order_add"
                   @click="handleAdd">新 增
        </el-button>
      </template>

      <template slot-scope="scope" slot="menu">
        <el-button v-if="scope.row.orderState == 2"
                   type="text"
                   size="small"
                   @click="handleSend(scope.row.id)"
        >发货</el-button>
      </template>
    </avue-crud>

    <el-dialog title="发货" @close="refreshChange"
               append-to-body
               :visible.sync="bindBox"
               :before-close="handleClose"
               width="500px"
               heigh="40%">
      <el-form :model="form" label-width="80px">
        <el-form-item label="物流公司:" placeholder="请选择物流公司">
          <el-select v-model="wlCompany" filterable placeholder="请选择物流公司">
            <el-option
                v-for="item in WL_COMPANY"
                :key="item.value"
                :label="item.label"
                :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="物流单号:">
          <el-input v-model="wlNo" autocomplete="off" placeholder="请输入物流单号"></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="deliverGoods()">确 定</el-button>
      </div>
    </el-dialog>


    <el-dialog
         title="新增" @close="refreshChange"
         append-to-body
         :visible.sync="addBox"
         :before-close="handleClose"
         width="60%">
      <avue-form :option="orderOption" v-model="orderForm"  @submit="orderSubmit" ></avue-form>
    </el-dialog>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove, deliverGoods} from "@/api/finance/order";
  import {orderOption} from "@/option/finance/order";
  import {mapGetters} from "vuex";

  export default {
    data() {
      return {
        orderForm: {},
        orderOption: orderOption,
        addBox: false,
        form: {},
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          height:'auto',
          calcHeight: 30,
          tip: false,
          searchShow: true,
          searchMenuSpan: 6,
          searchIcon: true,
          searchIndex: 3,
          menuWidth: 150,
          border: true,
          addBtn: false,
          viewBtn: true,
          editBtn: false,
          selection: true,
          dialogClickModal: false,
          group: [
            {
              label: '订单信息',
              prop: 'orderInfo',
              column: [
                {
                  label: "订单号",
                  prop: "orderNo",
                },
                {
                  label: "下单时间",
                  prop: "createTime",
                },
                {
                  label: "订单类型",
                  prop: "orderTypeDesc",
                },
                {
                  label: "数量",
                  prop: "quantity",
                },
                {
                  label: "订单状态",
                  prop: "orderStateDesc",
                },
                {
                  label: "订单金额",
                  prop: "totalAmount",
                },
              ]
            }, {
              label: '支付信息',
              prop: 'payInfo',
              column: [
                {
                  label: "支付金额",
                  prop: "paymentAmount"
                },
                {
                  label: "支付方式",
                  prop: "paymentModeDesc",
                },
                {
                  label: "支付时间",
                  prop: "paymentTime",
                },
                {
                  label: "支付状态",
                  prop: "paymentStateDesc",
                },
                {
                  label: "优惠金额",
                  prop: "discountAmount",
                },
                {
                  label: "邮费",
                  prop: "postage",
                },
              ]
            }, {
              label: '物流信息',
              prop: 'wuliuInfo',
              column: [
                {
                  label: "收货地址",
                  span: 24,
                  prop: "addressDetail",
                },
                {
                  label: "配送方式",
                  prop: "distributeMode",
                },
                {
                  label: "收货人",
                  prop: "consignee",
                },
                {
                  label: "收货人电话",
                  labelWidth: 100,
                  prop: "consigneePhoneNumber",
                },
                {
                  label: "发货时间",
                  prop: "sendTime",
                },
                {
                  label: "收货时间",
                  prop: "recordTime",
                },
              ]
            }
          ],
          column: [
            {
              label: "订单号",
              prop: "orderNo",
              search: true,
              viewDisplay: false,
              width: 180
            },
            {
              label: "商品图片",
              prop: "image",
              type: 'upload',
              listType: 'picture-img',
              viewDisplay: false,
              width: 90,
            },
            {
              label: "订单类型",
              prop: "orderType",
              viewDisplay: false,
              type: "select",
              search: true,
              hide: true,
              dicUrl: "/api/blade-system/dict-biz/dictionary?code=goods_order_type",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              dataType: "number",
            },
            {
              label: "订单类型",
              viewDisplay: false,
              prop: "orderTypeDesc",
            },
            {
              label: "订单状态",
              viewDisplay: false,
              prop: "orderState",
              type: "select",
              search: true,
              hide: true,
              dicUrl: "/api/blade-system/dict-biz/dictionary?code=order_state",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              dataType: "number",
            },
            {
              label: "订单状态",
              prop: "orderStateDesc",
              viewDisplay: false,
            },
            {
              label: "订单金额",
              prop: "totalAmount",
              viewDisplay: false,
            },
            {
              label: "支付金额",
              viewDisplay: false,
              prop: "paymentAmount"
            },
            {
              label: "收货人",
              prop: "consignee",
              viewDisplay: false,
              search: true
            },
            {
              label: "下单时间",
              prop: "createTime",
              viewDisplay: false,
              width: 150,
            },
          ]
        },
        data: [],
        WL_COMPANY: [{
          label: '圆通速递',
          value: 'yuantong'
        }, {
          label: '韵达快递',
          value: 'yunda'
        },{
          label: '申通快递',
          value: 'shentong'
        },{
          label: '中通快递',
          value: 'zhongtong'
        },{
          label: '极兔速递',
          value: 'jtexpress'
        },{
          label: '邮政快递包裹',
          value: 'youzhengguonei'
        },{
          label: '顺丰速运',
          value: 'shunfeng'
        },{
          label: '京东物流',
          value: 'jd'
        },{
          label: '百世快递',
          value: 'huitongkuaidi'
        },{
          label: '德邦快递',
          value: 'debangkuaidi'
        },{
          label: '邮政标准快递',
          value: 'youzhengbk'
        }],
        activities: [{
          content: '活动按期开始',
          timestamp: '2018-04-15'
        }, {
          content: '通过审核',
          timestamp: '2018-04-13'
        }, {
          content: '创建成功',
          timestamp: '2018-04-11'
        }],
        wlCompany: '',
        wlNo: '',
        viewDetail: false,
        bindBox: false,
        orderId: 0,
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.order_add, false),
          viewBtn: this.vaildData(this.permission.order_view, false),
          delBtn: this.vaildData(this.permission.order_delete, false),
          editBtn: this.vaildData(this.permission.order_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      rowSave(row, done, loading) {
        add(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          console.log(error);
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            let orderDetail = res.data.data.orderDetail;
            this.form = res.data.data;
            this.form.addressDetail = orderDetail.province + orderDetail.city + orderDetail.district + orderDetail.street + orderDetail.address;
            console.log(this.form.addressDetail)
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      },
      handleSend(id) {
        this.bindBox = true;
        this.orderId = id;
      },
      handleClose() {
        this.bindBox = false;
        this.addBox = false;
        this.wlCompany = '';
        this.wlNo = '';
      },
      deliverGoods() {
        deliverGoods({
          orderId: this.orderId,
          wlCompany: this.wlCompany,
          wlNo: this.wlNo
        }).then(res => {
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          this.onLoad(this.page, this.query);
          this.handleClose();
        })
      },
      handleAdd() {
        this.addBox = true;
      },
      orderSubmit(form,done) {
        form.orderType = 1;
        add(form).then(res => {
          this.addBox = false;
          done()
        }).catch(e => {
          done()
        })
      }
    }
  };
</script>

<style>
</style>
