<template>
  <div>
    <el-card class="box-card">
      <!-- 搜索区域 -->
      <div class="search-bx1">
        <el-select v-model="searchData.class_id" filterable placeholder="班次" class="g-mr20  g-mb20" style="width: 180px;" clearable>
          <el-option v-for="item in enumList.class_list" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
        <el-input v-model="searchData.feed_vehicle_name" placeholder="饲喂车名称"  style="width: 180px;" class="g-mr20" clearable />
        <el-input v-model="searchData.formula_name" placeholder="预设饲喂模板"  style="width: 180px;" class="g-mr20" clearable />
        <el-select v-model="searchData.status" filterable placeholder="车次状态" class="g-mr20" style="width: 180px;" clearable>
          <el-option v-for="item in statusList" :key="item.id" :label="item.value" :value="item.id" />
        </el-select>
        <el-input v-model="searchData.barn_group_name" placeholder="饲喂栏舍组"  style="width: 180px;" class="g-mr20" clearable />
        <el-input v-model="searchData.barn_name" placeholder="饲喂栏舍"  style="width: 180px;" class="g-mr20" clearable />
        <el-button type="primary"   @click="form_search">搜索</el-button>
        <el-button type="primary"   @click="form_clear">重置</el-button>
        <el-button type="primary" v-if="isButtonEdit"  @click="form_add">添加车次</el-button>
         <!-- <el-button type="primary"   @click="form_export">导出</el-button> -->
      </div>
      <!-- 表格 -->
      <el-table   key="0"  :data="tableList"  v-loading="tableLoading" element-loading-text="给我一点时间"    border fit>
        <el-table-column label="序号"  width="50px" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.id }}</span>
          </template>
        </el-table-column>
        <el-table-column label="饲喂计划名称" min-width="150px" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column label="车次" min-width="150px" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.bus_number }}</span>
          </template>
        </el-table-column>
        <el-table-column label="班次" min-width="150px" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.class_number }}</span>
          </template>
        </el-table-column>
        <el-table-column label="饲喂车名称" min-width="150px" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.feed_vehicle_name }}</span>
          </template>
        </el-table-column>
        <el-table-column label="容量(kg)" min-width="150px" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.capacity }}</span>
          </template>
        </el-table-column>

        <el-table-column label="日龄饲喂模板" min-width="150px" align="center">
          <template slot-scope="scope">
             <el-button type="primary" size="mini"  @click="form_record_see(scope.row)">{{ scope.row.formula_name }}</el-button>
          </template>
        </el-table-column>
        <el-table-column label="饲喂时间" min-width="150px" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.feed_time }}</span>
          </template>
        </el-table-column>
        <el-table-column label="饲喂栏舍组" min-width="150px" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.barn_group_name }}</span>
          </template>
        </el-table-column>
        <el-table-column label="饲喂栏舍" min-width="150px" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.barn_name }}</span>
          </template>
        </el-table-column>
        <el-table-column label="犊牛数量" min-width="150px" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.calf_count }}</span>
          </template>
        </el-table-column>
        <el-table-column label="今日计划饲喂量(kg)" min-width="150px" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.today_plan_feed_number }}</span>
          </template>
        </el-table-column>
        <el-table-column label="明日计划饲喂量(kg)" min-width="150px" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.tomorrow_plan_feed_number }}</span>
          </template>
        </el-table-column>

        <el-table-column  label="车次状态"  min-width="100px" align="center">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.status" active-color="#13ce66" inactive-color="#ff4949" :active-value="1" :inactive-value="2" @change="change_status(scope.row)"  />
          </template>
        </el-table-column>

        <el-table-column label="操作" align="center" width="300" class-name="small-padding fixed-width" fixed="right">
          <template slot-scope="scope">
             <!-- 修改按钮 -->
             <el-button type="primary" v-if="isButtonEdit" size="mini" icon="el-icon-edit" @click="form_edit(scope.row)">修改</el-button>
             <!-- 删除按钮 -->
             <el-button type="danger" v-if="isButtonEdit" size="mini" icon="el-icon-delete" @click="form_delete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination  @size-change="change_size_page"  @current-change="change_current_page"  :current-page="searchData.page" :page-sizes="[1,5,10,20,30,50]"  :page-size="searchData.page_size" layout="total, prev, pager, next" :total="searchData.total"/>
    </el-card>


    <!-- 弹窗 新增or修改 -->
    <el-dialog  :title="addFormTxt[addFormStatus]" :visible.sync="addFormShow" @close="add_dialog_close" width="80%">
      <div class="">
        <el-form  ref="addFormRef"  :rules="addFormRules"  :model="addForm"  label-position="right" label-width="140px"  style="width:90%;margin:0 auto 50px">
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="饲喂计划名称：" prop="name">
                <el-input   v-model="addForm.name"  placeholder="饲喂计划名称" style="width: 80%;" ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="日龄饲喂模板：" prop="formula_id">
                <el-select  v-model="addForm.formula_id" filterable placeholder="请选择" class="filter-item" style="width: 80%;" >
                  <el-option v-for="item in formulaList" :key="item.id" :label="item.name" :value="item.id" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="班次：" prop="class_number">
                <el-select  v-model="addForm.class_number"    filterable placeholder="请选择" class="filter-item" style="width: 80%;" >
                  <el-option v-for="item in enumList.class_list" :key="item.value" :label="item.label" :value="item.value" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="饲喂车：" prop="feed_vehicle_id">
                <el-select  v-model="addForm.feed_vehicle_id" filterable placeholder="请选择" class="filter-item" style="width: 80%;" >
                  <el-option v-for="item in feedVehicleList" :key="item.id" :label="item.name" :value="item.id" />
                </el-select>
              </el-form-item>
            </el-col>
            </el-row>
            <el-row :gutter="20">
                <el-col :span="12">
                  <el-form-item label="饲喂时间：" prop="feed_time">
                    <el-time-picker v-model="addForm.feed_time" format='HH:mm:ss' type="time" placeholder="选择日期"   style="width: 80%;" :clearable="false"   />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="车次状态：" prop="status">
                    <el-switch v-model="addForm.status" active-color="#13ce66" inactive-color="#ff4949" :active-value="1" :inactive-value="2"   />
                  </el-form-item>
                </el-col>

            </el-row>
            <el-row :gutter="20">
              <el-col :span="12">


                <!-- <el-checkbox  >备选项</el-checkbox> -->


                <el-table ref="multipleTable1"  row-key="id"  :data="barnGroupTableList"  element-loading-text="给我一点时间"  highlight-current-row  @selection-change="change_barn_group_selection"   border fit >
                  <el-table-column   type="selection"    width="55">  </el-table-column>
                  <el-table-column label="栏舍组名称" min-width="150px" align="center">
                    <template slot-scope="scope">
                      <span>{{ scope.row.name }}</span>
                    </template>
                  </el-table-column>
                  <el-table-column label="犊牛数量" min-width="150px" align="center">
                    <template slot-scope="scope">
                      <span >{{scope.row.number}}</span>

                    </template>
                  </el-table-column>
                </el-table>
              </el-col>
              <el-col :span="12">
                <el-table  ref="multipleTable2"  row-key="id"  :data="barnTableList"  element-loading-text="给我一点时间"  highlight-current-row  @selection-change="change_barn_selection"
                 border fit >
                  <el-table-column   type="selection" :reserve-selection=true  width="55">  </el-table-column>
                  <el-table-column label="栏舍名称" min-width="150px" align="center">
                    <template slot-scope="scope">
                      <span>{{ scope.row.name }}</span>
                    </template>
                  </el-table-column>
                  <el-table-column label="犊牛数量" min-width="150px" align="center">

                    <template slot-scope="scope">

                      <span v-if ="scope.row.status == 4">0</span>
                      <span v-else>1</span>
                      <!-- <span>{{ 1 }}</span> -->
                    </template>
                  </el-table-column>
                </el-table>
              </el-col>
            </el-row>

        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="addFormStatus==='create'?add_dialog_save():edit_dialog_save()">确认</el-button>
          <el-button @click="addFormShow = false">关闭</el-button>
        </div>
      </div>
    </el-dialog>


    <!-- 查看记录 -->
    <el-dialog   title="" :visible.sync="seeFormShow" @close="see_dialog_close" width="80%">
      <div class="">
         <h2 style="float:left">模板名称：{{tableListTxt2}}</h2>


        <el-table   key="0"  :data="tableList2"  v-loading="tableLoading2" element-loading-text="给我一点时间"    border fit>
              <el-table-column label="阶段"  width="50px" align="center">
                <template slot-scope="scope">
                  <span>{{ scope.row.id }}</span>
                </template>
              </el-table-column>
              <el-table-column label="开始日龄" min-width="150px" align="center">
                <template slot-scope="scope">
                  <span>{{ scope.row.start_daily_age }}</span>
                </template>
              </el-table-column>
              <el-table-column label="结束日龄" min-width="150px" align="center">
                <template slot-scope="scope">
                  <span>{{ scope.row.end_daily_age }}</span>
                </template>
              </el-table-column>
              <el-table-column label="饲料类型" min-width="150px" align="center">
                <template slot-scope="scope">
                  <span>{{ scope.row.forage_name }}</span>
                </template>
              </el-table-column>
              <el-table-column label="日饲喂量(kg)" min-width="150px" align="center">
                <template slot-scope="scope">
                  <span>{{ scope.row.daily_feed_number }}</span>
                </template>
              </el-table-column>
              <el-table-column label="第一班占比(%)" min-width="150px" align="center">
                <template slot-scope="scope">
                  <span>{{ scope.row.first_class_ratio }}</span>
                </template>
              </el-table-column>
              <el-table-column label="第二班占比(%)" min-width="150px" align="center">
                <template slot-scope="scope">
                  <span>{{ scope.row.second_class_ratio }}</span>
                </template>
              </el-table-column>
              <el-table-column label="第三班占比(%)" min-width="150px" align="center">
                <template slot-scope="scope">
                  <span>{{ scope.row.third_class_ratio }}</span>
                </template>
              </el-table-column>
              <el-table-column label="第四班占比(%)" min-width="150px" align="center">
                <template slot-scope="scope">
                  <span>{{ scope.row.fourth_class_ratio }}</span>
                </template>
              </el-table-column>
              <el-table-column label="班次允许误差(kg)" min-width="150px" align="center">
                <template slot-scope="scope">
                  <span>{{ scope.row.allow_class_error }}</span>
                </template>
              </el-table-column>
              <el-table-column label="饲喂温度℃" min-width="150px" align="center">
                <template slot-scope="scope">
                  <span>{{ scope.row.feed_temp }}</span>
                </template>
              </el-table-column>
              <el-table-column label="允许温差℃" min-width="150px" align="center">
                <template slot-scope="scope">
                  <span>{{ scope.row.allow_temp_error }}</span>
                </template>
              </el-table-column>



        </el-table>



        <div slot="footer" class="dialog-footer">
          <el-button @click="seeFormShow = false">关闭</el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {  ajaxDataGet, ajaxDataPost, ajaxDataPut, ajaxDataDelete, checkButtons} from '@/api/common'
import { parseTime, json2excel } from '@/utils/index.js'
export default {
  data() {
    return {
      //接口 - 获取 - 表格
      url_get_table:'/api/v1/ops/formula_plan/list',
      //接口 - 新增 - 表格
      url_add_table:'/api/v1/ops/formula_plan/add',
      //接口 - 修改 - 表格
      url_edit_table:'/api/v1/ops/formula_plan/update',
      //接口 - 删除 - 表格
      url_delete_table:'/api/v1/ops/formula_plan',
      //接口 - 下拉框 列表
      url_get_select1:'/api/v1/ops/calf_feed/enum/list',
      //接口 - 下拉框 列表 栏舍组
      url_get_select2:'/api/v1/ops/barn_group/list?page=1&page_size=1000',
      // 接口 - 车次状态
      url_status: '/api/v1/ops/formula_plan/status/',
      //按钮权限
      isButtonEdit:false,
      //获取 - 表格数据 - 参数
      searchData:{
        class_id: undefined,
        feed_vehicle_name: "",
        formula_name: "",
        barn_name:"",
        barn_group_name:"",
        status:undefined,
        page: 1,  //页码
        page_size: 10,   //每页数量
        total:0,  //总页数
        pastureId: ""
      },
      tableLoading: false,
      //表格内容
      tableList:[
        //  { username:"admin", id:100, date:'2023-12-01',email:'测试信息1123',roleList:[2,3] },
        // { username:"epans", id:200, date:'2023-12-01',email:'测试信息1123',roleList:[] },
        // { username:"derek", id:300, date:'2023-12-01',email:'测试信息1123' },
        // { username:"admin", id:100, date:'2023-12-01',email:'测试信息1123' },
        // { username:"epans", id:200, date:'2023-12-01',email:'测试信息1123' },
        // { username:"derek", id:300, date:'2023-12-01',email:'测试信息1123' },
        // { username:"admin", id:100, date:'2023-12-01',email:'测试信息1123' },
        // { username:"epans", id:200, date:'2023-12-01',email:'测试信息1123' },
        // { username:"derek", id:300, date:'2023-12-01',email:'测试信息1123' },
        // { username:"admin", id:100, date:'2023-12-01',email:'测试信息1123' },
        // { username:"epans", id:200, date:'2023-12-01',email:'测试信息1123' },
        // { username:"derek", id:300, date:'2023-12-01',email:'测试信息1123' },
      ],

      //日龄饲喂模板
      formulaList:[],

      // 饲喂车列表
      feedVehicleList:[],

      sexList:[
        {id: 1, value: '公'},
        {id: 2, value: '母'},
      ],

      statusList:[
        {id: 1, value: '开启'},
        {id: 2, value: '关闭'},
      ],
      enumList: {
        barn_status:[],
        category_type:[],
        calf_genders:[],
        class_list:[],
        event_type:[],
        event_source:[],
      },
      addFormShow:false,
      addFormStatus: '',
      addFormTxt: {  edit: '修改',  create: '新增' },
      addForm:{
        name:'',  id:undefined ,formula_id:undefined ,class_number:undefined ,feed_vehicle_id:undefined ,status:undefined ,
        feed_time: parseTime(new Date(), '{y}-{m}-{d} {h}:{i}:{s}'),
      },



      addFormRules:{
        name: [ { required: true, message: '饲喂计划名称必填', trigger: 'blur' },  ],
        formula_id: [ { required: true, message: '日龄饲喂模板必填', trigger: 'blur' },  ],
        class_number: [ { required: true, message: '班次必填', trigger: 'blur' },  ],
        feed_vehicle_id: [ { required: true, message: '饲喂车必填', trigger: 'blur' },  ],
        status: [ { required: true, message: '车次状态必填', trigger: 'blur' },  ],
        feed_time: [ { required: true, message: '饲喂时间必填', trigger: 'blur' },  ],
      },

      barnGroupTableList:[],
      barnTableList:[],

      barnIdsArrAll:[],

      barnGroupArr:[],
      barnArr:[],

      barnGroupIdsArr:[],
      barnIdsArr:[],




      tableLoading2: false,
      //表格内容
      tableListTxt2:'',
      tableList2:[],
      seeFormShow:false,
      seeFormStatus: '',
      seeFormTxt: {  edit: '修改',  create: '新增' },
      seeForm:{
        name:'',id:''
      },

    }
  },
  created(){

    //获取按钮权限
    const isButtonEdit = checkButtons(this.$store.state.buttonsList, "饲喂计划编辑")
      this.isButtonEdit = isButtonEdit
      console.log('this.isButtonEdit==========',this.isButtonEdit)



    //获取下拉框
    this.get_select_list1()

    //表格 - 初始化
    this.get_table_data()

  },
  methods:{
    //车次状态
    change_status(row){
      var me = this
      //加载 - 保存
      ajaxDataPut(me.url_status +row.id, {}).then(e => {
        console.log("更改结果：",e)
        //打印请求成功结果
        if(e.code == 200  ){
          me.$message({ type: 'success',  message: '设置成功!'  });
          me.get_table_data()
        } else {
          me.$message({ type: 'error', message: '设置失败！' + e.msg, duration: 2000 })
        }
      })
    },



    //获取 下拉框
    get_select_list1(){
      var me = this
      ajaxDataPost('/api/v1/ops/formula_plan/formula/list?page=1&page_size=1000', {name:""}).then(e => {
        if(e.data.list === null || e.data.list === undefined ||e.data.list.length === 0   ){
          me.formulaList = []
        } else {
          me.formulaList = e.data.list
        }
      })

      ajaxDataPost('api/v1/ops/base_setting/feed_vehicle/list?page=1&page_size=1000',{name:""}).then(e=>{
        if(e.code === 200 && e.data.list.length > 0) {
          me.feedVehicleList = e.data.list
        }
      })

      // 班次下拉框
      ajaxDataGet('/api/v1/ops/calf_feed/enum/list').then(e => {
        if(e.code === 200){
          me.enumList = e.data
        }
      })

      //栏舍组表格
      ajaxDataPost('/api/v1/ops/barn_group/list?page=1&page_size=1000', {name:""}).then(e => {
        if(e.code === 200 ){
          me.barnGroupTableList = e.data.list
        }
      })
    },


    //获取 表格
    get_table_data() {
      var me = this
      me.tableLoading = true

      if( me.searchData.class_id == ""){
        me.searchData.class_id = undefined
      }
      if( me.searchData.status == ""){
        me.searchData.status = undefined
      }


      var send_url = me.url_get_table + '?page=' + me.searchData.page +  '&page_size=' + me.searchData.page_size
      ajaxDataPost(send_url,  me.searchData).then(e => {
        if(e.code === 200 ){
          me.tableList = e.data.list
          me.searchData.total = e.data.total;	// 总数
          me.searchData.page = e.data.page;	//页码
          me.searchData.page_size = e.data.page_size;	//每页数量
        } else {
          me.$message({ type: 'error', message: '请求表格列表失败！' + e.msg, duration: 2000 })
        }
        setTimeout(() => {
          me.tableLoading = false
        }, 100)
      })
    },
    //搜索 查询表格
    form_search(){
      var me = this
      console.log('请求表格列表searchData',me.searchData)
      me.get_table_data()
    },

    //重置 表格
    form_clear(){
      var me = this
      me.searchData.class_id = undefined
      me.searchData.feed_vehicle_name = ""
      me.searchData.formula_name = ""
      me.searchData.barn_name = ""
      me.searchData.barn_group_name = ""
      me.searchData.status = undefined
      me.searchData.page = 1
      me.searchData.page_size = 10


      me.get_table_data()
    },

    //监听 page_size 改变的事件
    change_size_page(item){
      console.log(item)
     // this.searchData.page_size = item
      //  this.getUserList()
    },

    //监听 page 改变的事件
    change_current_page(item){
      console.log(item)
       this.searchData.page = item
       this.get_table_data()
    },

    //清空弹窗表单信息
    reset_form(){
      this.addForm.name = ''
      this.addForm.id = undefined
      this.addForm.formula_id = undefined
      this.addForm.class_number = undefined
      this.addForm.feed_vehicle_id = undefined
      this.addForm.status = undefined
      this.addForm.feed_time = parseTime(new Date(), '{y}-{m}-{d} {h}:{i}:{s}')

      this.addForm.number = ''






    },

    //查看记录
    form_record_see(row) {
      var me = this
      console.log('行内容row=========', row)
      //编辑行内容赋值
      this.seeForm = Object.assign({}, row)

      this.seeFormShow = true

      ajaxDataGet('/api/v1/ops/formula_plan/formula/'+row.formula_id, {  }).then(e => {
          console.log("日龄饲喂模板：",e)

          //打印请求成功结果
          if(e.data.list == null || e.data.list == undefined ||e.data.list.length == 0   ){
            me.tableList2 = []
          } else {
            me.tableListTxt2 = e.data.name
            me.tableList2 = e.data.list
          }

        })

    },

    //新增关闭
    see_dialog_close(){
       //内容重置
       this.seeFormShow = false
     },

    //新增
    form_add() {



      this.barnGroupArr = []
      this.barnArr = []
      this.barnGroupIdsArr = []
      this.barnIdsArr = []
      this.barnTableList = []

      this.addFormStatus = 'create'
      this.addFormShow = true

      this.$nextTick(() => {
        this.$refs['addFormRef'].clearValidate()
        this.$refs.multipleTable1.clearSelection()
         this.$refs.multipleTable2.clearSelection()
      })



      this.reset_form()




    },



    //编辑
    form_edit(row) {
      var me = this




      this.reset_form()

      this.barnGroupArr = []
      this.barnArr = []
      this.barnGroupIdsArr = []
      this.barnIdsArr = []
      this.barnTableList = []








      //编辑行内容赋值
      this.addForm = Object.assign({}, row)

      var feed_time = "Sat Oct 07 2023 "+row.feed_time+" GMT+0800 (中国标准时间)"
      this.addForm.feed_time = new Date(feed_time)
      console.log( this.addForm.feed_time)
      var barnGroupIdsArr = row.barn_group_ids
      var barnIdsArr = row.barn_ids

      var barnTableList = []
      me.barnGroupIdsArr = barnGroupIdsArr

      me.barnGroupIdsArr = barnGroupIdsArr
      me.barnIdsArr = barnIdsArr
      me.addFormStatus = 'edit'
      me.addFormShow = true



      this.$nextTick(() => {
        this.$refs['addFormRef'].clearValidate()
        // this.$refs.multipleTable1.clearSelection()
        //  this.$refs.multipleTable2.clearSelection()
      })
      console.log('  me.barnGroupTableList==========',me.barnGroupTableList)
      console.log('  barnGroupIdsArr==========',barnGroupIdsArr)

      console.log('  barnIdsArr==========',barnIdsArr)
       // 查询栏舍组下的所有栏舍数据
       if(barnGroupIdsArr.length > 0) {
          me.$nextTick(() => {

                // for(let i = 0; i<me.barnGroupTableList.length;i++){

                //   for(let j = 0; i<barnGroupIdsArr.length;i++){
                //     if(me.barnGroupTableList[i].id == barnGroupIdsArr[i]){
                //       me.$refs['multipleTable1'].toggleRowSelection(me.barnGroupTableList[i])
                //       console.log("me.barnGroupTableList[i]==========",me.barnGroupTableList[i])
                //     }
                //   }

                // }

                for(let i = 0; i<me.barnGroupTableList.length;i++){
                    for(let j = 0; j<barnGroupIdsArr.length;j++){
                      if(me.barnGroupTableList[i].id == barnGroupIdsArr[j]){
                        me.$refs['multipleTable1'].toggleRowSelection(me.barnGroupTableList[i])
                        console.log("me.barnGroupTableList[i]==========",me.barnGroupTableList[i])
                      }
                    }
                }


                 

                ajaxDataPost('/api/v1/ops/barn/list/ids',{ids:barnGroupIdsArr}).then(e => {
                    if(e.code === 200) {
                      // me.barnTableList = e.data.list
                      console.log('  me.barnTableList==========',me.barnTableList)

                      setTimeout(() => {
                        me.barnTableList = e.data.list

                        console.log('me.barnTableList', me.barnTableList)
                        console.log('barnIdsArr',barnIdsArr)

                          for(let i = 0; i<me.barnTableList.length;i++){
                              for(let j = 0; j< barnIdsArr.length;j++){
                                if(me.barnTableList[i].id ==  barnIdsArr[j]){

                                   console.log(me.barnTableList[i].id)
                                    me.$refs['multipleTable2'].toggleRowSelection(me.barnTableList[i])
                                }
                              }
                          }

                      }, 1000)






                    }
                })



          })





      }





    },

    //新增关闭
    add_dialog_close(){
       //内容重置


      var me = this
      me.$nextTick(() => {
        me.$refs['addFormRef'].clearValidate()
        me.$refs.multipleTable1.clearSelection()
        me.$refs.multipleTable2.clearSelection()
      })

    },

    //新增 保存
    add_dialog_save() {
      var me = this
      this.$refs['addFormRef'].validate(valid => {
        //验证成功
        if (valid) {
          //发起请求
          console.log('新增保存me.addForm',me.addForm)

          console.log('新增保存send_data',send_data)

          console.log('新增保存 barnGroupArr', me.barnGroupArr)
          console.log('新增保存 barnArr', me.barnArr)

          var  barnGroupIdsArr = []
          var  barnIdsArr = []
          var barnArrTemp = []

          me.barnArr.forEach(function(item){
            me.barnGroupArr.forEach(function(items){
              if(item.barn_group_id == items.id){
                barnArrTemp.push(item)
              }
            })
          })

          console.log('新增保存 有组别的栏舍 barnArrTemp', barnArrTemp)
          me.barnGroupArr.forEach(function(items){
            barnGroupIdsArr.push(items.id)
          })
          barnArrTemp.forEach(function(items){
            barnIdsArr.push(items.id)
          })

          var send_data = {

            "name": me.addForm.name,
            "class_number": parseFloat(me.addForm.class_number),
            "formula_id":parseFloat(me.addForm.formula_id),
            "formula_name": me.addForm.formula_name,
            "feed_vehicle_id": parseFloat(me.addForm.feed_vehicle_id) ,
            "feed_vehicle_name": me.addForm.feed_vehicle_name,
            "status": parseFloat(me.addForm.status) ,
            "feed_time": parseTime(me.addForm.feed_time, '{h}:{i}:{s}'),
            "barn_group_ids":barnGroupIdsArr,
            "barn_ids":barnIdsArr,
          }


          //关闭弹窗
         // me.addFormShow = false
         //加载 - 发送新增保存

           ajaxDataPost(me.url_add_table, send_data).then(e => {
              console.log("新增结果：",e)
              //打印请求成功结果
              if(e.code == 200  ){
                me.$message({ type: 'success', message: '新增成功！'  , duration: 2000 })
                //关闭弹窗
                me.addFormShow = false
                me.get_table_data()
              } else {
                me.$message({ type: 'error', message: '新增失败！' + e.msg, duration: 2000 })
              }
            })


        }
      })
    },
    //编辑 保存
    edit_dialog_save(){
      var me = this
      this.$refs['addFormRef'].validate(valid => {
        //验证成功
        if (valid) {
          //发起请求

          var  barnGroupIdsArr = []
          var  barnIdsArr = []
          var barnArrTemp = []

          me.barnArr.forEach(function(item){
            me.barnGroupArr.forEach(function(items){
              if(item.barn_group_id == items.id){
                barnArrTemp.push(item)
              }
            })
          })

          console.log('新增保存 有组别的栏舍 barnArrTemp', barnArrTemp)
          me.barnGroupArr.forEach(function(items){
            barnGroupIdsArr.push(items.id)
          })
          barnArrTemp.forEach(function(items){
            barnIdsArr.push(items.id)
          })

          var send_data = {
            "id": me.addForm.id,
            "name": me.addForm.name,
            "class_number": parseFloat(me.addForm.class_number),
            "formula_id":parseFloat(me.addForm.formula_id),
            "formula_name": me.addForm.formula_name,
            "feed_vehicle_id": parseFloat(me.addForm.feed_vehicle_id) ,
            "feed_vehicle_name": me.addForm.feed_vehicle_name,
            "status": parseFloat(me.addForm.status) ,
            "feed_time": parseTime(me.addForm.feed_time, '{h}:{i}:{s}'),
            "barn_group_ids":barnGroupIdsArr,
            "barn_ids":barnIdsArr,
          }




          console.log('编辑保存 barnGroupArr', me.barnGroupArr)
          console.log('编辑保存 barnArr', me.barnArr)

           // 编辑保存
          console.log("弹窗参数：", send_data)

          //关闭弹窗
          // me.addFormShow = false
          //加载 - 发送新增保存
            ajaxDataPut(me.url_edit_table, send_data).then(e => {
                console.log("编辑结果：",e)
                //打印请求成功结果
                if(e.code == 200  ){
                  me.$message({ type: 'success', message: '编辑成功！'  , duration: 2000 })
                  //关闭弹窗
                  me.addFormShow = false
                  me.get_table_data()
                } else {
                  me.$message({ type: 'error', message: '编辑失败！' + e.msg, duration: 2000 })
                }
            })

        }
      })
    },

    form_delete(row) {
      var me = this

       me.$confirm('是否删除此条内容?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
            console.log("删除的ID参数：", row.id)
            //加载 - 发送新增保存
            ajaxDataDelete(me.url_delete_table + '/' +row.id, {}).then(e => {
                console.log("编辑结果：",e)
                //打印请求成功结果
                if(e.code == 200  ){
                  me.$message({ type: 'success',  message: '删除成功!'  });
                  me.get_table_data()
                } else {
                  me.$message({ type: 'error', message: '删除失败！' + e.msg, duration: 2000 })
                }
            })




        }).catch(() => {
          me.$message({
            type: 'info',
            message: '已取消删除'
          });
        });



    },


    //栏舍组选择
    change_barn_group_selection(val){
      console.log("选中的数组", val)
      var me = this
      me.barnGroupArr = val
      console.log("选中的栏舍组数组", me.barnGroupArr)


     var barnGroupIdsArr = []

      val.forEach(function (item,index) {
        barnGroupIdsArr.push(item.id)

      })
      me.barnGroupIdsArr = barnGroupIdsArr



      // me.$refs.multipleTable2.clearSelection()

      // 查询栏舍组下的所有栏舍数据
      if(barnGroupIdsArr.length > 0) {
        ajaxDataPost('/api/v1/ops/barn/list/ids',{ids:barnGroupIdsArr}).then(e => {
          if(e.code === 200) {
            me.barnTableList = e.data.list

          }
        })
      }
    },



    change_barn_selection(val){
      var me = this
      var barnArr = []
      barnArr = val
      me.barnArr = barnArr
      console.log('栏舍选中的',  me.barnArr)
      // var barnIdsArr = []
      // val.forEach(function (item,index) {
      //   barnIdsArr.push(item.id)

      // })
      // me.barnIdsArr = barnIdsArr
      // console.log('栏舍选中的ID',  me.barnIdsArr)




    },

    form_export() {
      var excelDatas = [
        {
            tHeader: ['表头1', '表头2', '表头3', '表头4',],
            filterVal: ['title1', 'title2', 'title3', 'title4'],
            tableDatas: [
                {title1: "内容1",title2: "内容2",title3: "内容3",title4: "内容4"},
                {title1: "内容11",title2: "内容22",title3: "内容32",title4: "内容44"},
                {title1: "内容111",title2: "内容222",title3: "内容333",title4: "内容444"},
            ],
            sheetName: 'Sheet1'
        }
      ]
      json2excel(excelDatas, '表格大标题', true, 'xlsx')
    }
  }
}
</script>
<!-- 加了scoped，则是仅在此组件里生效 -->
<style scoped>

</style>
