<template>
  <div :class="containerClass">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" prop="orderNo" v-if="isShow('orderNo')">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="加工商" prop="workType" v-if="isShow('workType')">
        <el-select v-model="queryParams.workType" placeholder="请选择加工商" clearable @change="handleQuery">
          <el-option
            v-for="dict in dict.type.order_prod_work_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否成品" prop="isEnd" v-if="isShow('isEnd')">
        <el-radio-group v-model="queryParams.isEnd" @change="handleQuery">
          <el-radio :label="null">全部</el-radio>
          <el-radio :label="true">是</el-radio>
          <el-radio :label="false">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="物料编码" prop="materialNo" v-if="isShow('materialNo')">
        <el-input
          v-model="queryParams.materialNo"
          placeholder="请输入物料编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名称" prop="name" v-if="isShow('name')">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规格" prop="spec" v-if="isShow('spec')">
        <el-input
          v-model="queryParams.spec"
          placeholder="请输入规格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      
      <el-form-item label="处理方式" prop="handleWay">
        <el-input
          v-model="queryParams.handleWay"
          placeholder="请输入处理方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="效果" prop="effect">
        <el-input
          v-model="queryParams.effect"
          placeholder="请输入效果"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="颜色" prop="color">
        <el-input
          v-model="queryParams.color"
          placeholder="请输入颜色"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="盖子颜色" prop="coverColor">
        <el-input
          v-model="queryParams.coverColor"
          placeholder="请输入盖子颜色"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 分组表格 -->
    <el-table 
      v-loading="loading" 
      size="mini"
      :data="groupList" 
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column type="expand">
        <template slot-scope="scope">
          <prod-table :data="scope.row.prodList" />
        </template>
      </el-table-column>
      <el-table-column label="产品名称" prop="name" align="left" />
      <el-table-column label="总数量" prop="totalNum" align="center" width="100" />
      <el-table-column label="已上报" prop="reportNum" align="center" width="100" />
      <el-table-column label="已清点" prop="storeNum" align="center" width="100" />
      <el-table-column label="进度" align="center" width="160">
        <template slot-scope="scope">
          <el-progress 
            :percentage="calculateProgress(scope.row)" 
            :color="ProgressColors" 
            :format="ProgressFormat" 
          />
        </template>
      </el-table-column>
    </el-table>

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

<script>
import { listOrderProdGroupByName } from '@/api/bst/orderProd'
import { $showColumns } from '@/utils/mixins'
import { ProgressColors, ProgressFormat } from '@/utils/constants'
import ProdTable from './components/ProdTable.vue'

export default {
  name: "OrderProdGroup",
  mixins: [$showColumns],
  dicts: ['order_prod_work_type'],
  components: { ProdTable },
  props: {
    query: {
      type: Object,
      default: () => ({})
    },
    // 隐藏查询
    hiddenSearch: {
      type: Boolean,
      default: false,
    },
    containerClass: {
      type: String,
      default: "app-container"
    }
  },
  data() {
    return {
      ProgressColors,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 分组数据
      groupList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        orderId: null,
        workType: null,
        isEnd: null,
        materialNo: null,
        name: null,
        spec: null,
        remark: null,
        handleWay: null,
        effect: null,
        color: null,
        coverColor: null,
        orderNo: null
      }
    };
  },
  created() {
    this.queryParams = {
      ...this.queryParams,
      ...this.query,
    }

    this.showSearch = !this.hiddenSearch;
    this.initColumns();
    this.getList();
  },
  methods: {
    ProgressFormat,
    calculateProgress(row) {
      if (!row.totalNum || row.totalNum === 0) {
        return 0;
      }
      return (row.storeNum / row.totalNum) * 100;
    },
    /** 查询订单产品分组列表 */
    getList() {
      this.loading = true;
      listOrderProdGroupByName(this.queryParams).then(response => {
        this.groupList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    }
  }
};
</script>

<style lang="scss" scoped>

</style>