<template>
  <basic-container>
    <avue-crud :option="option"
               :data="dataList"
               ref="crud"
               v-model="form"
               :page.sync="page"
               :search.sync="search"
               :table-loading="loading"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @on-load="onLoad">
      <template slot="corpsIdSearch">
        <select-component
          v-model="search.corpsId"
          :configuration="configuration"
        ></select-component>
      </template>
      <template slot="menuLeft">
        <el-button size="small"
                   type="success"
                   :disabled="selectionList.length == 0 "
                   @click.stop="batchCheck"
        >批量审批
        </el-button>
        <el-button size="small"
                   type="info"
                   :disabled="true"
                   @click.stop=""
        >报表
        </el-button>
      </template>
      <template slot-scope="scope" slot="menu">
        <el-button
          type="text"
          size="small"
          :disabled="scope.row.operate"
          @click.stop="jumpPage(scope.row)"
        >查看
        </el-button>
        <el-button
          type="text"
          size="small"
          :disabled="scope.row.auditStatus != 'S'"
          @click.stop="openCheck(scope.row)"
        >审核
        </el-button>
      </template>
    </avue-crud>
    <el-dialog
      append-to-body
      title="审批"
      class="el-dialogDeep"
      :visible.sync="checkDialog"
      width="50%"
      :close-on-click-modal="false"
      :destroy-on-close="true"
      :close-on-press-escape="false"
      v-dialog-drag
    >
      <check
        :checkData="checkData"
        @choceCheckFun="choceCheckFun"
      >
      </check>
    </el-dialog>
  </basic-container>
</template>

<script>
  import option from "./configuration/mainList.json";
  import { getList,approvePass } from "@/api/approveData/main";
  import { batchOperation } from "@/api/approveData/main"
  import check from "@/components/check/check";
  import _ from "lodash";

  export default {
    components:{
      check
    },
    data() {
      return {
        loading : false,
        form: {},
        search:{},
        show:true,
        checkDialog:false,
        detailData:{},
        option: option,
        parentId:0,
        checkData:{},
        dataList: [],
        selectionList:[],
        page: {
          pageSize: 10,
          pagerCount: 5,
          total: 0,
        },
        query:{},
        configuration:{
          multipleChoices:false,
          multiple:false,
          disabled:false,
          searchShow:true,
          collapseTags:false,
          clearable:true,
          placeholder:'请点击右边按钮选择',
          dicData:[]
        },
      }
    },
    created() {

    },
    mounted() {
      // option.height = window.innerHeight - 200 ;
    },
    methods: {
      batchCheck(){
        let idList = [];
        for(let i=0;i<this.selectionList.length;i++){
          if(this.selectionList[i].auditStatus != "S"){
            return this.$message.error("审核状态必须都为待审核状态!")
          }else{
            idList.push(this.selectionList[i].id)
          }
        }

        this.$confirm('确定进行批量操作吗?', '提示', {
          confirmButtonText: '审核通过',
          cancelButtonText: '取消',
          type: 'warning',
          distinguishCancelAndClose : false
        }).then(() => {
          //批量通过
          batchOperation(idList.join(","),"1").then(res=>{
            if(res.data.success){
              this.$message.success("操作成功!")
              this.refreshChange();
            }
          })
        })
      },
      //跳转页面
      jumpPage(row){
        if(row.url){
            this.$router.$avueRouter.closeTag(row.url);
            this.$router.push({
              path: row.url,
              query: {check : row},
            });
        }
      },
      //审批通过
      pass(row,operate){
        row.operate =  operate
        this.loading = true;
        approvePass(row).then(res=>{
          this.$message.success("操作成功!")
          this.refreshChange()
        }).finally(()=>{
          this.loading = false
        })
      },
      openCheck(row){
        this.checkDialog = true
        this.checkData = row
      },
      choceCheckFun(){
        this.checkDialog = false;
        this.refreshChange()
      },
      //新单打开
      addReceipt(row){

      },
      //编辑打开
      editOpen(row, status){
        this.detailData = {
          id: row.id,
          status: status
        };
        this.show = false;
      },
      rowDel(row, index, done) {
        if(row.id){
          this.$confirm("确定将选择数据删除?", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(() => {
            remove(row.id).then(res =>{
              if(res.data.success){
                this.$message.success("操作成功!");
                this.onLoad(this.page);
              }
            })
          });
        }
      },
      //点击搜索按钮触发
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done()
      },
      searchReset() {
        console.log('1')
      },
      selectionChange(list) {
        this.selectionList = list
      },
      currentChange() {
        console.log('1')
      },
      sizeChange() {
        console.log('1')
      },
      refreshChange() {
        this.onLoad(this.page);
      },
      onLoad(page, params = {}) {
        this.loading = true;
        if(!params.auditStatus && this.search.auditStatus != ""){
          params.auditStatus = "S";
        }
        getList(page.currentPage, page.pageSize,params).then(res=>{
          this.dataList = res.data.data.records
          this.page.total = res.data.data.total
          this.loading = false;
        })
      },
      goBack() {
        this.detailData=this.$options.data().detailData
        this.show = true;
      }
    }
  }
</script>

<style scoped>

</style>
