<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="70px">
      <gong-zhu @btns='handleQuery'  ref="cdhe"  @input="resetQuery"/>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExportgt"
        >打印</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <div id="jue">
      <el-table height="250" v-loading="loading" :data="pigletcheckList" :span-method="objectSpanMethod" >
        <el-table-column >
        <el-table-column label="品种" align="center" prop="pz"    :formatter="pzpxFormat" />
        <el-table-column label="类型" align="center" prop="lx" />
        <el-table-column label="养殖场名称" align="center" prop="name" width="120px"/>
        </el-table-column>
        <el-table-column >
        <el-table-column label="期初" align="center" prop="qc" />
        <el-table-column label="转入" align="center" prop="zr" />
         <el-table-column label="转出" align="center" prop="zc" />
        </el-table-column>
        <el-table-column label='购入' align="center">
          <el-table-column label="进口" align="center" prop="jk" />
          <el-table-column label="国内购买" align="center" prop="gngm"/>
          <el-table-column label="购入小计" align="center" prop="gmhj" />
        </el-table-column>
        <el-table-column label="销售" align="center">
          <el-table-column label="出口" align="center" prop="ck" />
          <el-table-column label="国内销售" align="center" prop="gnxs" />
          <el-table-column label="场间销售" align="center" prop="cjxs" />
          <el-table-column label="销售淘汰" align="center" prop="xstt" />
        </el-table-column>
        <el-table-column >
        <el-table-column label="屠宰" align="center" prop="tz"/>
         <el-table-column label="死亡" align="center" prop="sw" />
        </el-table-column>
      </el-table>
    </div>


   <!-- <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    /> -->
  </div>
</template>

<script>
import { listPigletcheck, listDept, getPigletcheck, delPigletcheck, addPigletcheck, updatePigletcheck, exportPigletcheck  ,listPighomelist} from "@/api/statistics/boarReport/boarStock";

export default {
  name: "Pigletcheck",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 仔猪育肥猪盘点表格数据
      pigletcheckList: [],
       //猪舍数据
      pighomeList: [],
      //品种品系数据
      pzpxOptions: [],
      //猪只类别
      zzlbOptions: [],
      //商品等级
      spdjOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
        queryParams: {
          condition:{

          }
        },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        createTime: [
          { required: true, message: "create_time不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "update_time不能为空", trigger: "blur" }
        ],
      },
      deptOptions:[],
      optionPropstwo: {
      	value: 'deptId',
      	label: 'deptName',
      	children: 'children',
      	checkStrictly: true
      },
      spanArr:[],
      pos:0
    };
  },
  created() {
    this.queryParams.condition.endTime = this.getset()
    this.getList();
    this.getPighome();
    this.getDicts("pzpx").then(response => {
          this.pzpxOptions = response.data;
        });
    this.getDicts("group_two").then(response => {
          this.zzlbOptions = response.data;
        });
    this.getDicts("group_one").then(response => {
          this.spdjOptions = response.data;
        });
    // this.diertw()
  },
  methods: {
    /** 查询仔猪育肥猪盘点列表 */
    getList() {
      this.loading = true;
      listPigletcheck(this.queryParams).then(response => {
        this.pigletcheckList = response.rows;
        this.total = response.total;
        this.loading = false;
        let pigletcheckList = this.pigletcheckList;
       this.getSpanArr(pigletcheckList)
      });
    },
     /*查询猪舍信息*/
    getPighome(){
      // this.loading = true;
     listPighomelist(this.queryParams).then(response => {
       this.loading = false
        this.pighomeList = response.data;
      });
    },
    getSpanArr(data) {　
    this.spanArr = []
    this.pos = 0
              for (var i = 0; i < data.length; i++) {
                if (i === 0) {
                  this.spanArr.push(1);
                  this.pos = 0
                } else {
                  // 判断当前元素与上一个元素是否相同
                if (data[i].deptName === data[i - 1].deptName) {
                    this.spanArr[this.pos] += 1;
                    this.spanArr.push(0);
                  } else {
                    this.spanArr.push(1);
                    this.pos = i;
                  }
                }
                console.log(this.spanArr)
            }
        },
        // 获取当前时间
        getset(){
           let date = new Date();
              let y = date.getFullYear();
              let m = date.getMonth() + 1;
              let d = date.getDate();
              return y + "-" + m + "-" + d
        },
    //  获取猪舍
    homeNameFormat(row, column) {
      return this.selectHomeDataLabel(this.pighomeList,row.pighomeId);
      },
    zzlbFormat(row, column) {
      return this.selectDictLabel(this.zzlbOptions, row.checkZzlb);
    },
    spdjFormat(row, column) {
      return this.selectDictLabel(this.spdjOptions, row.checkSpdj);
    },
    pzpxFormat(row, column) {
      return this.selectDictLabel(this.pzpxOptions, row.pz);
    },
    // 合并列
    objectSpanMethod({row, column, rowIndex, columnIndex}){
      let tableData = this.pigletcheckList;
      // //rowIndex：行    columnIndex：列
      // let {tableData} = this;
      console.log(row,column, rowIndex, columnIndex)
                     if (columnIndex === 0 ) {
                                 const _row = this.spanArr[rowIndex];
                                 const _col = _row > 0 ? 1 : 0;
                                 console.log(`rowspan:${_row} colspan:${_col}`)
                                 return { // [0,0] 表示这一行不显示， [2,1]表示行的合并数
                                       rowspan: _row,
                                       colspan: _col
                                 }
                               }

    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        checkId: null,
        pighomeId: null,
        homeName: null,
        pigcolumId: null,
        columName: null,
        checkBatch: null,
        checkPzpx: null,
        checkZzlb: null,
        checkSpdj: null,
        checkPd: null,
        checkEndday: null,
        checkPcts: null,
        checkPdzl: null,
        createDeptid: null,
        createDeptname: null,
        status: "0",
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery(data) {
      console.log(data,9876)
      console.log(this.queryParams,987)
     this.queryParams.condition.deptId = data.condition.deptId
     this.queryParams.condition.secondaryGroup = data.condition.secondaryGroup
     this.queryParams.condition.thirdGroup = data.condition.thirdGroup
      this.queryParams.condition.primaryGroup = data.condition.primaryGroup
      this.queryParams.condition.startTime = data.condition.startTime
     if(data.condition.pigPzpx !== undefined && data.condition.pigPzpx !== null){
       this.queryParams.condition.pigPzpx = data.condition.pigPzpx.join(',')
     }
     if(data.condition.endTime !== undefined ){
       this.queryParams.condition.endTime = data.condition.endTime
     }
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery(data) {
      console.log(data,9976)
      console.log(this.queryParams,997)
      this.queryParams.condition.deptId = data.condition.deptId
      this.queryParams.condition.secondaryGroup = data.condition.secondaryGroup
      this.queryParams.condition.thirdGroup = data.condition.thirdGroup
      this.queryParams.condition.startTime = data.condition.startTime
       this.queryParams.condition.primaryGroup = data.condition.primaryGroup
      if(data.condition.pigPzpx !== undefined && data.condition.pigPzpx !== null){
        this.queryParams.condition.pigPzpx = data.condition.pigPzpx.join(',')
      }else{
        this.queryParams.condition.pigPzpx = data.condition.pigPzpx
      }
      // if(data.condition.endTime !== undefined ){
        this.queryParams.condition.endTime = this.getset()
      // }
      this.getList();
      // this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.checkId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加仔猪育肥猪盘点";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const checkId = row.checkId || this.ids
      getPigletcheck(checkId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改仔猪育肥猪盘点";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.checkId != null) {
            updatePigletcheck(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPigletcheck(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const checkIds = row.checkId || this.ids;
      this.$confirm('是否确认删除仔猪育肥猪盘点编号为"' + checkIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delPigletcheck(checkIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出数据?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          // return exportPigletcheck(queryParams);
        }).then(response => {
          // this.download(response.msg);
        })
    },
    chahetwo(data) {
    	console.log(data)
    	// this.queryParams.villageTowns = data[1];
    	// this.queryParams.village = data[2];
    	// this.queryParams.villagerGroup = data[3];
    	this.queryParams.deptId = data[ data.length - 1]
    },
    // 打印
    handleExportgt(){
      this.printExcel("jue")
    }
  }
};
</script>
<style type="text/css" lang="scss">
  .hues{
    .el-form-item--medium .el-form-item__label{
      width: 42px !important;
    }
    .el-form-item--medium .el-form-item__content{
      width: 80% !important;
    }
  }
</style>
