<template>
  <div>
    <el-dialog append-to-body title="标准名称" :visible.sync="showProcess" width="600px" height="200px">
      <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="stdCode" min-width="100"></el-table-column>
        <el-table-column :show-overflow-tooltip="true" label="标准名称" prop="stdName" min-width="100"></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: ['std', 'stdCodeList'],
  data () {
    return {
      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++) {
        for (let item of this.selection[i]) {
          processCode.push(item.stdCode);
          processDesc.push(item.stdName);
        }
      }
      code = processCode.join(',');
      desc = processDesc.join(',');
      this.$emit('getStd', code, desc);
      this.showProcess = false;
    },
    Cancel () {
      this.showProcess = false;
    },
    findProcess () {
      let _this = this;
      // 一级工序点
      this.axios.post('/ComBaseStdRTestService/doQueryStdGorX', [{
        object: {},
        pageIndex: this.pageIndex,
        pageSize: this.pageSize
      }]).then(function (response) {
        if (response) {
          _this.processData = response.data.data.rows;
          _this.total = response.data.data.total;
          // _this.selection = [];
          if (_this.stdCodeList || _this.selection) {
            _this.$nextTick(function () {
              if (_this.stdCodeList) {
                for (let j = 0; j < _this.processData.length; j++) {
                  if (_this.stdCodeList.indexOf(_this.processData[j].stdCode) >= 0) {
                    _this.$refs.processTab.toggleRowSelection(_this.processData[j], true);
                  }
                }
              }
              if (_this.selection) {
                for (let i = 0; i < _this.processData.length; i++) {
                  for (let item of _this.selection) {
                    if (item.stdCode === _this.processData[i].stdCode) {
                      _this.$refs.processTab.toggleRowSelection(_this.processData[i], true);
                    }
                  }
                }
              }
            });
          }
        }
      });
    }
  },
  watch: {
    std: function () {
      this.findProcess();
      this.showProcess = true;
    }
  }
}
</script>
<style scoped></style>
