<template>

  <el-dialog  title="子行编码" :visible.sync="visible" append-to-body  @open="open"  v-dialogDrag
    width="1000px" :close-on-click-modal="false" custom-class="common-dialog over-dialog">

      <div class="item-search">
        <div>
          <el-button type="primary" @click="handleSure" size="mini">确认</el-button>
        </div>
      </div>

      <el-table :data="tableData"  ref="tableRef"
        @row-dblclick="rowDblclick"
        height="100%"  size="mini" stripe
        highlight-current-row  >

       <el-table-column label="选择" width="60" >
         <template slot-scope="scope">
           <el-radio v-model="rowVal" :label="scope.row">&nbsp;</el-radio>
         </template>
       </el-table-column>

        <el-table-column
          v-for="(item,index) in columns"
          :key="index"
          :prop="item.prop"
          :formatter="formatter"
          :min-width="item.width"
          :label="item.label"
          :fixed="item.fixed"
          >
        </el-table-column>
      </el-table>

  </el-dialog>
</template>

<script>
export default {
    name: 'dialog-lineCode',
    props: ['projectId'],
    created(){

    },
    methods: {

        open(){
           if(this.projectId){
             this.getSampleProjectInfo();
           }else {
             this.tableData = [];
           }
        },

        getSampleProjectInfo () {

            this.$ajax.post({
                domain: 'crmUrl',
                url: 'projectSample.sampleProjectInfo',
                data: {	projectId: this.projectId},
                requestType: 'body',
                showLoad: true,
                success: function (res) {
                    if (res.errorCode === 200) {
                        this.tableData = res.data.records || [];
                    }
                }.bind(this),
                error: function (res) {
                   this.$message({
                     type: 'error',
                     message: res.body.msg || res.msg
                   })
                }.bind(this)
            })
        },
        formatter(row, column, cellValue, index){
             if(column.property == 'lockFw'){
                 if(row.lockFw === 'Y'){
                   return '是';
                 } else {
                   return '否';
                 }
             }  else {
               return cellValue;
             }
         },
        // 双击选中行
        rowDblclick (row, column, event) {
          this.rowVal = row;
          this.getLineCodeCount(row);
          this.$emit('setLineInfo', this.rowVal, this.lineCodeIndex);
        },
        handleSure(){
          if(!this.rowVal){
            this.$message('请先选择数据！');
            return;
          }
          this.getLineCodeCount(this.rowVal);
          this.$emit('setLineInfo', this.rowVal, this.lineCodeIndex);
        },
        getLineCodeCount(row){
          let array = [];
          array.push({projectLineCode: row.lineCode});
          this.$ajax.post({
              domain: 'crmUrl',
              url: 'sample.byPLineCodeCount',
              data: array,
              requestType: 'body',
              showLoad: true,
              success: function (res) {
                if (res.errorCode === 200) {
                   if(res.data.length) {
                     let tips = '';
                     res.data.forEach(item=>{
                       tips += item.projectLineCode + ' 已申请 ' + item.applyQuantity + '（pcs），';
                     })
                     this.$message({ showClose: true, duration: 6000, message:  tips + '请确认是否再次发起申请'});
                   }
                }
              }.bind(this),
              error: function (res) {
                  this.$message.error(  res.body.msg || res.msg );
              }.bind(this),
             complete: function(){
               this.visible = false;
             }.bind(this),
          })
        }

    },
    data () {
        return {
            visible: false,
            rowVal: '',
            lineCodeIndex: '', //标识哪行 子行编码 打开弹窗
           // 格字段
            columns: [
               {label: '子行编码', prop: 'lineCode', width: '100', fixed: false},
               {label: '细分领域', prop: 'industrySegmentation', width: '100', fixed: false},
               {label: 'PN', prop: 'projectPn', width: '120', fixed: false},
               {label: '等级', prop: 'itemLevel', width: '80', fixed: false},
               {label: '芯片平台', prop: 'platformKey', width: '100', fixed: false},
               {label: '芯片型号', prop: 'chipModel', width: '100', fixed: false},
               {label: '锁定FW', prop: 'lockFw', width: '60', fixed: false},
            ],
            tableData: [],

        }
    },

}
</script>

<style type="text/css" scoped  lang="scss">
.el-dialog {
    .item-search {
      display: flex;
      justify-content: flex-end;
      align-items: center;
      margin-bottom: 10px;

    }
  }
</style>
