<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page.sync="page"
               ref="crud"
               @row-del="rowDel"
               v-model="form"
               @row-update="rowUpdate"
               @row-save="rowSave"
               :before-open="beforeOpen"
               @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="warning"
                   size="small"
                   v-if="permission.chongzhi_stat && data.length > 0"
                   @click="handleStat">
          采购统计</el-button><span span style="color: red; font-weight:bold">{{chongZhiStat}}</span>
      </template>
      <template slot="menu" slot-scope="{row}">
        <el-button type="text" size="small" icon="el-icon-zoom-in" @click="purchaseDetail(row)">采购明细
        </el-button>
      </template>
      <template slot="payStatus" slot-scope="{row}">
        <el-tag plain size="small" type="info" v-if="row.payStatus === 0">待付款</el-tag>
        <el-tag plain size="small" type="success" v-if="row.payStatus === 1">付款成功</el-tag>
        <el-tag plain size="small" type="danger" v-if="row.payStatus === 3">付款失败</el-tag>
        <el-tag plain size="small" type="danger" v-if="row.payStatus === 4">系统异常</el-tag>
      </template>
    </avue-crud>
    <el-dialog :modal-append-to-body="false" :visible.sync="purchaseDetailVisible" title="采购明细页面" width="70%" center top="2vh" modal="true">
        <bussinesspurchaserecord :bill-record-id="billRecordId" :bussiness-id="bussinessId" v-if="purchaseDetailVisible"></bussinesspurchaserecord>
    </el-dialog>
  </basic-container>
</template>

<script>
  import {getBussinessList as getList, billStat} from "@/api/billrecord/billrecord";
  import {mapGetters} from "vuex";
  import bussinesspurchaserecord from "../bussiness/bussinesspurchaserecord.vue"

  export default {
    components: {
      bussinesspurchaserecord
    },
    data() {
      return {
        form: {},
        query: {},
        chongZhiStat: "",
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        billRecordId: null,
        bussinessId: null,
        purchaseDetailVisible: false,
        selectionList: [],
        option: {
          // showHeader:false,
          addBtn: false,
          menu: true,
          height: 'auto',
          calcHeight: 30,
          dialogWidth: 950,
          tip: false,
          searchShow: true,
          searchMenuSpan: 6,
          border: true,
          index: true,
          viewBtn: false,
          editBtn: false,
          delBtn: false,
          addBtn: false,
          selection: true,
          dialogClickModal: false,
          column: [
            {
              label: "采购商",
              prop: "userId",
              type: "select",
              dicUrl: "/api/cyzh-guosen/bussiness/list?size=500",
              dicFormatter:(res)=>{
                return res.data.records;
              },
              props:{
                label: "name",
                value: "id"
              },
              search: true,

            },
            {
              label: "采购金额",
              prop: "price",

            },
            {
              label: "采购状态",
              prop: "payStatus",
              type: "select",
              slot: true,
              search: true,
              dicData:[
                {
                  label:"待付款",
                  value:0
                },
                {
                  label:"付款成功",
                  value: 1
                },
                {
                  label:"付款失败",
                  value: 3
                },
                {
                  label:"系统异常",
                  value: 4
                },
              ]
            },
            {
              label: "描述",
              prop: "failReason",
            },
            {
              label: "时间",
              prop: "createTime",
              type: "date",
              search: true,
              valueFormat: "yyyy-MM-dd HH:mm:ss"
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {

        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      purchaseDetail(row){
        this.purchaseDetailVisible = true;
        this.billRecordId = row.id;
        this.bussinessId = row.userId;
      },
      handleStat(){
        billStat(this.query).then(res =>{
          this.chongZhiStat = "¥" + res.data.data;
        })
      },
      rowSave(row, done, loading) {
        add(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      searchReset() {
        this.chongZhiStat = "";
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.chongZhiStat = "";
        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();
      },
      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)) {
          getNotice(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      currentChange(currentPage) {
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize) {
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      onLoad(page, params = {}) {
        const releaseTimeRange = this.query;
        let values = {
          ...params,
        };
        if (releaseTimeRange) {
          values = {
            ...params,
            releaseTime_datege: releaseTimeRange[0],
            releaseTime_datelt: releaseTimeRange[1],
            ...this.query
          };
          values.releaseTimeRange = null;
        }
        this.loading = true;
        getList(page.currentPage, page.pageSize, values).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      }
    }
  };
</script>

<style>
</style>
