<template>
  <div>
    <el-dialog append-to-body title="交货状态" :visible.sync="showProcess" width="600px">
      <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="deliveryStateCode" min-width="120"></el-table-column>
        <el-table-column :show-overflow-tooltip="true" label="交货状态" prop="deliveryStateDesc" 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 {
  /**
   * delivery: 如果这个变量发生变化就显示弹框
   * deliveryCodeList: 已经勾选的交货状态，以英文逗号分隔的字符串
   * delivery_psc: 产品码，如果有值则查询交货状态与产品码关联表，反之则查询交货状态表
   * */
  props: ['delivery', 'deliveryCodeList', 'delivery_psc'],
  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.deliveryStateCode);
        processDesc.push(item.deliveryStateDesc);
      }
      code = processCode.join(',');
      desc = processDesc.join(',');
      this.$emit('getDelivery', code, desc);
      this.showProcess = false;
    },
    Cancel () {
      this.showProcess = false;
    },
    findProcess () {
      let _this = this;
      // 交货状态
      this.axios.post('market.Qms.com.service.BaseDeliverystateService/GetDeliveryState', [{model: {deliveryStateCode: '', validflag: '1'}}]).then(function (response) {
        if (response) {
          _this.processData = response.data.result;
          _this.selection = [];
          if (_this.deliveryCodeList) {
            _this.$nextTick(function () {
              for (let j = 0; j < _this.processData.length; j++) {
                if (_this.deliveryCodeList.indexOf(_this.processData[j].deliveryStateCode) >= 0) {
                  _this.$refs.processTab.toggleRowSelection(_this.processData[j], true);
                }
              }
            });
          }
        }
      });
    },
    findProcessPsc () {
      let _this = this;
      // 交货状态
      this.axios.post('/QmsApiService/GetPscDelivery', [{object: {psc: this.delivery_psc}}]).then(function (response) {
        if (response) {
          _this.processData = response.data.data;
          _this.selection = [];
          if (_this.deliveryCodeList) {
            _this.$nextTick(function () {
              for (let j = 0; j < _this.processData.length; j++) {
                if (_this.deliveryCodeList.indexOf(_this.processData[j].deliveryStateCode) >= 0) {
                  _this.$refs.processTab.toggleRowSelection(_this.processData[j], true);
                }
              }
            });
          }
        }
      });
    }
  },
  watch: {
    delivery: function () {
      if (this.delivery_psc) {
        this.findProcessPsc();
      } else {
        this.findProcess();
      }
      this.showProcess = true;
    }
  }
}
</script>
<style scoped></style>
