<template>
  <div class="priceManage">
    <module-view :propConfig="config" ref="view" v-loading="load" @modalHandle="modalHandle" @onRefresh="getList" 
    @resert="queryList" @search="queryList" @clickHandle="clickHandle" @detail="detail" />
  </div>
</template>

<script lang="ts">
import { Component, Prop, Vue, Watch } from "vue-property-decorator";
import { queryPrice, add, update, del } from '@/api/priceManage'
@Component({components:{}})
export default class SourceShop extends Vue {
  load=false;
  time:any;
  timeNum = 0;
  isSearch=false;
  config:any={
    attr:{
      calculateH:true
    },
    // search:{
    //   attr:{
    //     size:'small'
    //   },
    //   columns:[]
    // },
    tool:{
      tools:{
        add:true,
        delete:true,
        // search:true,
        refresh:true
      }
    },
    table:{
      attr:{
        size:'mini',
        seq:true,
        align:'center',
        checkbox:true
      },
      columns:[{
        title:'价格',
        field:'price',
        isDetail:true,
      },
      // {
      //   title:'类型',
      //   field:'type'
      // },
      {
        title:'备注',
        field:'remark',
      },
      {
        title:'操作',
        action:true,
        width:100,
        plugins:[{
          name:'删除',
          event:{
            click:(item:any) => {
              this.del([item.id]);
            }
          }
        }]
      }
    ]
    },
    modal:{
      tool:{
        tools:{
          return:true,
          add:true
        }
      },
      form:{
        attr:{
          width:'800px',
          size:'small',
          labelWidth:'130px',
          rules:{
            price:[
              {required: true, message: '请输入价格', trigger: 'blur'}
            ]
          }
        },
        columns:[
          [{
            label:'价格',
            prop:'price',
            component:'by-input',
            compConfig: {
              attr:{
                type: 'float'
              }
            }
          }],
          // [{
          //   label:'类型',
          //   prop:'type',
          //   component:'by-input'
          // }],
          [{
            label:'备注',
            prop:'remark',
            component:'by-input'
          }]
        ]
      }
    }
  }
  mounted(){
    this.$nextTick(()=>{
      this.getList()
    })
  }
  //删除
  del(ids:Array<number>){
    this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(() => {
      this.load = true;
      del({ids:ids}).then(()=>{
        this.load = false;
        this.$message({
          message:'删除成功！',
          type:'success'
        })
        this.pagination();
      }).catch(()=>{
        this.load = false;
      })
    }).catch(() => {});
  }
  //分页
  pagination(){
    if(this.isSearch){
      this.queryList();
    }else{
      this.getList()
    }
  }
  //列表请求（只有分页，不包含搜素条件）
  getList(){
    if(!this.$refs.view){
      if(this.timeNum > 5){
        return
      }
      setTimeout(()=>{
        this.getList()
      },500) 
      this.timeNum ++;
      return
    }
    this.isSearch = false;
    let data = (this.$refs.view as any).getPage();
    this.requestList(data);
  }
  //列表请求（包含分页和搜素条件）
  queryList(){
    this.isSearch = true;
    let data = (this.$refs.view as any).getQuery();
    this.requestList(data);
  }
  requestList(data:any){
    this.load = true;
    queryPrice(data).then((res:any) => {
      this.load = false;
      (this.$refs.view as any).setTableValue(res.data);
      // let page = {
      //   pageNo: res.data.current, //当前页
      //   pageSize: res.data.size, //每页条数
      //   total: res.data.total //总条数
      // };
      // (this.$refs.view as any).setPage(page)

    }).catch(()=>{
      this.load = false;
    })
  }
  //修改价格数据
  onSave(){
    let data = (this.$refs.view as any).getFormValue();
    this.load = true;
    update(data).then((res:any) => {
      this.$message({
        message:res.msg,
        type:'success'
      });
      this.load = false;
      (this.$refs.view as any).closeModal();
      this.pagination();
    }).catch(()=>{
      this.load = false;
    })
  }
  //表单工具栏按钮事件
  modalHandle(n:string){
    if(n == 'onAdd'){
      (this.$refs.view as any).getFormValidate(this.addPrice)
    }

    if(n == 'onSave'){
      (this.$refs.view as any).getFormValidate(this.onSave)
    }
  }
  //表格工具栏按钮事件
  clickHandle(n:string){ 
    if(n == 'onAdd'){
      this.config.modal.tool.tools.add = true;
      delete this.config.modal.tool.tools.save;
      (this.$refs.view as any).initFormTool();
    }
    if(n == 'onDelete') {
      let data = (this.$refs.view as any).getSelectData();
      if(!data || data.length == 0) {
        this.$message('请选择数据！');
        return
      }
      let ids:Array<number> = [];
      for(const item of data) {
        ids.push(item.id)
      }
      this.del(ids);
    }
  }
  //添加平台
  addPrice(){
    let value = (this.$refs.view as any).getFormValue();
    if(value){
      this.load = true;
      add(value).then((res:any)=>{
        this.load = false;
        this.$message({
          message:res.msg,
          type:'success'
        });
        (this.$refs.view as any).closeModal();
        this.getList();
      }).catch(()=>{
        this.load = false;
      })
    }
  }
  //点击详情
  detail(){
    this.config.modal.tool.tools.save = true;
    delete this.config.modal.tool.tools.add;
    (this.$refs.view as any).initFormTool();
  }
}
</script>

<style lang="scss" scoped>
.priceManage{
  width: 100%;
  height: 100%;
  overflow-y: hidden;
}
</style>