<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.auctiongoods_delete"
                   @click="handleDelete">删 除
        </el-button>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove} from "@/api/auction/auctiongoods";
  import {mapGetters} from "vuex";

  export default {
    data() {
      return {
        form: {},
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          height:'auto',
          calcHeight: 30,
          tip: false,
          searchShow: false,
          searchMenuSpan: 6,
          border: true,
          index: true,
          viewBtn: true,
          selection: true,
          dialogClickModal: false,
          column: [
            {
              label: "标题",
              prop: "title",
              rules: [{
                required: true,
                message: "请输入标题",
                trigger: "blur"
              }]
            },
            {
              label: "图片集合",
              prop: "images",
              rules: [{
                required: true,
                message: "请输入图片集合",
                trigger: "blur"
              }]
            },

            {
              label: "拍卖机构名称",
              prop: "agencyName",
              rules: [{
                required: true,
                message: "请输入拍卖机构名称",
                trigger: "blur"
              }]
            },
            {
              label: "机构类型",
              prop: "agencyType",
              rules: [{
                required: true,
                message: "请输入拍卖机构类型",
                trigger: "blur"
              }]
            },
            {
              label: "拍卖开始时间",
              prop: "auctionStartTime",
              rules: [{
                required: true,
                message: "请输入拍卖开始时间",
                trigger: "blur"
              }]
            },
            {
              label: "拍卖结束时间",
              prop: "auctionEndTime",
              rules: [{
                required: true,
                message: "请输入拍卖结束时间",
                trigger: "blur"
              }]
            },
            {
              label: "拍卖周期",
              prop: "auctionPeriod",
              rules: [{
                required: true,
                message: "请输入拍卖周期",
                trigger: "blur"
              }]
            },
            {
              label: "拍卖周期单位",
              prop: "auctionPeriodUnit",
              rules: [{
                required: true,
                message: "请输入拍卖周期单位",
                trigger: "blur"
              }]
            },
            {
              label: "延时周期",
              prop: "delayPeriod",
              rules: [{
                required: true,
                message: "请输入延时周期",
                trigger: "blur"
              }]
            },
            {
              label: "延时周期单位",
              prop: "delayPeriodUnit",
              rules: [{
                required: true,
                message: "请输入延时周期单位",
                trigger: "blur"
              }]
            },
            {
              label: "保证金",
              prop: "earnestPrice",
              rules: [{
                required: true,
                message: "请输入保证金",
                trigger: "blur"
              }]
            },
            {
              label: "竞拍预缴款",
              prop: "advancePrice",
              rules: [{
                required: true,
                message: "请输入竞拍预缴款",
                trigger: "blur"
              }]
            },
            {
              label: "加价幅度",
              prop: "markupPrice",
              rules: [{
                required: true,
                message: "请输入加价幅度",
                trigger: "blur"
              }]
            },
            {
              label: "起拍价",
              prop: "startingPrice",
              rules: [{
                required: true,
                message: "请输入起拍价",
                trigger: "blur"
              }]
            },
            {
              label: "评估价",
              prop: "valuationsPrice",
              rules: [{
                required: true,
                message: "请输入评估价",
                trigger: "blur"
              }]
            },
            {
              label: "落槌价",
              prop: "hammerPrice",
              addDisplay: false,
              editDisplay: false,
              rules: [{
                required: false,
                message: "请输入落槌价",
                trigger: "blur"
              }]
            },
            {
              label: "当前出价",
              prop: "presentPrice",
              addDisplay: false,
              editDisplay: false,
              rules: [{
                required: false,
                message: "请输入当前出价",
                trigger: "blur"
              }]
            },
            {
              label: "规则说明",
              prop: "ruleMsg",
              rules: [{
                required: false,
                message: "请输入竞拍规则类型说明",
                trigger: "blur"
              }]
            },
            {
              label: "拍卖品介绍",
              prop: "auctionPresenta",
              rules: [{
                required: true,
                message: "请输入拍卖品介绍",
                trigger: "blur"
              }]
            },
            {
              label: "拍卖公告",
              prop: "auctionAffiche",
              rules: [{
                required: false,
                message: "请输入拍卖公告",
                trigger: "blur"
              }]
            },
            {
              label: "竞拍须知",
              prop: "auctionNotice",
              rules: [{
                required: false,
                message: "请输入竞拍须知",
                trigger: "blur"
              }]
            },
            {
              label: "状态",
              prop: "status",
              addDisplay: false,
              editDisplay: false,
              rules: [{
                required: false,
                message: "请输入状态，0：未开始，1：流拍，2：已成交",
                trigger: "blur"
              }]
            },
            {
              label: "报名人数",
              prop: "applyCount",
              addDisplay: false,
              editDisplay: false,
              rules: [{
                required: false,
                message: "请输入报名人数",
                trigger: "blur"
              }]
            },
            {
              label: "设置提醒人数",
              prop: "remindCount",
              addDisplay: false,
              editDisplay: false,
              rules: [{
                required: false,
                message: "请输入设置提醒人数",
                trigger: "blur"
              }]
            },
            {
              label: "围观人次数",
              prop: "onlookerCount",
              addDisplay: false,
              editDisplay: false,
              rules: [{
                required: false,
                message: "请输入围观人次数",
                trigger: "blur"
              }]
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.auctiongoods_add, false),
          viewBtn: this.vaildData(this.permission.auctiongoods_view, false),
          delBtn: this.vaildData(this.permission.auctiongoods_delete, false),
          editBtn: this.vaildData(this.permission.auctiongoods_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 => {
            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;
        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();
        });
      }
    }
  };
</script>

<style>
</style>
