<template>
  <div>

    <div ref="lxHeader">
      <lx-header icon="md-apps" :text="rukuType=='回库'?`归还入库管理`:`${rukuType}入库管理`" style="margin-bottom: 10px; border: 0px; margin-top: 15px">
        <slot>
          <div style="display: flex;justify-content: flex-start;flex-wrap: wrap;">
            <Input v-model="select.keyword" style="width: 200px;margin-right: 10px;" placeholder="经办人搜索" />
            <Button type="primary" @click="getindex">查询</Button>
            <Button icon="ios-add" type="primary" style="margin-left: 10px;"
              @click="$refs['addRuku'].isShow = true,$refs['addRuku'].type = 'add',
              $refs['addRuku'].rukuType = rukuType
              $refs['addRuku'].authName =authName">添加</Button>
            <!-- <Button icon="ios-add" type="primary" style="margin-left: 10px;"
              @click="$refs['imports'].show()">导入</Button> -->

          </div>
        </slot>
      </lx-header>
    </div>

    <xy-table :list="list" :total="total" v-loading="loading" @pageSizeChange="e => select.pageSize = e" @pageIndexChange="pageChange"
      :table-item="table">
      <template v-slot:btns>
        <el-table-column fixed="right" label="操作" width="260" header-align="center">
          <template slot-scope="scope">
            <div v-if="scope.row.jingbanren==authName||roleName=='系统管理员'||isCkName=='仓库管理员'">
              <Button type="primary" size="small" @click="editorRuku(scope.row.id,'editor')">编辑</Button>
              <!-- <Button type="primary" size="small" @click="editorRuku(scope.row.id,'editor')">请示流程</Button> -->
              <!-- <Button type="primary" size="small" @click="editorActivity(scope.row.id,'editor')">编辑</Button>
               <Button style="margin-left: 10px;" type="primary" size="small" @click="checkActivity(scope.row.id)">审核</Button>
              <Button type="primary" size="small" style="margin-left: 10px;" @click="editorActivity(scope.row.id,'copy')">复制新增</Button> -->
              <Poptip transfer confirm title="确认要删除吗？" @on-ok="deleteRuku(scope.row)">
                <Button type="primary" style="margin-left: 10px;" size="small" ghost>删除</Button>
              </Poptip>
            </div>

          </template>
        </el-table-column>
      </template>

    </xy-table>
    <addRuku ref="addRuku" @refresh="getindex"></addRuku>

    <imports :table-name="customForm.tableName" :form-info="form" ref="imports" @refresh="getindex"></imports>

  </div>
</template>

<script>
  import {
    index,
    destroy
  } from "@/api/system/baseForm.js"
  import addRuku from "./component/addRuku.vue"
  import {
    Message
  } from 'element-ui'
  import {
    getInfo
  } from "@/api/user.js"
  import imports from "@/views/component/imports.vue"
  import {
    index as fieldIndex
  } from "@/api/system/customFormField";
  import {
    getparameter
  } from "@/api/system/dictionary";
  export default {
    components: {
      addRuku,
      imports
    },
    data() {
      return {
        authName:'',
        roleName:'',
        isCkName:'',
        loading:false,
        select: {
          pageSize: 10,
          pageIndex: 1,
          keyword: ""
        },
        rukuType:'',
        rukuTypeList:{
          purchase:'采购',
          return:'回库',
          check:'盘点',
          data:"数据校对",
          mock:'模拟盘点',
          ask:'请示',
          yingji:'应急采购'
        },
        customForm: {
          customFormId: "",
          tableName: "stocks"
        },
        form: [],
        total: 0,
        list: [],
        table: [{
            label: '序号',
            type: 'index',
            fixed: 'left',
            width:80
          },
          {
            label: "入库时间",
            width: 180,
            prop: 'rukushijian',
            align: 'center',
            formatter:(cell,data,value)=>{
              return value?value.substring(0,10):''
            }
          },
          {
            label: "入库类型",
            width: 180,
            prop: 'rukuleixing',
            align: 'center'
          },
          {
            label: "经办人",
            width: 180,
            prop: 'jingbanren',
            align: 'center'
          },
          {
            label: "记录人员",
            width: 180,
            prop: 'jilurenyuan',
            align: 'center'
          },
          {
            label: "保管人员",
            width: 180,
            prop: 'baoguanrenyuan',
            align: 'center'
          },
          {
            label: "固定资产编号",
            width: 180,
            prop: 'gudingzichanbianhao',
            align: 'center'
          },
          {
            label: "备注",
            width: 240,
            prop: 'beizhu',
            align: 'left'
          }
        ]
      }
    },
    methods: {
      async getUserName(){
         getInfo().then(res => {
           for(var k of res.role){
             if(k.name=='系统管理员'){
               this.roleName = k.name
             }
             if(k.name=='仓库管理员'){
               this.isCkName = '仓库管理员'
             }
           }
          this.authName = res.name
         }).catch(error => {})

      },
      async getindex() {
        this.loading = true
        const res = await index({
          page_size: this.select.pageSize,
          page: this.select.pageIndex,
          table_name: 'stocks',
          filter: [{
              "key": "jingbanren",
              "op": "like",
              "value": this.select.keyword
            },
            {
              "key": "rukuleixing",
              "op": "eq",
              "value": this.rukuType
            }
          ],
        })
        this.list = res.data
        this.total = res.total
        this.loading = false
      },

      pageChange(e) {
        this.select.pageIndex = e
        this.getindex()
      },
      editorRuku(id, type) {
        this.$refs['addRuku'].id = id
        this.$refs['addRuku'].type = type
        this.$refs['addRuku'].isShow = true
        // this.$refs['addRuku'].getDetail()
        this.$refs['addRuku'].rukuType = this.rukuType
      },
      deleteRuku(row) {
        console.log(row.id)
        // return
        destroy({
          id: row.id,
          table_name: this.customForm.tableName,
          destroy_relation:['id_stocks_item_links_stocks_id_relation','id_stocks_items_stocks_id_relation'],
        }).then(res => {
          Message({
            type: 'success',
            message: '删除成功'
          })
          this.getindex()
        })
      },
    },
    mounted() {
      if(this.$route.path){
        let path = this.$route.path.split("_")[1]
        console.log(path)
        for(var k in this.rukuTypeList){
          if(path==k){
            this.rukuType = this.rukuTypeList[k]
            if(this.rukuType=='采购'||this.rukuType=='请示'||this.rukuType=='应急采购'){
              let rowname = ''
              rowname = this.rukuType=='采购'? 'caigouhetong':(this.rukuType=='请示'?'qingshiliucheng':(this.rukuType=='应急采购'?'yingjicaigou':''))
              this.table.unshift(          {
            label: "关联记录",
            width: 240,
            prop: 'caigouhetong',
            align: 'left',
            customFn: (row) => {
              return ( <div>
                {row[rowname]}
              </div> )
            }
          })
            }
          }
        }
      }
      this.getUserName()
      this.getindex()
    }

  }
</script>

<style scoped lang="scss">
</style>
