<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">
            <a-form-item label="委托单位">
              <!-- <j-search-select-tag placeholder="请选择委托单位名称" :dictOptions="weituoClientList" @change="handlewcChange"/> -->
              <j-search-select-tag placeholder="请选择委托单位名称" v-model="queryParam.weituoClient" :dictOptions="weituoClientList" @change="handlewcChange"/>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="委托编号">
              <j-search-select-tag placeholder="请选择委托编号" v-model="queryParam.weituoNo" :dictOptions="weituoNoOptions"/>
            </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.shenheStatus" dictCode="shenhe_status"/>
              <!-- <j-search-select-tag placeholder="请选择委托单位名称" v-model="queryParam.weituoClient" :dictOptions="weituoClientList"/> -->
            </a-form-item>
          </a-col>
          <!-- <a-col :xl="7" :lg="7" :md="8" :sm="24"> -->
            <!-- <a-form-item label="委托单位名称">
              <a-input placeholder="请输入委托单位名称" v-model="queryParam.weituoClient"></a-input>
            </a-form-item> -->
             <!-- <a-select
              show-search
              placeholder="请选择"
              v-model="queryParam.id"
              :filter-option="false"
              :options="options"
              :allow-clear="true"
            >
            </a-select> -->
          <!-- </a-col> -->
          <template v-if="toggleSearchStatus">
            <a-col :xl="10" :lg="11" :md="12" :sm="24">
              <a-form-item label="选择时间">
                <!-- <j-date :show-time="{ format: 'HH:mm:ss' }" date-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择开始时间" class="query-group-cust" v-model="queryParam.startTime"></j-date>
                <span class="query-group-split-cust"></span>
                <j-date :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择结束时间" class="query-group-cust" v-model="queryParam.endTime"></j-date> -->
                <a-date-picker :show-time="{ format: 'HH:mm:ss' }" class="query-group-cust" format="YYYY-MM-DD HH:mm:ss" v-model="queryParam.startTime" placeholder="请输入开始时间"/>
                <span class="query-group-split-cust"></span>
                <a-date-picker  format="YYYY-MM-DD HH:mm:ss" class="query-group-cust" v-model="queryParam.endTime" placeholder="请输入结束时间"/>
              </a-form-item>
              <!-- <a-date-picker :show-time="{ format: 'HH:mm:ss' }" format="YYYY-MM-DD HH:mm:ss" v-model="queryParam.startTime" placeholder="请输入开始时间"/> -->
            </a-col>
          </template>
           <!-- <a-col :xl="5" :lg="7" :md="8" :sm="24">
           <a-date-picker  format="YYYY-MM-DD HH:mm:ss" v-model="queryParam.endTime" placeholder="请输入结束时间"/>
          </a-col> -->

          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->
    <!-- 操作按钮区域 -->
    <!-- <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <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> -->
      <!-- 高级查询区域 -->
      <!-- <j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </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> 已选择 <div style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div> -->

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        class="j-table-force-nowrap"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text,record">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" :preview="record.id" 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="download" slot-scope="text, record">
          <a v-if="record.shenheStatus ==1" @click="handleDownload(record)">下载委托</a>
          <a v-if="record.shenheStatus !=1" disabled>下载委托</a>
        </span> -->

        <span slot="action" slot-scope="text, record">
          <!-- <a @click="handleEdit(record)">编辑</a> -->
          <a @click="handleDetail(record)">详情</a>
          <!-- <a-divider type="vertical" v-if="record.shenheStatus ==1"/>
          <a v-if="record.shenheStatus ==1" @click="handleDownload(record)">下载委托</a> -->

          <!-- <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown> -->
        </span>

      </a-table>
    </div>

    <itdm-weituo-yangpin-modal ref="modalForm" @ok="modalFormOk"></itdm-weituo-yangpin-modal>
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import ItdmWeituoYangpinModal from './modules/ItdmWeituoYangpinModal'
  import { httpAction, getAction } from '@/api/manage'
  import { downWeituo_info, getWeituoInfoListByClient } from '@api/api'
  export default {
    name: 'ItdmWeituoYangpinList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      ItdmWeituoYangpinModal
    },
    data () {
      return {
        queryParam: {
          shenheStatus: 1,
          weituoClient: undefined,
          weituoNo: undefined
        },
        weituoClientList:[],
        weituoNoOptions: [],
        description: '委托样品管理页面',
        // 表头
        columns: [
          // {
          //   title: '#',
          //   dataIndex: '',
          //   key:'rowIndex',
          //   width:60,
          //   align:"center",
          //   customRender:function (t,r,index) {
          //     return parseInt(index)+1;
          //   }
          // },
          {
            title:'委托单位名称',
            align:"center",
            dataIndex: 'weituoClient',
            customRender:function (t,r,index) {
              return {children: t,attrs: {rowSpan:r.rowSpan}};
            },
            // customRender: (value, row, index) => {
            //   console.log(value, row, index);
            //   const obj = {
            //     children: value,
            //     attrs: {},
            //   };
            //   if(index === 0){   // 第一行数据开始，跨行合并的长度为数据data的长度
            //     obj.attrs.rowSpan = this.dataSource.length;
            //   }
            //   if (index >= 1) {   // 从第一行往后的所有行表格均合并
            //     obj.attrs.rowSpan = 0;
            //   }
            //   return obj;
            // }
          },
          {
            title:'委托编号',
            align:"center",
            dataIndex: 'weituoNo',
            customRender:function (t,r,index) {
              return {children: t,attrs: {rowSpan:r.rowSpan3}};
            },
          },
          {
            title:'样品名称',
            align:"center",
            dataIndex: 'sampleName',
            customRender:function (t,r,index) {
              return {children: t,attrs: {rowSpan:r.rowSpan2}};
            },
          },
          {
            title:'样品规格/型号',
            align:"center",
            dataIndex: 'sampleModelSpecification'
          },
          {
            title:'样品数量',
            align:"center",
            dataIndex: 'sampleQuantities'
          },
          {
            title:'检测项目',
            align:"center",
            dataIndex:'testItems'
          },
          {
            title:'检测依据',
            align:"center",
            dataIndex:'standardRequirement'
          },
                    {
            title:'判定依据',
            align:"center",
            dataIndex:'judgementBasis'
          },
                    {
            title:'备注',
            align:"center",
            dataIndex:'beizhu'
          },
                    {
            title:'时间',
            align:"center",
            dataIndex:'createTime'
          },
                    {
            title:'状态',
            align:"center",
            dataIndex:'shenheStatus_dictText'
          },
                    {
            title:'下载',
            align:"center",
            fixed:"right",
            width:70,
            dataIndex:'download',
            customRender:function (t,r,index) {
              var childrenVal
              if(r.shenheStatus === '1'){
                childrenVal = (<a href="#" id='download111'> 下载委托</a>)
              } else {
                childrenVal = (<a href="#" id='download111' disabled> 下载委托</a>)
              }
              return {
                children: childrenVal,
                attrs: {
                  rowSpan: r.rowSpan3
                }
              }
            },
            customCell: (row, index) => {
              return {
                on: {
                  click: (e) => {
                    this.handleDownload(row, index);
                  },
                },
              };
            },
          },
          // {
          //   title:'生产厂家',
          //   align:"center",
          //   dataIndex: 'sampleManufacturer'
          // },
          // {
          //   title:'样品编号',
          //   align:"center",
          //   dataIndex: 'sampleCode'
          // },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:70,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/weituo/ItdmWeituochaxun/list",
          delete: "/itdmWeituoChaXun/itdmWeituoYangpin/delete",
          deleteBatch: "/itdmWeituoChaXun/itdmWeituoYangpin/deleteBatch",
          exportXlsUrl: "/itdmWeituoChaXun/itdmWeituoYangpin/exportXls",
          importExcelUrl: "itdmWeituoChaXun/itdmWeituoYangpin/importExcel",

        },
        dictOptions:{},
        superFieldList:[],
      }
    },
    created() {
      this.init();
      this.getSuperFieldList();
      // this.getList();
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
            init(){

        getAction('/weituo/itdmWeituoInfo/distinctClientList', {}).then((res) => {
          if (res.success) {
            console.log(res.result)
            this.weituoClientList = res.result.map(i=>{
              return { value: i, text: i}
            })
            console.log(this.weituoClientList)
          }
        })

      },
      // 监听选中委托单位，获取该单位的委托编号
      handlewcChange(data){
        console.log(data)
        this.weituoNoOptions = []
        this.queryParam.weituoNo = undefined
        getWeituoInfoListByClient(this.queryParam).then((res) =>{
          console.log(res.result)
          this.weituoNoOptions = res.result.map( res => {
            return {
              value: res.weituoNo,  //传的值
              text: res.weituoNo  //展示
            }
          })
          console.log(this.weituoNoOptions)
        })
      },
//
      // getList(){
      //   let that = this
      //   getAction(this.url.list,this.queryParam).then((res) => {
      //     console.log(111111111,res)
      //     if (res.success) {
      //       this.dataSource = res.result.records
      //       //判断跨行
      //       let rowSpan = 0
      //       let weituoClient = ''
      //       // let sampleName = ''
      //       for (let i = that.dataSource.length-1; i >= 0; i--) {
      //         if (weituoClient == '') {
      //           that.dataSource[i].rowSpan = 0
      //           weituoClient = that.dataSource[i].weituoClient
      //         } else {
      //           if(weituoClient == that.dataSource[i].weituoClient){
      //             that.dataSource[i].rowSpan = 0
      //             rowSpan++
      //           }else{
      //             that.dataSource[i+1].rowSpan = rowSpan
      //             that.dataSource[i].rowSpan = 0
      //             rowSpan = 1
      //             weituoClient = that.dataSource[i].weituoClient
      //           }

      //         }
      //       }
      //       that.dataSource[0].rowSpan = rowSpan
      //       console.log(22222222222,that.dataSource)

      //     }
      //   })
      // },


      loadData(arg) {
        let that = this
      if(!this.url.list){
        this.$message.error("请设置url.list属性!")
        return
      }
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1;
      }
      var params = this.getQueryParams();//查询条件
      this.loading = true;
      getAction(this.url.list, params).then((res) => {
        if (res.success) {
          //update-begin---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
          this.dataSource = res.result.records||res.result;
          //判断跨行
            let rowSpan = 0
            let rowSpan2 = 0
            let rowSpan3 = 0
            let weituoClient = ''
            let sampleName = ''
            let weituoNo = ''
            for (let i = that.dataSource.length-1; i >= 0; i--) {
              // 委托单位合并
              if (weituoClient == '') {
                that.dataSource[i].rowSpan = 0
                weituoClient = that.dataSource[i].weituoClient
                rowSpan++
              } else {
                if(weituoClient == that.dataSource[i].weituoClient){
                  that.dataSource[i].rowSpan = 0
                  rowSpan++
                }else{
                  that.dataSource[i+1].rowSpan = rowSpan
                  that.dataSource[i].rowSpan = 0
                  rowSpan = 1
                  weituoClient = that.dataSource[i].weituoClient
                }

              }
              // 样品名称合并
              if (sampleName == '') {
                that.dataSource[i].rowSpan2 = 0
                sampleName = that.dataSource[i].sampleName
                rowSpan2++
              } else {
                if(sampleName == that.dataSource[i].sampleName){
                  that.dataSource[i].rowSpan2 = 0
                  rowSpan2++
                }else{
                  that.dataSource[i+1].rowSpan2 = rowSpan2
                  that.dataSource[i].rowSpan2 = 0
                  rowSpan2 = 1
                  sampleName = that.dataSource[i].sampleName
                }

              }
              // 委托编号合并
              if (weituoNo == '') {
                that.dataSource[i].rowSpan3 = 0
                weituoNo = that.dataSource[i].weituoNo
                rowSpan3++
              } else {
                if(weituoNo == that.dataSource[i].weituoNo){
                  that.dataSource[i].rowSpan3 = 0
                  rowSpan3++
                }else{
                  that.dataSource[i+1].rowSpan3 = rowSpan3
                  that.dataSource[i].rowSpan3 = 0
                  rowSpan3 = 1
                  weituoNo = that.dataSource[i].weituoNo
                }

              }
            }
            that.dataSource[0].rowSpan = rowSpan
            that.dataSource[0].rowSpan2 = rowSpan2
            that.dataSource[0].rowSpan3 = rowSpan3
            console.log(22222222222,that.dataSource)
          if(res.result.total)
          {
            this.ipagination.total = res.result.total;
          }else{
            this.ipagination.total = 0;
          }
          //update-end---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
        }else{
          this.$message.warning(res.message)
        }
      }).finally(() => {
        this.loading = false
      })
    },

    // 下载委托
    handleDownload(record){
      downWeituo_info({ id: record.weituoId }).then(response => {
        console.log(response)
        const blob = new Blob([response])
        const url = window.URL.createObjectURL(blob)
        let a = document.createElement('a')
        a.setAttribute('href', url)
        a.setAttribute('download', `${record.weituoNo}委托单.docx`)
        a.click()
      })
    },
    
      // handlewcChange(data){

      // var weituoClient={weituoClient:data}
      // getAction('/weituo/itdmWeituoInfo/weituoNoList', weituoClient).then((res) => {
      //   if (res.success) {
      //     console.log("xxxxxxxxxxxxxxxxxxx")
      //     console.log(res.result)
      //     this.weituoNoList = res.result.map(i=>{
      //       return { value: i.id, text: i.weituoNo}
      //     })
      //     console.log(this.weituoNoList)
      //   }
      // })




      // console.log(data)
      // },
      initDictConfig(){
      },
      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'string',value:'weituoId',text:'委托单id',dictCode:''})
        fieldList.push({type:'string',value:'weituoClient',text:'委托单位名称',dictCode:''})
        fieldList.push({type:'string',value:'sampleName',text:'样品名称',dictCode:''})
        fieldList.push({type:'string',value:'sampleModelSpecification',text:'样品规格/型号',dictCode:''})
        fieldList.push({type:'string',value:'sampleQuantities',text:'样品数量',dictCode:''})
        fieldList.push({type:'string',value:'sampleManufacturer',text:'生产厂家',dictCode:''})
        fieldList.push({type:'string',value:'sampleCode',text:'样品编号',dictCode:''})
        this.superFieldList = fieldList
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>