<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 :md="6" :sm="12">
            <a-form-item label="设备名称">
              <a-input placeholder="输入设备名称查询" v-model="queryParam.name"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item label="购买日期">
              <a-date-picker
               style="width: 100%"
               placeholder="请选择购买日期"
               v-model="buyTime"
                @change="onEndDateChange"
              />
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item label="状态">
              <a-input placeholder="输入设备状态查询" v-model="queryParam.type"></a-input>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
               <a-col :md="6" :sm="12">
            <a-form-item label="编号">
              <a-input placeholder="输入编号查询" v-model="queryParam.code"></a-input>
            </a-form-item>
          </a-col>
          </template>

          <a-col :md="6" :sm="8">
            <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>

    <!-- 操作按钮区域 -->
    <div class="table-operator" style="border-top: 5px">
      <a-button @click="handleAdd" type="primary" icon="plus">添加</a-button>
       <a-popconfirm title="确定删除吗?" ok-text="是" cancel-text="否" @confirm="handleDelete()">
            <a-button  type="primary" icon="plus">删除</a-button>
        </a-popconfirm>
    </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>已选择&nbsp;<a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项&nbsp;&nbsp;
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

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

        <span slot="operation" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical" />
          <a @click="handleDetail(record)">详情</a>
        </span>


      </a-table>
    </div>
    <!-- 新增框 -->
    <add-material-registration ref='AddMaterialRegistration' @close="handleCl"></add-material-registration>
    <detail-material-registration ref="DetailMaterialRegistration"></detail-material-registration>
   </a-card>
   
</template>

<script>
  
  import {JeecgListMixin} from '@/mixins/JeecgListMixin'
  import AddMaterialRegistration from './modules/AddMaterialRegistration'
  import DetailMaterialRegistration from './modules/DetailMaterialRegistration'
  import JEllipsis from '@/components/jeecg/JEllipsis'
  import {
    managementList,
    managementAdd,
    managementEdit,
    enterpriseEDelete,
    managementListA,
    managementListB,
    managementListC
} from '@api/oa/cd-material-registration'
import moment from 'moment'


  export default {
    name: "MaterialRegistration",
    mixins: [JeecgListMixin],
    components: {
    AddMaterialRegistration,
    DetailMaterialRegistration,
    moment,
    JEllipsis
    },
    data() {
       let ellipsis = (v, l = 10) => <j-ellipsis value={v} length={l} />
      return {
          queryParam:{   //查询条件

          },
          buyTime:'',
          selectedRowKeysL:[],
        //   表头
        columns:[
            {
            title: '编号',
            align: "center",
            dataIndex: 'code',
            width: 120
          },
          {
            title: '设备名称',
            align: "center",
            dataIndex: 'name',
            width: 120
          },
          {
            title: '型号',
            align: "center",
            dataIndex: 'model',
            width: 120
          },
          {
            title: '购买日期',
            align: "center",
            dataIndex: 'buyTime',
            width: 120
          },
          {
            title: '购买数量',
            align: "center",
            dataIndex: 'buyNum',
            width: 120
          },
          {
            title: '单位',
            align: "center",
            dataIndex: 'unit',
            width: 120
          },
          {
            title: '购买金额',
            align: "center",
            dataIndex: 'amount',
            width: 120
          },
          {
            title: '使用者',
            align: "center",
            dataIndex: 'useId',
            width: 120
          },
          {
            title: '状态',
            align: "center",
            dataIndex: 'type',
            width: 120
          },
          {
            title: '供货方',
            align: "center",
            dataIndex: 'supply',
            width: 120
          },
          {
            title: '备注',
            align: "center",
            dataIndex: 'remarks',
            width: 120,
            customRender: (t) => ellipsis(t),
          },
          {
            title: '来源',
            align: "center",
            dataIndex: 'source',
            width: 120
          },
          {
            title: '操作',
            align: "center",
            dataIndex: 'operation',
            scopedSlots: { customRender: 'operation' },
            width: 120
          },
        ],
        pagination:{  }
      }
    },
    computed: {
    
    },
    created(){
        this.getData()
    },
    methods: {
        // 查询
        searchQuery(){
          this.getData()
        },
        //重置
        searchReset(){
          this.queryParam ={}
          this.buyTime = ''
          this.getData()
        },
        // 获取数据
        getData(){
             this.$nextTick(() => {
                managementList(this.queryParam).then(res => {
                    // console.log('>>>>', this.queryParam)
                    if (res.success) {
                       this.dataSource =res.result.records
                       this.pagination = {
                          total: res.result.total,
                           current: res.result.current,
                           pageSize: res.result.size
                        }
                    }else{
                       this.$message.error(res.message);
                    }
                })
            })
        },
         onEndDateChange(value){
           this.queryParam.buyTime =moment(value).format('YYYY-MM-DD');
         },
        // 删除
        handleDelete(){
          if(this.selectedRowKeys.length == 0){
            this.$message.error("请选择要删除的数据");
          }else{
            var id = this.selectedRowKeys.toString()
            enterpriseEDelete({ids:id}).then(res => {
                    // console.log('>>>>', this.queryParam)
                    if (res.success) {
                      this.$message.success('删除成功');
                      this.getData()
                    }else{
                       this.$message.error(res.message);
                    }
                })
          }
          
        },
        // 添加
        handleAdd(){
          this.$refs.AddMaterialRegistration.visible = true
        },
        //编辑
      async  handleEdit(record){
        await  managementListA({id:record.id}).then(res => {
                    // console.log('>>>>', this.queryParam)
                    if (res.success) {
                     this.$refs.AddMaterialRegistration.formState = res.result
                     this.$refs.AddMaterialRegistration.formState.buyTime = moment( res.result.buyTime)
                    }else{
                       this.$message.error(res.message);
                    }
            })
          await   managementListB({id:record.id}).then(res => {
                    // console.log('>>>>', this.queryParam)
                    if (res.success) {
                       res.result.map(item =>[
                       item.state = '0'
                     ])
                     this.$refs.AddMaterialRegistration.List = res.result
                    }else{
                       this.$message.error(res.message);
                    }
            })
          await  managementListC({id:record.id}).then(res => {
                    // console.log('>>>>', this.queryParam)
                    if (res.success) {
                       res.result.map(item =>[
                       item.state = '0'
                     ])
                     this.$refs.AddMaterialRegistration.List1 = res.result
                     this.$refs.AddMaterialRegistration.visible = true
                     this.$refs.AddMaterialRegistration.defult = 'edit'
                    }else{
                       this.$message.error(res.message);
                    }
            })
        },
        //详情
      async  handleDetail(record){
        await  managementListA({id:record.id}).then(res => {
                    // console.log('>>>>', this.queryParam)
                    if (res.success) {
                     this.$refs.DetailMaterialRegistration.formState = res.result
                    }else{
                       this.$message.error(res.message);
                    }
            })
          await   managementListB({id:record.id}).then(res => {
                    // console.log('>>>>', this.queryParam)
                    if (res.success) {
                     this.$refs.DetailMaterialRegistration.List = res.result
                    }else{
                       this.$message.error(res.message);
                    }
            })
          await  managementListC({id:record.id}).then(res => {
                    // console.log('>>>>', this.queryParam)
                    if (res.success) {
                     this.$refs.DetailMaterialRegistration.List1 = res.result
                     this.$refs.DetailMaterialRegistration.visible = true
                      this.$refs.DetailMaterialRegistration.visible = true
                    }else{
                       this.$message.error(res.message);
                    }
            })
        },
      onSelectChange(keys,rows){
           this.selectedRowKeys = keys;
            this.selectedRows = rows;
         },
         // 分页变化时触发
         handleTableChange(pagination, filters, sorter) {
           this.queryParam.pageNo = pagination.current
             this.getData()
          },
       handleCl(){
         this.getData()
       }
    }
     

  }
</script>
<style scoped>
  @import '~@assets/less/common.less'
</style>