<template>
  <div>
    <el-dialog append-to-body title="牌号信息" :visible.sync="showProcess" width="600px">
      <el-table height="528" ref="processTab" :data="processData" border highlight-current-row @selection-change="handleSelect">
        <el-table-column type="selection" width="50" align="center"></el-table-column>
        <el-table-column :show-overflow-tooltip="true" label="牌号名称" prop="steelName" min-width="100"></el-table-column>
        <el-table-column :show-overflow-tooltip="true" label="牌号类型" prop="steelStyle" min-width="100">
          <template slot-scope="scope">
            <span v-if="scope.row.steelStyle === '1'">钢级</span>
            <span v-else-if="scope.row.steelStyle === '2'">牌号</span>
            <span v-else></span>
          </template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" label="备注" prop="memo" min-width="100"></el-table-column>
      </el-table>
      <div class="std-page">
        <el-pagination
          @size-change="handlePageSize"
          @current-change="handlePageIndex"
          :current-page="pageIndex"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="Cancel">取消</el-button>
        <el-button type="primary" size="small" @click="OK">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  props: ['steel', 'steelCodeList'],
  data () {
    return {
      // selection是一个二维数组，表示分页状态下所勾选的数据，[第一页勾选的数据集合, 第二页勾选的数据集合, ...]
      selection: [],
      showProcess: false,
      processData: [],
      pageIndex: 1,
      pageSize: 20,
      total: 0
    }
  },
  methods: {
    handlePageIndex (pageIndex) {
      this.pageIndex = pageIndex;
      // 查询
      this.findProcess();
    },
    handlePageSize (pageSize) {
      this.pageIndex = 1;
      this.pageSize = pageSize;
      // 查询
      this.findProcess();
    },
    handleSelect (selection) {
      this.selection[this.pageIndex - 1] = selection;
    },
    OK () {
      let processCode = [], processDesc = [], code = '', desc = '';
      for (let i = 0; i < this.selection.length; i++) {
        // 注意这里的selection是一个二维数组 = [第一页勾选的数据集合, 第二页勾选的数据集合, ...]
        if (this.selection[i] && this.selection[i].length > 0) {
          // 如果这一页有勾选的数据
          for (let item of this.selection[i]) {
            processCode.push(item.steelCode);
            processDesc.push(item.steelName);
          }
        }
      }
      code = processCode.join(',');
      desc = processDesc.join(',');
      this.$emit('getSteel', code, desc);
      this.showProcess = false;
    },
    Cancel () {
      this.showProcess = false;
    },
    findProcess () {
      let _this = this;
      this.axios.post('market.Qms.com.service.BaseSteelService/GetSteel', [{
        pageIndex: this.pageIndex,
        pageSize: this.pageSize,
        object: {steelName: '', validflag: '1'}
      }]).then(function (response) {
        if (response) {
          _this.processData = response.data.data.rows;
          _this.total = response.data.data.total;
          // _this.selection = [];
          if (_this.steelCodeList || _this.selection) {
            _this.$nextTick(function () {
              if (_this.steelCodeList) {
                for (let j = 0; j < _this.processData.length; j++) {
                  if (_this.steelCodeList.indexOf(_this.processData[j].steelCode) >= 0) {
                    _this.$refs.processTab.toggleRowSelection(_this.processData[j], true);
                  }
                }
              }
              // 把selection里面勾选的数据拼成字符串，跟查询结果比较
              if (_this.selection && _this.selection.length > 0) {
                let arr = [], str = '';
                for (let i = 0; i < _this.selection.length; i++) {
                  if (_this.selection[i] && _this.selection[i].length > 0) {
                    for (let j = 0; j < _this.selection[i].length; j++) {
                      arr.push(_this.selection[i][j].steelCode);
                    }
                  }
                }
                str = arr.join(',');
                for (let k = 0; k < _this.processData.length; k++) {
                  if (str.indexOf(_this.processData[k].steelCode) >= 0) {
                    _this.$refs.processTab.toggleRowSelection(_this.processData[k], true);
                  }
                }
              }
            });
          }
        }
      });
    }
  },
  watch: {
    steel: function () {
      this.findProcess();
      this.showProcess = true;
    }
  }
}
</script>
<style scoped></style>
