<template>
  <div>
    <el-dialog append-to-body title="产品信息" :visible.sync="showProcess" width="800px">
      <el-table height="580" 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="prodName" min-width="120"></el-table-column>
        <el-table-column :show-overflow-tooltip="true" label="产品简称" prop="prodAbbr" min-width="120"></el-table-column>
        <el-table-column :show-overflow-tooltip="true" label="英文名称" prop="prodEng" min-width="120"></el-table-column>
        <el-table-column :show-overflow-tooltip="true" label="产品大类" prop="prodLine" min-width="120"></el-table-column>
        <el-table-column :show-overflow-tooltip="true" label="备注" prop="memo" min-width="120"></el-table-column>
      </el-table>
      <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 {
  /**
   * 传入组件的参数：
   * product: boolean, watch到product的变化之后执行一些操作
   * productCodeList: 一个字符串数组，表示弹框中默认勾选的行
   * */
  /**
   * 组件向外返回的数据：
   * this.$emit('getProduct', code, desc);
   * code: 字符串，组件表格中所勾选行的主键，以英文逗号分隔
   * desc: 字符串，组件表格中所勾选行的名称，以英文逗号分隔
   * */
  props: ['product', 'productCodeList'],
  data () {
    return {
      selection: [],
      showProcess: false,
      processData: []
    }
  },
  methods: {
    handleSelect (selection) {
      this.selection = selection;
    },
    OK () {
      let processCode = [], processDesc = [], code = '', desc = '';
      for (let item of this.selection) {
        processCode.push(item.prodCode);
        processDesc.push(item.prodName);
      }
      code = processCode.join(',');
      desc = processDesc.join(',');
      this.$emit('getProduct', code, desc);
      this.showProcess = false;
    },
    Cancel () {
      this.showProcess = false;
    },
    findProcess () {
      let _this = this;
      // 产品名称
      this.axios.post('market.Qms.com.service.BaseProductService/GetProduct', [{model: {prodName: '', validflag: '1'}}]).then(function (response) {
        if (response) {
          _this.processData = response.data.result;
          _this.selection = [];
          if (_this.productCodeList) {
            _this.$nextTick(function () {
              for (let j = 0; j < _this.processData.length; j++) {
                if (_this.productCodeList.indexOf(_this.processData[j].prodCode) >= 0) {
                  _this.$refs.processTab.toggleRowSelection(_this.processData[j], true);
                }
              }
            });
          }
        }
      });
    }
  },
  watch: {
    product: function () {
      this.findProcess();
      this.showProcess = true;
    }
  }
}
</script>
<style scoped></style>
