<template>
  <div id="demandSummary">
    <div v-if="isList">
      <el-form class="search_area" label-width="120px">
        <el-row :gutter="10">
          <el-col :span="1.5">
            <el-form-item label="单据状态">
              <el-select v-model="queryParams.code" size="small" style="width: 200px" clearable>
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="1.5">
            <el-form-item label="采购员">
              <el-input
              v-model="queryParams.cgy"
              size="small"
              placeholder=""
              clearable
              style="width: 200px"
              />
            </el-form-item>
          </el-col>
          <el-col :span="1.5">
            <el-form-item label="品类">
              <el-input
              v-model="queryParams.pl"
              size="small"
              placeholder=""
              clearable
              style="width: 200px"
              />
            </el-form-item>
          </el-col>
          <el-col :span="1.5">
            <el-form-item label="生产厂家">
              <el-input
              v-model="queryParams.sccj"
              size="small"
              placeholder=""
              clearable
              style="width: 200px"
              />
            </el-form-item>
          </el-col>
          <el-col :span="1.5">
            <el-form-item label="" label-width="20px">
              <el-button type="primary" size="small" icon="el-icon-search" plain>搜索</el-button>
              <el-button size="small" icon="el-icon-refresh" plain>重置</el-button>
            </el-form-item>
          </el-col>
        </el-row>

        <CollapseTransition>
        <div v-show="expanded">
          <el-row :gutter="10">
            <el-col :span="1.5">
              <el-form-item label="物料类别">
                <el-select v-model="queryParams.wllb" size="small" style="width: 200px" clearable>
                  <el-option
                    v-for="item in options"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="1.5">
              <el-form-item label="周期单位">
                <el-select v-model="queryParams.zqdw" size="small" style="width: 200px" clearable>
                  <el-option
                    v-for="item in options"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="1.5">
              <el-form-item label="业务部门">
                <el-input
                v-model="queryParams.ywbm"
                size="small"
                placeholder=""
                clearable
                style="width: 200px"
                />
              </el-form-item>
            </el-col>
            <el-col :span="1.5">
              <el-form-item label="审核人">
                <el-input
                v-model="queryParams.shr"
                size="small"
                placeholder=""
                clearable
                style="width: 200px"
                />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="10">
            <el-col :span="1.5">
              <el-form-item label="转请购单号">
                <el-input
                v-model="queryParams.zqgdh"
                size="small"
                placeholder=""
                clearable
                style="width: 200px"
                />
              </el-form-item>
            </el-col>
            <el-col :span="1.5">
              <el-form-item label="采购需求单号">
                <el-input
                v-model="queryParams.cgxqdh"
                size="small"
                placeholder=""
                clearable
                style="width: 200px"
                />
              </el-form-item>
            </el-col>
            <el-col :span="1.5">
              <el-form-item label="注册人">
                <el-input
                v-model="queryParams.zcr"
                size="small"
                placeholder=""
                clearable
                style="width: 200px"
                />
              </el-form-item>
            </el-col>
            <el-col :span="1.5">
              <el-form-item label="业务类型">
                <el-select v-model="queryParams.ywlx" size="small" style="width: 200px" clearable>
                  <el-option
                    v-for="item in options"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="10">
            <el-col :span="1.5">
              <el-form-item label="单据来源">
                <el-input
                v-model="queryParams.zqgdh"
                size="small"
                placeholder=""
                clearable
                style="width: 200px"
                />
              </el-form-item>
            </el-col>
            <el-col :span="1.5">
              <el-form-item label="需求客户">
                <el-input
                v-model="queryParams.cgxqdh"
                size="small"
                placeholder=""
                clearable
                style="width: 200px"
                />
              </el-form-item>
            </el-col>
            <el-col :span="1.5">
              <el-form-item label="末级供应仓库">
                <el-input
                v-model="queryParams.zcr"
                size="small"
                placeholder=""
                clearable
                style="width: 200px"
                />
              </el-form-item>
            </el-col>
            <el-col :span="1.5">
              <el-form-item label="末级供应库位">
                <el-select v-model="queryParams.ywlx" size="small" style="width: 200px" clearable>
                  <el-option
                    v-for="item in options"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="10">
            <el-col :span="1.5">
              <el-form-item label="物料编码">
                <el-input
                v-model="queryParams.zqgdh"
                size="small"
                placeholder=""
                clearable
                style="width: 200px"
                />
              </el-form-item>
            </el-col>
            <el-col :span="1.5">
              <el-form-item label="默认采购组织">
                <el-input
                v-model="queryParams.cgxqdh"
                size="small"
                placeholder=""
                clearable
                style="width: 200px"
                />
              </el-form-item>
            </el-col>
            <el-col :span="1.5">
              <el-form-item label="需求日期">
                <el-input
                v-model="queryParams.zcr"
                size="small"
                placeholder=""
                clearable
                style="width: 200px"
                />
              </el-form-item>
            </el-col>
            <el-col :span="1.5">
              <el-form-item label="转请购时间">
                <el-select v-model="queryParams.ywlx" size="small" style="width: 200px" clearable>
                  <el-option
                    v-for="item in options"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="10">
            <el-col :span="1.5">
              <el-form-item label="汇总审核时间">
                <el-input
                v-model="queryParams.zqgdh"
                size="small"
                placeholder=""
                clearable
                style="width: 200px"
                />
              </el-form-item>
            </el-col>
            <el-col :span="1.5">
              <el-form-item label="汇总确认时间">
                <el-input
                v-model="queryParams.cgxqdh"
                size="small"
                placeholder=""
                clearable
                style="width: 200px"
                />
              </el-form-item>
            </el-col>
          </el-row>
        </div>
        </CollapseTransition>
      </el-form>
      <el-divider class="lines"><i style="cursor: pointer;" :class="expanded?'el-icon-arrow-up':'el-icon-arrow-down'" @click="drop"></i></el-divider>
    
      <el-card>
        <div class="btn_grooup">
          <el-button type="primary" size="small">编辑</el-button>
          <el-button type="primary" size="small">保存</el-button>
          <el-button type="primary" size="small">确认</el-button>
          <el-button type="primary" size="small">取消</el-button>
          <el-button type="primary" size="small" @click="audits">审核</el-button>
          <el-button type="primary" size="small">弃审</el-button>

          <el-dropdown size="small" @command="handleCommand">
            <el-button size="small" type="primary" style="margin: 0 10px;">
              导出<i class="el-icon-arrow-down el-icon--right"></i>
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="Excel导出">Excel导出</el-dropdown-item>
              <el-dropdown-item command="导出明细">导出明细</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>

          <el-button type="primary" size="small">转请购</el-button>
        </div>
        <el-table 
          :data="tableList" 
          fit
          show-summary
          max-height="680"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" />
          <el-table-column label="一级品类" align="center" prop="code"/>
          <el-table-column label="物料编码" align="center" prop="code"/>
          <el-table-column label="品名" align="center" prop="code"/>
          <el-table-column label="规格" align="center" prop="code"/>
          <el-table-column label="单位" align="center" prop="demandUnit"/>
          <el-table-column label="生产厂家/代理人" align="center" prop="manufacturer"/>
          <el-table-column label="需求单位数" align="center" prop="code"/>
          <el-table-column label="总最终净需求量" align="center" prop="code"/>
          <el-table-column label="总月销量" align="center" prop="totalMonthlySales"/>
          <el-table-column label="需求可用周期" align="center" prop="demandCycle"/>
          <el-table-column label="呆滞量" align="center" prop="dullQut"/>
          <el-table-column label="采购周期" align="center" prop="buyPeriod"/>
          <el-table-column label="中心公共库存" align="center" prop="centralPublicStock"/>
          <el-table-column label="中心仓专属货位" align="center" prop="centralWarehouse"/>
          <el-table-column label="区域分仓公共库存" align="center" prop="regionPublicStock"/>
          <el-table-column label="各项目仓库存" align="center" prop="eachWarehouseStock"/>
          <el-table-column label="电商仓库" align="center" prop="commerceWarehouse"/>
          <el-table-column label="采购在途" align="center" prop="buyTransit"/>
          <el-table-column label="借出在途" align="center" prop="lendTransit"/>
          <el-table-column label="调拨在途" align="center" prop="transferTransit"/>
          <el-table-column label="库存总计" align="center" prop="stockTotal"/>
          <el-table-column label="最小包装量" align="center" prop="minPackage"/>
          <el-table-column label="最小订货量" align="center" prop="minOrder"/>
          <el-table-column label="最小批量" align="center" prop="minBatch"/>
          <el-table-column label="人工调整数" align="center" prop="artificialAdjust"/>
          <el-table-column label="修改原因" align="center" prop="modifyReason"/>
          <el-table-column label="建议采购量" align="center" prop="suggestionPurchase"/>
          <el-table-column label="建议净采购量" align="center" prop="suggestBuyQty"/>
          <el-table-column label="最终采购量" align="center" prop="finalBuyQty"/>
          <el-table-column label="二级品类" align="center" prop="code"/>
          <el-table-column label="三级品类" align="center" prop="code"/>
          <el-table-column label="四级品类" align="center" prop="code"/>
          <el-table-column label="单据状态" align="center" prop="status"/>
          <el-table-column label="采购员" align="center" prop="buyerName"/>
          <el-table-column label="默认采购组织" align="center" prop="code"/>
          <el-table-column label="有效期" align="center" prop="validityPeriod"/>
          <el-table-column label="有效期单位" align="center" prop="validityPeriodUnit"/>
          <el-table-column label="业务类型" align="center" prop="businessType"/>
          <el-table-column label="安全库存量" align="center" prop="code"/>
          <el-table-column label="单据来源" align="center" prop="billSource"/>
          <el-table-column label="行号" align="center" prop="rowNo"/>
          <el-table-column label="注册人" align="center" prop="code"/>
          <el-table-column label="可用量" align="center" prop="qty"/>
          <el-table-column label="总需与终采差异" align="center" prop="buyDiscrepancy"/>
          <el-table-column label="集团预测分类" align="center" prop="forecastClassification"/>
          <el-table-column label="中心仓占有量" align="center" prop="centerBinPossession"/>
          <el-table-column label="中心仓可用量" align="center" prop="code"/>
          <el-table-column label="物料类别" align="center" prop="materialCategory"/>
          <el-table-column label="业务部门" align="center" prop="departmentName"/>
          <el-table-column label="需求单位" align="center" prop="demandUnit"/>
          <el-table-column label="采购经理审核人" align="center" prop="puManagerAuditor"/>
          <el-table-column
          fixed="right"
          label="操作"
          align="center"
          width="150"
          >
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="check(scope.row)">查看</el-button>
          </template>
        </el-table-column>
        </el-table>

        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="1"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="100"
          layout="total, sizes, prev, pager, next, jumper"
          :total=total>
        </el-pagination>
      </el-card>
    </div>

    <Add v-model="isList" v-if="!isList" :pageStu="page" :disable="disable" :row="rowDetail" @refresh="getList"/>
  </div>
</template>

<script>
import Add from './add.vue'
import CollapseTransition from '@/components/MyCollapse/collapse.vue'
import {getSummaryList, auditSummary} from '@/api/purchase/DemandSummary.js'
export default {
  name: 'demandSummary',
  components: {
    Add,
    CollapseTransition
  },
  data() {
    return {
      expanded: false,
      // 页面配置
      isList: true,
      // 页面状态
      page: '',
      queryParams: {
        code: '',
        cgy: '',
        pl: '',
        sccj: '',
        wllb: '',
        zqdw: '',
        ywbm: '',
        shr: '',
        zqgdh: '',
        cgxqdh: '',
        zcr: '',
        ywlx: '',
        pageNum: 1,
        pageSize: 10
      },
      options: [{
        value: '1', label: '是',
      }, {
        value: '0', label: '否'
      }],
      tableList: [],
      total: 0,
      rowDetail: {},
      disable: false,
      ids: []
    }
  },
  created() {
    this.getList(this.queryParams)
  },
  methods: {
    getList(params){
      getSummaryList(params).then(res => {
        if (res.code === 200) {
          this.tableList = res.rows
          this.total = res.total
        }
      })
    },
    handleSelectionChange(selection) {
      console.log('选中', selection)
      this.ids = selection.map(item => item.demandItemId)
      console.log('选中数组', this.ids.join())
    },
    handleCommand(command) {
      alert(command)
    },
    check(row) {
      this.isList = false
      this.page = 'check'
      this.rowDetail = row
      this.disable = true
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.queryParams.pageSize = val
      this.getList(this.queryParams)
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.queryParams.pageNum = val
      this.getList(this.queryParams)
    },
    drop() {
      this.expanded = !this.expanded
    },
    audits() {
      if(this.ids.length == 0) {
        this.$modal.msgWarning("请选中至少一条数据");
      } else {
        let param = this.ids.join()
        auditSummary(param).then(res => {
          if (res.code === 200) {
            this.$modal.msgSuccess("审核成功");
            this.getList(this.queryParams)
          }
        })
      }
    },
  }
}
</script>

<style lang="scss" scoped>
#demandSummary {
  height: calc(100vh - 84px);
  padding: 12px;
  box-sizing: border-box;
}
.btn_grooup {
  margin-bottom: 10px;
  display: flex;
  justify-content: flex-end;
}
.lines {
  margin-top: 0;
}
</style>