<template>
  <div>

    <div ref="lxHeader">
      <lx-header icon="md-apps" :text="`${chukuType}出库管理`" 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['addoutbounds'].isShow = true,$refs['addoutbounds'].type = 'add',
              $refs['addoutbounds'].chukuType = chukuType,
              $refs['addoutbounds'].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" v-loading="loading" :total="total" @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">
            <template v-if="isCkName=='仓库管理员'||roleName=='系统管理员'">
              <Button v-if='scope.row.zhuangtai==="已办结"' type="primary" size="small" style="margin-left: 10px;" @click="editorChuku(scope.row.id,'outbounds')">出库</Button>
            </template>
            <template v-if="scope.row.jilurenyuan==authName||isCkName=='仓库管理员'||roleName=='系统管理员'">
              <Button type="primary" size="small" style="margin-left: 10px;" @click="printChuku(scope.row.id,'print')">打印</Button>
           <!-- <template v-if="scope.row.jilurenyuan==authName||roleName=='系统管理员'"> -->
              <Button type="primary" size="small" style="margin-left: 10px;" @click="editorChuku(scope.row.id,'editor')">编辑</Button>
              <Button v-if='scope.row.zhuangtai==="待处理"' type="primary" size="small" style="margin-left: 10px;" @click="tofollow(scope.row.id,scope.row)">请示流程</Button>
              <Poptip transfer confirm title="确认要删除吗？" @on-ok="deleteChuku(scope.row)">
                <Button type="primary" style="margin-left: 10px;" size="small" ghost>删除</Button>
              </Poptip>
            <!-- </template> -->
            </template>
          </template>
        </el-table-column>
      </template>

    </xy-table>
    <addoutbounds ref="addoutbounds" @refresh="getindex"></addoutbounds>
    <printoutbounds ref="printoutbounds" @refresh="getindex"></printoutbounds>
    <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 {
    getInfo
  } from "@/api/user.js"
  import {
    getOutboundsStatus,
    getOatoken
  } from "@/api/contract.js"
  import addoutbounds from "./component/addoutbounds.vue"
  import printoutbounds from "./component/printoutbounds.vue"
  import {
    Message
  } from 'element-ui'
  import imports from "@/views/component/imports.vue"
  import {
    index as fieldIndex
  } from "@/api/system/customFormField";
  import {
    getparameter
  } from "@/api/system/dictionary";
  export default {
    components: {
      addoutbounds,
      imports,
      printoutbounds
    },
    data() {
      return {
        window: {
          width: 0,
          height: 0,
          top: 0,
          left: 0
        },
        loading:false,
        userName:'',
        authName:'',
        roleName:'',
        isCkName:'',
        select: {
          pageSize: 10,
          pageIndex: 1,
          keyword: ""
        },
        chukuType:'领用',
        customForm: {
          customFormId: "",
          tableName: ""
        },
        wuziguanli_oatoken:'',
        form: [],
        total: 0,
        list: [],
        table: [{
            label: '序号',
            type: 'index',
            fixed: 'left',
            width:80,
          },{
            label: "出库时间",
            width: 180,
            prop: 'chukushijian',
            align: 'center',
            fixed: 'left',
          },
          {
            label: "状态",
            width: 180,
            prop: 'zhuangtai',
            align: 'center'
          },
          {
            label: "出库类型",
            width: 180,
            prop: 'chukuleixing',
            align: 'center',
          },
          {
            label: "借用仓库",
            width: 180,
            prop: 'jieyongcangku',
            align: 'center',
          },
          {
            label: "借用部门",
            width: 180,
            prop: 'jieyongbumen',
            align: 'center',
          },
          // {
          //   label: "借用人",
          //   width: 180,
          //   prop: 'jieyongren',
          //   align: 'center',
          // },
          {
            label: "经办人",
            width: 180,
            prop: 'jilurenyuan',
            align:'center'
          },
          {
            label: "出库详情",
            width: 300,
            prop: 'id_outbounds_items_outbounds_id_relation',
            align:'left',
            customFn: (row) => {
              if(row.id_outbounds_items_outbounds_id_relation.length>0){
                return row.id_outbounds_items_outbounds_id_relation.map(item => {
                    return ( <p> {item.wuzibianma}-{item.zichanmingcheng}
                   ：{item.guigexinghao?'('+item.guigexinghao+')':''}
                      {item.jieyongshuliang}{item.jiliangdanwei}
                       </p>)
                })
              }else{
                return
              }

            }
            // formatter(cell, data, value, index){
            //   console.log("value",value)
            //   if(value.length>0){
            //     let html = ''
            //     for(var item of value){
            //       html+= `${item.wuzibianma}-${item.zichanmingcheng}${item.guigexinghao?'('+item.guigexinghao+')':''}-${item.jieyongshuliang}${item.jiliangdanwei}`
            //     }
            //     return html
            //   }else{
            //     return ''
            //   }
            // }
          },
          {
            label: "备注",
            prop: 'beizhu',
            align: 'left'
          }
        ]
      }
    },
    methods: {
     async getUserName(){
       getInfo().then(res => {
         console.log("res",res)
         this.userName = res.username
         this.authName = res.name
         for(var k of res.role){
           if(k.name=='系统管理员'){
             this.roleName = k.name
           }
           if(k.name=='仓库管理员'){
             this.isCkName = '仓库管理员'
           }
         }

       }).catch(error => {})
      },
      async getindex() {
        this.loading = true
        const res = await index({
          page_size: this.select.pageSize,
          page: this.select.pageIndex,
          table_name: 'outbounds',
          filter:[{"key":"jilurenyuan","op":"like","value":this.select.keyword},
          {"key":"chukuleixing","op":"like","value":this.chukuType}],
        })
        this.list = res.data
        this.total = res.total
        this.loading = false
      },

      pageChange(e) {
        this.select.pageIndex = e
        this.getindex()
      },
      editorChuku(id, type) {
        this.$refs['addoutbounds'].id = id
        this.$refs['addoutbounds'].isShow = true
        this.$refs['addoutbounds'].type = type
        this.$refs['addoutbounds'].chukuType = this.chukuType
      },
      printChuku(id, type){
        this.$refs['printoutbounds'].id = id
        this.$refs['printoutbounds'].isShow = true
        this.$refs['printoutbounds'].type = type
        this.$refs['printoutbounds'].chukuType = this.chukuType
      },
      async tofollow(id,row){
        console.log(row)
        if(row.id_outbounds_items_outbounds_id_relation.length<1){
          Message({
            type: 'warning',
            message: '请补充出库物资'
          })
          return
        }
        console.log(row)
        // return
        let wuzimingcheng = ''
        let shuliang = ''
        let guigexinghao = ''
        row.id_outbounds_items_outbounds_id_relation.map(item=>{
          wuzimingcheng+=item.zichanmingcheng+'、'
          shuliang+=item.jieyongshuliang+'、'
          guigexinghao+=item.guigexinghao+'、'
        })

        let default_json = {
          // 'yewukeshi':row.jieyongbumen,
          // 'bianhao':row.chukushijian+'-'+row.id,
          // 'wuzimingcheng':row.id_outbounds_items_outbounds_id_relation[0].zichanmingcheng,
          // 'lingyongshijian':row.chukushijian,
          // 'shuliang':row.id_outbounds_items_outbounds_id_relation[0].jieyongshuliang,
          // 'xinghaoguige':row.id_outbounds_items_outbounds_id_relation[0].guigexinghao,
          // 'jingbanren':row.jilurenyuan,
          // 'beizhu':row.beizhu,
          // 'gudingzichanbianhao':row.gudingzichanbianhao,
          // 'baofeiyuanyin':row.baofeiyuanyin,
          "wuzimingcheng":wuzimingcheng,
          "shuliang":shuliang,
          "xinghaoguige":guigexinghao,
          'yewukeshi':row.jieyongbumen,
          'bianhao':row.chukushijian+'-'+row.id,
          'lingyongshijian':row.chukushijian,
          'jingbanren':row.jilurenyuan,
          'beizhu':row.beizhu,
          'baofeiyuanyin':row.baofeiyuanyin,
          'link_id':row.id
        }
        let typeid = ''
        let typeObj = JSON.parse(process.env.VUE_APP_OA_URL_TYPE)
        for(var k in typeObj){
          if(k==this.chukuType){
            typeid =  parseInt(typeObj[k])
          }
        }
        // let res = await getOatoken()
         // = this.chukuType=='领用'?31:(this.chukuType=='处置'?32:33)
        // http://suzhouhedaooa.langye.net/admin/flow/create/31?username=admin&default_json={%22bianhao%22:%22100%22}
        let url =
          `${process.env.VUE_APP_OA_URL}/admin/flow/create/${typeid}?wuziguanli_oatoken=${this.wuziguanli_oatoken}&username=${this.userName}&default_json=${JSON.stringify(default_json)}`
        let seeBuy = window.open(url, 'follow',
          `top=${this.window.top},left=${this.window.left},width=${this.window.width},height=${this.window.height},location=0`
          )
            console.log(url)
      },
      deleteChuku(row) {
        // return
        destroy({
          id: row.id,
          table_name: 'outbounds',
          destroy_relation:['id_outbounds_items_outbounds_id_relation'],
        }).then(res => {
          Message({
            type: 'success',
            message: '删除成功'
          })
          this.getindex()
        })
      },
    },
    mounted() {
      if(this.$route.path){
        let path = this.$route.path.split("_")[1]
        this.chukuType = path=='receive'?'领用':(path=='disposition'?'处置':'应急')
        let tableName = path=='receive'?'lingyongchuku':(path=='disposition'?'baofeichuzhi':'yingjichuku')
        getOutboundsStatus({
          table:tableName
        })
      }
      this.window.width = screen.availWidth * 0.95
      this.window.height = screen.availHeight * 0.95
      this.window.top = (window.screen.height - 30 - this.window.height) / 2
      this.window.left = (window.screen.width - 10 - this.window.width) / 2
      this.getUserName()
      this.getindex()
      getOatoken().then(res=>{
        this.wuziguanli_oatoken =res.oatoken
      })

    }

  }
</script>

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