<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="info"
                   size="small"
                   plain
                   icon="el-icon-setting"
                   @click="handlePlatform">选择用户
        </el-button>
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   v-if="permission.orderconsign_delete"
                   @click="handleDelete">删 除
        </el-button>
      </template>
    </avue-crud>
    <el-dialog title="用户列表"
               append-to-body
               :visible.sync="platformBox">
      <avue-crud :option="platformOption"
                 :table-loading="platformLoading"
                 :data="platformData"
                 ref="platformCrud"
                 v-model="platformForm"
                 :before-open="platformBeforeOpen"
                 :page.sync="platformPage"
                 :permission="platformPermissionList"
                 @search-change="platformSearchChange"
                 @search-reset="platformSearchReset"
                 @selection-change="platformSelectionChange"
                 @current-change="platformCurrentChange"
                 @size-change="platformSizeChange"
                 @refresh-change="platformRefreshChange"
                 @on-load="platformOnLoad">
        <template slot="menuLeft">
        <el-button type="primary"
                   size="small"
                   icon="el-icon-add"
                   :loading="butLoading"
                   @click="handleRemit">打款
        </el-button>
        </template>
      </avue-crud>

    </el-dialog>
  </basic-container>
</template>

<script>
import {getList, getDetail, add, update, remove, getUserAppList, payment} from "@/api/finance/orderconsign";
  import {mapGetters} from "vuex";

  export default {
    data() {
      return {
        form: {},
        query: {},
        loading: true,
        butLoading: false,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        platformBox: false,
        platformLoading: false,
        platformPage: {
          pageSize: 5,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          height:'auto',
          calcHeight: 30,
          tip: false,
          searchShow: true,
          searchMenuSpan: 6,
          border: true,
          index: true,
          viewBtn: true,
          selection: true,
          dialogClickModal: false,
          selectable:(row,index)=>{
            return row.isRemittance !== 1
          },
          column: [
            {
              label: "主键ID",
              prop: "id",
              hide: true,
              display: false,
            },
            {
              label: "订单ID",
              prop: "orderId",
              hide: true,
              display: false,
            },
            {
              label: "商品ID",
              prop: "goodsId",
              hide: true,
              display: false,
            },
            {
              label: "商品编号",
              prop: "productNo",
              search: true,
              width: 110,
            },
            {
              label: "商品名称",
              prop: "goodsName",
              overHidden: true,
            },
            {
              label: "支付价格",
              prop: "totalPrice",
            },
            {
              label: "寄售用户ID",
              prop: "consignUserId",
              hide: true,
              display: false,
            },
            {
              label: "支付用户ID",
              prop: "payUserId",
              hide: true,
              display: false,
            },
            {
              label: "寄售用户",
              prop: "consignRealName",
              display: false,
              width: 120,
            },
            {
              label: "支付用户",
              prop: "payRealName",
              display: false,
              width: 120,
            },
            {
              label: "打款状态",
              prop: "isConfirmDisplayName",
              display: false,
            },
            {
              label: "寄售类型",
              prop: "consignTypeDisplayName",
              display: false,
            },
            {
              label: "抢购价",
              prop: "consignPrice",
            },
            {
              label: "是否打款",
              prop: "isRemittanceDisplayName",
            },
            {
              label: "寄售天数",
              prop: "consignDays",
              width: 70,
            },
            {
              label: "数量",
              prop: "totalNum",
              width: 70,
            },
            {
              label: "寄售类型",
              prop: "consignType",
              type: "select",
              dicUrl: "/api/blade-system/dict-biz/dictionary?code=consign_type",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              dataType: "number",
              hide: true,
              rules: [{
                required: true,
                message: "请选择寄售类型",
                trigger: "blur"
              }]
            },
            {
              label: "寄售类型",
              prop: "isConfirm",
              type: "select",
              dicUrl: "/api/blade-system/dict-biz/dictionary?code=is_confirm",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              dataType: "number",
              hide: true,
              rules: [{
                required: true,
                message: "请选择寄售类型",
                trigger: "blur"
              }]
            },
            {
              label: "打款时间",
              prop: "confirmTime",
              width: 140,
              rules: [{
                required: true,
                message: "请输入确认时间",
                trigger: "blur"
              }]
            },
            {
              label: "进货时间",
              prop: "createTime",
              width: 140,
            },
          ]
        },
        data: [],
        platformQuery: {},
        platformSelectionList: [],
        platformData: [],
        platformForm: {},
        platformOption: {
          tip: false,
          searchShow: true,
          searchMenuSpan: 6,
          border: true,
          index: true,
          selection: true,
          viewBtn: true,
          dialogClickModal: false,
          menuWidth: 120,
          editBtnText: '配置',
          column: [
            {
              label: "用户姓名",
              prop: "realName",
              search: true,
              display: false
            },
            {
              label: "手机号",
              prop: "phone",
              search: true,
              display: false
            },
            {
              label: "特殊权限",
              prop: "enableAuthDisplayName",
              addDisplay: false,
              editDisplay: false,
            },
            {
              label: "特殊权限",
              prop: "enableAuth",
              type: "select",
              dicUrl: "/api/blade-system/dict/dictionary?code=yes_no",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              dataType: "number",
              hide: true,
              search: true,
              addDisplay: false,
              rules: [{
                required: true,
                message: "请选择特殊权限",
                trigger: "blur"
              }]
            },
          ],
        },
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.orderconsign_add, false),
          viewBtn: this.vaildData(this.permission.orderconsign_view, false),
          delBtn: this.vaildData(this.permission.orderconsign_delete, false),
          editBtn: this.vaildData(this.permission.orderconsign_edit, false)
        };
      },
      platformPermissionList() {
        return {
          addBtn: false,
          viewBtn: false,
          delBtn: false,
          editBtn: 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: "操作成功!"
            });
          });
      },
      handlePlatform() {
        this.platformBox = true;
      },
      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 => {
            this.form = res.data.data;
          });
        }
        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;
        params.consignUserId = 0;//平台单
        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();
        });
      },
      platformSearchReset() {
        this.platformQuery = {};
        this.platformOnLoad(this.platformPage);
      },
      platformSearchChange(params, done) {
        this.platformQuery = params;
        this.platformPage.currentPage = 1;
        this.platformOnLoad(this.platformPage, params);
        done();
      },
      platformSelectionChange(list) {
        this.platformSelectionList = list;
      },
      platformSelectionClear() {
        this.platformSelectionList = [];
        this.$refs.platformCrud.toggleSelection();
      },
      platformCurrentChange(currentPage) {
        this.platformPage.currentPage = currentPage;
      },
      platformSizeChange(pageSize) {
        this.platformPage.pageSize = pageSize;
      },
      platformRefreshChange() {
        this.platformOnLoad(this.platformPage, this.platformQuery);
      },
      platformOnLoad(page, params = {}) {
        this.platformLoading = true;
        getUserAppList(page.currentPage, page.pageSize, Object.assign(params, this.query), this.treeDeptId).then(res => {
          const data = res.data.data;
          this.platformPage.total = data.total;
          this.platformData = data.records;
          this.platformLoading = false;
          this.platformSelectionClear();
        });
      },
      handleRemit() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请至少选择一条订单数据");
          return;
        }
        if (this.platformSelectionList.length === 0) {
          this.$message.warning("请选择一个用户");
          return;
        }
        if (this.platformSelectionList.length > 1) {
          this.$message.warning("只能选择一个用户");
          return;
        }
        let orderConsignIds = [];
        this.selectionList.forEach(ele => {
          orderConsignIds.push(ele.id);
        });
        let param = {"orderConsignIds": orderConsignIds, "paymentUserId": this.platformSelectionList[0].userId};
        this.butLoading = true;
        const loading = this.$loading({
          lock: true,
          text: '打款中，请稍等...',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        });
        payment(param).then(res => {
          this.$message.success("打款成功");
          loading.close();
          this.butLoading = false;
          this.platformBox = false;
          this.platformSelectionList = [];
          this.platformSelectionClear();
          this.onLoad(this.page, this.query);
        })
      }
    }
  };
</script>

<style>
</style>
