<template>
  <basic-container>
    <avue-crud
        ref="crud"
        :option="option"
        :data="data"
        :page.sync="page"
        @search-change="searchChange"
        :search.sync="search"
        @on-load="onLoad"
        @row-update="rowUpdate">
      <template slot-scope="{row,index}" slot="menu">
        <el-button
            type="text"
            size="small"
            @click="rowCell(row,index)"
        >{{ row.$cellEdit ? '保存' : '修改' }}
        </el-button>
        <el-button
            type="text"
            size="small"
            @click="deletePrice(row,index)"
        >删除
        </el-button>
      </template>
      <template slot="menuLeft">
        <el-button
            type="primary"
            size="small"
            icon="el-icon-plus"
            @click="excelBox = !excelBox"
        >导入
        </el-button>
        <el-button
            type="success"
            icon="el-icon-upload2"
            size="small"
            @click="derivation()"
        >下载模板
        </el-button>
        <el-button
            type="info"
            icon="el-icon-printer"
            size="small"
        >报 表
        </el-button>
      </template>
    </avue-crud>
    <el-dialog title="导入价格"
               append-to-body
               :visible.sync="excelBox"
               width="555px">
      <avue-form :option="excelOption" v-model="excelForm" :upload-after="uploadAfter"/>
    </el-dialog>
  </basic-container>
</template>
<script>
import option from "./configuration/mainList.json"
import {deleteTemplate,customerList,typeSave} from "@/api/basicData/inventoryAccount"
import {getToken} from "@/util/auth";
import { defaultDate } from "@/util/date";

export default {
  data() {
    return {
      data: [],
      search:{},
      excelForm: {},
      excelOption: {
        submitBtn: false,
        emptyBtn: false,
        column: [
          {
            label: '导入数据',
            prop: 'excelFile',
            type: 'upload',
            drag: true,
            loadText: '导入数据中，请稍等',
            span: 24,
            propsHttp: {
              res: 'data'
            },
            tip: '请上传 .xls,.xlsx 标准格式文件',
            action: "/api/blade-stock/stockgoods/import-price"
          }
        ]
      },
      excelBox: false,
      option: option,
      page: {
        pageSize: 10,
        pagerCount: 5,
        total: 0,
      }
    }
  },
  created() {
    this.search.createTime = defaultDate(1)
    console.log(this.search)
    let i = 0;
    this.option.column.forEach(item => {
      if (item.search) i++
    })
    if (i % 3 !== 0){
      const num = 3 - Number(i % 3)
      this.option.searchMenuSpan = num * 8;
      this.option.searchMenuPosition = "right";
    }
  },
  methods: {
    derivation() {
      this.$confirm("是否下载模板?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        window.open(`/api/blade-stock/stockgoods/export-template?${this.website.tokenHeader}=${getToken()}`);
      });
    },
    uploadAfter(res, done, loading, column) {
      window.console.log(column);
      this.excelBox = false;
      this.page.currentPage = 1;
      this.onLoad(this.page);
      loading()
      done();
    },
    rowCell(row, index) {
      this.$refs.crud.rowCell(row, index)
    },
    deletePrice(row,index){
      if (row.id){
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          return deleteTemplate(row.id);
        }).then(() => {
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          this.page.currentPage = 1;
          this.onLoad(this.page);
        })
      }
    },
    //点击搜索按钮触发
    searchChange(params, done) {
      if (params.createTime) {
        params.createStartTime = params.createTime[0]+ " " + "00:00:00"
        params.createEndTime = params.createTime[1]+ " " + "23:59:59"
        delete params.createTime;
      }
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done()
    },
    onLoad(page, params = {}) {
      if (this.search.createTime && this.search.createTime.length > 0) {
        params = {
          ...params,
          createStartTime: this.search.createTime[0]+ " " + "00:00:00",
          createEndTime: this.search.createTime[1]+ " " + "23:59:59",
        }
        delete params.createTime;
      }
      let queryParams = Object.assign({}, params, {
        size: page.pageSize,
        current: page.currentPage,
      })
      customerList(queryParams).then(res => {
        this.data = res.data.data.records
        this.page.total = res.data.data.total
      })
    },
    rowUpdate(form, index, done) {
      console.log(form)
      typeSave(form).then(res => {
        this.$message({type: "success", message: form.id ? "修改成功!" : "新增成功!"});
        // this.page.currentPage = 1;
        // this.onLoad(this.page);
        //成功关闭此页面回到列表页
        // this.backToList()
      })
      done()
    }
  }
}
</script>
