<template>
    <a-card :bordered="false">
      <!-- 查询区域 -->
      <div class="table-page-search-wrapper">
        <a-form layout="inline" @keyup.enter.native="searchQuery">
          <a-row :gutter="24">
            <a-col :xl="6" :lg="7" :md="8" :sm="24" :hidden="show">
              <a-form-item label="项目部">
                <a-select v-model="queryParam.departId" placeholder="请输入项目部"  @mouseenter="getUnit" >
                  <a-select-option v-for="(item,index) in queryParamsUntil" :key="index" :value="item.unit">{{item.unit_dictText}}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="物料组">
                <a-input placeholder="请输入物料组" v-model="queryParam.materialGroup"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="物料">
                <a-input placeholder="请输入物料" v-model="queryParam.materialNumber"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="物料描述">
                <a-input placeholder="请输入物料描述" v-model="queryParam.materialDescription"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="核算属性">
                <j-dict-select-tag placeholder="请输入核算属性" v-model="queryParam.accountingAttributes" dictCode="material_type"></j-dict-select-tag>
<!--                <a-input placeholder="请输入核算属性" v-model="queryParam.accountingAttributes" dictCode="material_type"></a-input>-->
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <!-- 查询区域-END -->

      <!-- 操作按钮区域 -->
      <div class="table-operator">
        <a-button type="primary" icon="download" @click="handleExportXls('账外物资库')">导出</a-button>
<!--        <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">-->
<!--          <a-button type="primary" icon="import">导入</a-button>-->
<!--        </a-upload>-->
        <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
        <a-button type="primary" @click="searchReset" icon="reload">重置</a-button>
      </div>

      <!-- table区域-begin -->
      <div>
        <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
          <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
          <a style="margin-left: 24px" @click="onClearSelected">清空</a>
        </div>

        <a-table
          ref="table"
          size="middle"
          bordered
          :scroll="{ y:470}"
          rowKey="id"
          :columns="columns"
          :dataSource="dataSource"
          :pagination="ipagination"
          :loading="loading"
          :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
          @change="handleTableChange">

          <span slot="materialDescription" slot-scope="text, record">
            <j-ellipsis :value="text" :length="9"/>
          </span>
          <span slot="type" slot-scope="text, record">
            <j-ellipsis :value="text" :length="9"/>
          </span>
          <span slot="specification" slot-scope="text, record">
            <j-ellipsis :value="text" :length="9"/>
          </span>
          <template slot="htmlSlot" slot-scope="text">
            <div v-html="text"></div>
          </template>
          <template slot="imgSlot" slot-scope="text">
            <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
            <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
          </template>
          <template slot="fileSlot" slot-scope="text">
            <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
            <a-button
              v-else
              :ghost="true"
              type="primary"
              icon="download"
              size="small"
              @click="downloadFile(text)">
              下载
            </a-button>
          </template>

          <span slot="action" slot-scope="text, record" style="overflow: hidden;" class="table-page-search-submitButtons">
          <a-dropdown>
            <a class="ant-dropdown-link" @click="handleDetail(record)"><a-icon type="transaction" /> 明细</a>
          </a-dropdown>
          </span>


        </a-table>


      </div>

      <process-waste-materials-modal ref="modalForm" @ok="modalFormOk"></process-waste-materials-modal>
    </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import ProcessWasteMaterialsModal from '@views/wastematerials/modules/ProcessWasteMaterialsModal'
  import JDate from '@/components/jeecg/JDate.vue'
  import { httpAction, getAction } from '@/api/manage'
  import JSuperQuery from '@/components/jeecg/JSuperQuery.vue'
  import ProcessWasteMaterialsLists from '@views/wastematerials/ProcessWasteMaterialsLists'
  import { ajaxGetDictItems, getDictItemsFromCache } from '@api/api'
  import {initDictOptions, filterDictText} from '@/components/dict/JDictSelectUtil'
  import JSelectDepart from '@/components/jeecgbiz/JSelectDepart'
  export default {
    name: 'ProcessWasteMaterialsList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      ProcessWasteMaterialsLists,
      JDate,
      ProcessWasteMaterialsModal,
      JSuperQuery,
      JSelectDepart
    },
    data () {
      return {
        description: '账外物资库管理页面',
        // 表头
        columns: [
          {
            title: '序号',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'物料组',
            align:"center",
            dataIndex: 'materialGroup'
          },
          {
            title:'物料编号',
            align:"center",
            dataIndex: 'materialNumber'
          },
          {
            title:'物料名称及规格型号',
            align:"center",
            scopedSlots: { customRender: 'materialDescription' },
            dataIndex: 'materialDescription'
          },
          // {
          //   title:'型号',
          //   align:"center",
          //   scopedSlots: { customRender: 'type' },
          //   dataIndex: 'type'
          // },
          // {
          //   title:'规格',
          //   align:"center",
          //   scopedSlots: { customRender: 'specification' },
          //   dataIndex: 'specification'
          // },
          {
            title:'计量单位',
            align:"center",
            dataIndex: 'materialsUnit'
          },
          {
            title:'现有数量',
            align:"center",
            dataIndex: 'existingNumber'
          },
          {
            title:'消耗数量',
            align:"center",
            dataIndex: 'usageQuantity'
          },
          {
            title: '查看详情',
            dataIndex: 'action',
            align:"center",
            width:147,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/wastematerials/processWasteMaterials/list",
          delete: "/wastematerials/processWasteMaterials/delete",
          deleteBatch: "/wastematerials/processWasteMaterials/deleteBatch",
          exportXlsUrl: "/wastematerials/processWasteMaterials/exportXls",
          importExcelUrl: "wastematerials/processWasteMaterials/importExcel",

          listByuntil: '/unitwaste/processUnitWasteWarehouseCode/list',
          queryLibraryName: "/libraryassociation/processLibraryAssociation/list",

        },
        dictOptions:{},
        superFieldList:[],
        libraryNames:[],
        Listmaterials:[],
        queryParamsUntil:[],
        show: true
      }
    },
    created() {
      this.showDepart();
      this.getSuperFieldList();
      this.initDictConfig();
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      //加載項目部
      getUnit(){
        getAction(this.url.listByuntil).then((res) => {
          if (res.success) {
            this.queryParamsUntil =  res.result.records||res.result;
            console.log(this.queryParamsUntil)
            // // for (let i = 0; i <this.queryParamsUntil.length; i++) {
            // //   console.log(this.queryParamsUntil[i].unit)
            // // }
            // // console.log(this.queryParamsUntilList)
          }
        })
      },
      loadData(arg) {
        if(!this.url.list){
          this.$message.error("请设置url.list属性!")
          return
        }
        //加载数据 若传入参数1则加载第一页的内容
        if (arg === 1) {
          this.ipagination.current = 1;
        }
        this.section();
        var params = this.getQueryParams();//查询条件
        this.loading = true;
        console.log("params>>>>>>",params)
        getAction(this.url.list, params).then((res) => {
          if (res.success) {
            this.dataSource = res.result.records||res.result;
            if(res.result.total)
            {
              this.ipagination.total = res.result.total;
            }
          }
          if(res.code===510){
            this.$message.warning(res.message)
          }
          this.loading = false;
        })
      },

      //是否隐藏项目部查询
      showDepart(){
        let ue= sessionStorage.getItem('USER_INFORMATION');
        let res=JSON.parse(ue);
        if(res.realname == "管理员"){
          this.show = false;
        }
      },

      //获取部门ID
      section(){
        let ue= sessionStorage.getItem('USER_INFORMATION');
        let res=JSON.parse(ue);
        console.log("23r4qwerfasdfgzsfh",res)
        if(res.realname !== "管理员"){
          this.queryParam.departIds = res.departIds;
        }
      },

      handleDetail(record){
        // console.log("1111111111111111111111111111111111")
        // console.log(record);
        this.$router.push({
          // name:'src-views-wastematerials-ProcessWasteMaterialsLists',
          // params:{
          //   merchandiseNewsId: record.merchandiseNewsId,
          //   materialStorageId : record.materialStorageId
          // }
          path:'/materials/wastematerials_detail',
          query:{
            merchandiseNewsId: record.merchandiseNewsId,
            materialStorageId : record.departId,
            merchandiseName : ' - '+record.materialDescription.substring(0,10)
          }
        })
      },
      initDictConfig(){
        //优先从缓存中读取字典配置
        if(getDictItemsFromCache(this.dictCode)){
          this.dictOptions = getDictItemsFromCache(this.dictCode);
          return
        }

        //根据字典Code, 初始化字典数组
        ajaxGetDictItems(this.dictCode, null).then((res) => {
          if (res.success) {
               console.log(res.result);
            this.dictOptions = res.result;
          }
        })
      },
      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'string',value:'supplier',text:'供应商',dictCode:''})
        fieldList.push({type:'string',value:'supplierDescription',text:'供应商描述',dictCode:''})
        fieldList.push({type:'string',value:'materialGroup',text:'物料组',dictCode:''})
        fieldList.push({type:'string',value:'materialNumber',text:'物料',dictCode:''})
        fieldList.push({type:'string',value:'materialDescription',text:'物料描述',dictCode:''})
        fieldList.push({type:'string',value:'materialStorageId',text:'库存位置',dictCode:''})
        fieldList.push({type:'string',value:'freightSpace',text:'库存仓位',dictCode:''})
        fieldList.push({type:'string',value:'materialsUnit',text:'计量单位',dictCode:''})
        fieldList.push({type:'int',value:'materialQuantity',text:'数量',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'price',text:'单价',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'materialAmount',text:'金额',dictCode:''})
        fieldList.push({type:'date',value:'productionDate',text:'生产日期'})
        fieldList.push({type:'String',value:'expirationDate',text:'保质期'})
        fieldList.push({type:'Date',value:'storageTime',text:'入库时间',dictCode:''})
        fieldList.push({type:'string',value:'librarySign',text:'入库人',dictCode:''})
        fieldList.push({type:'int',value:'accountingAttributes',text:'核算属性',dictCode:''})
        fieldList.push({type:'string',value:'libraryNames',text:'库存位置',dictCode:''})
        this.superFieldList = fieldList
        let item ={
          state : 0,
          sort : 0
        }
        getAction(this.url.queryLibraryName,item).then((res)=>{
          if(res.success){
            this.libraryNames = res.result.records;
            // console.log(res.result.records);
          }
        });

        // getAction(this.url.queryListmaterials).then((res)=>{
        //   if(res.success){
        //     this.Listmaterials = res.result;
        //     // console.log(res.result);
        //   }
        // });
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>