<template>
  <div>
    <Drawer title="搜索" v-model="startSearch" width="1024" :mask-closable="false" :styles="styles">
      <Forms
        ref="searchform"
        v-model="searchModel"
        isSearch
        :bizCode="bizCode"
        isAuto
        @on-action="doexec"
      ></Forms>
    </Drawer>
    <template>
      <template v-for="action in insideListButtonInfo">
        <span   v-show="!isShowForm" :key="action.key+'div'">
          <template v-if="action.key=='delete'">
            <Poptip
              :key="action.key+'p'"
              placement="bottom"
              confirm
              title="您真的要删除当前数据行吗?"
              @on-ok="doexec(action.key,null)"
            >
              <Button :key="action.key"  type="text" >
                <icon :type="action.icon"/>
                {{action.title}}
              </Button>
            </Poptip>
          </template>
          <template v-else>
            <Button :key="action.key" type="text" @click="doexec(action.key,null)">
              <icon :type="action.icon"/>
              {{action.title}}
            </Button>
          </template>
        </span>
      </template>
    </template>
    <Tables
      v-show="!isShowForm"
      :isMultiSelect="isMultiSelect"
      highlightRow
      ref="tables"
      editable
      v-model="tableData"
      :rowtotal="rowtotal"
      :columns="cols"
      @on-search-data="searchData"
      @on-delete="handleDelete"
      @onSelectionChange="setCurrentSels"
    >
      <template slot="actions" slot-scope="{row,column,index}">
        <template v-for="action in insideRowButtonInfo">
          <template v-if="action.key=='delete'">
            <Poptip
              :key="action.key+'p'"
              :placement="(index+1)%10==0   ?'top':'left-start'"
              style="z-index:1"
              confirm
              title="您真的要删除当前数据行吗?"
              @on-ok="doexec(action.key,row)"
            >
              <Button :key="action.key" type="text">
                <icon :type="action.icon"/>
                {{action.title}}
              </Button>
            </Poptip>
          </template>
          <template v-else>
            <Button :key="action.key" type="text" @click="doexec(action.key,row)">
              <icon :type="action.icon"/>
              {{action.title}}
            </Button>
          </template>
        </template>
      </template>
    </Tables>
    <Forms 
      ref="editform"
      v-show="isShowForm"
      v-model="currentRow"
      isShowTabs
      :bizCode="bizCode"
      isAuto
      @on-action="doexec"
    ></Forms>
  </div>
</template>
<script>
import { getTableData,create,update,deleteByIds} from "@/api/data";
import { tableInfoQuery } from "@/api/meta";
import Tables from "@/components/tables";
import Forms from "@/view/components/forms";
export default {
  name: "EditTable",
  components: {
    Tables,
    Forms
  },
  props: [
    "bizCode",
    "modelName",
    "listMethod",
    "deleteMethod",
    "updateMethod",
    "createMethod",
    "initMethod",
    "groupName",
    "isMultiSelect"
  ],
  data() {
    return {
      searchModel: {},
      startSearch: false,
      styles: {
        height: "calc(100% - 55px)",
        overflow: "auto",
        paddingBottom: "53px",
        position: "static"
      },
      currentRow: {},
      cols: [],
      tableData: [],
      rowtotal: 0,
      isShowForm: false,
      insideRowButtonInfo: [],
      insideListButtonInfo: [],
      currentSels: [],
      opstate:'normal'
    };
  },
  created() {
    tableInfoQuery(this.bizCode).then(res => {
      let cinfo = res.data.tableinfo;
      this.cols = cinfo;
      this.insideRowButtonInfo = res.data.authbuttons.inrow;
      this.insideListButtonInfo = res.data.authbuttons.onlist;
    });
  },
  mounted() {
    this.fetchdata(this.searchInfo);
  },
  computed:{
      searchInfo(){
          return {
            pageinfo:this.$refs.tables.insidePageInfo,
            search:this.searchModel
          }
        },
  },
  methods: {
    setCurrentSels(sels) {
      this.currentSels = sels;
    },
    buildPathPrefix() {
      let groupName = this.groupName ? this.groupName : "common";
      let modelName = this.modelName ? this.modelName : "common";
      return "web/" + groupName + "/" + modelName + "Ctl" + "/";
    },
    fetchdata(searchinfo) {
      let listMethod = this.listMethod ? this.listMethod : "findAndCountAll";
      let url = this.buildPathPrefix() + listMethod;
      getTableData(searchinfo, url).then(res => {
        this.tableData = res.data.results.rows;
        this.rowtotal = res.data.results.count;
      });
    },
    searchData(searchinfo) {
      console.log("searchinfo");
      this.fetchdata(searchinfo);
    },
    handleDelete(params) {
      console.log(params);
    },
    exportExcel() {
      this.$refs.tables.exportCsv({
        filename: `table-${new Date().valueOf()}.csv`
      });
    },
    doexec(action, item) {
      console.log(action);
      if (action == "search") {
        this.startSearch = true;
      }
      if (action == "searchok") {
        this.fetchdata(this.searchInfo);
        this.startSearch = false;
      }
      if (action == "delete") {
        if(currentSels.length<=0){
          return false;
        }else{
          let prefix=this.buildPathPrefix();
          let  methodName=this.deleteMethod?this.deleteMethod:"delete"
             let purl=prefix+methodName
          deleteByIds(purl,currentSels).then(res=>{
             if(res.status>=0){
                  this.$Notice.success({
                    title: '操作通知',
                    desc:  '删除记录成功. '
                  });
                   this.fetchdata(this.searchInfo);
              }else{
                  this.$Notice.error({
                    title: '操作通知',
                    desc: '删除记录失败. '
                  });
              }
          })
        }
      }
      if (action == "edit") {
        this.opstate="edit"
        this.isShowForm = true;
        this.currentRow = item;
      }
      if (action == "create") {
            this.opstate="create"
            this.currentRow={}
            this.isShowForm = true
           // this.$refs.editform.resetForm()
            //调用hou台，初始化当前currentRow，调用后台的initInstance
      }
      if (action == "save") {
        let prefix=this.buildPathPrefix();
        var  methodName="";
        if(this.opstate=="create"){
            methodName=this.createMethod?this.createMethod:"create"
            let purl=prefix+methodName
            create(purl,this.currentRow).then(res=>{
              if(res.status>=0){
                  this.$Notice.success({
                    title: '操作通知',
                    desc: '新增记录成功. '
                  });
                   this.isShowForm = false;
                   this.fetchdata(this.searchInfo);
                   
              }else{
                  this.$Notice.error({
                    title: '操作通知',
                    desc:'新增记录失败. '
                  });
              }
             
            })
        }
        if(this.opstate=="edit"){
            methodName=this.createMethod?this.updateMethod:"update"
            let purl=prefix+methodName
            update(purl,this.currentRow).then(res=>{
              if(res.status>=0){
                    this.$Notice.success({
                    title: '操作通知',
                    desc:  '更新记录成功. '
                  });
                  this.fetchdata(this.searchInfo);
                  this.isShowForm = false;
              }else{
                 this.$Notice.error({
                    title: '操作通知',
                    desc:  '更新记录失败. '
                  });
              }
             
            })
        }
      }
      if (action == "cancel") {
          this.isShowForm = false;
          // this.fetchdata(this.searchModel);
      }
      if (action == "export") {
        this.$refs.tables.exportCsv({
          filename: `table-${new Date().valueOf()}.csv`
        });
      }
    }
  }
};
</script>
