<template>
  <div class="app-container">
    <div style="padding-bottom: 8px">
    <el-select v-model="use_scene" placeholder="请选择使用场景" style="padding-right: 14px">
      <el-option
        v-for="item in options"
        :key="item.value"
        :label="item.label"
        :value="item.value">
      </el-option>
    </el-select>
    <span>
      <el-input placeholder="请输入查询条件" v-model="searchCondition" style="width:250px"/>
      <el-button type="primary" @click="recommend" style="margin-left: 20px">推荐</el-button>
    </span>
    </div>
    <el-table v-loading="loading" :data="componentList" >
      <el-table-column label="元器件名称" align="center" prop="componentName" :show-overflow-tooltip="true"/>
      <el-table-column label="型号规格" align="center" prop="componentModel" :show-overflow-tooltip="true"/>
      <el-table-column label="质量等级" align="center" prop="qualityGrade" :show-overflow-tooltip="true"/>
      <el-table-column label="国产替代型号" align="center" prop="replaceDomesticModel" :show-overflow-tooltip="true"/>
<!--      <el-table-column label="元器件应用" align="center" prop="applications"/>-->
      <el-table-column label="元器件应用" align="center" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{ scope.row.applications }}
        </template>
      </el-table-column>
      <el-table-column label="相关推荐" align="center" :show-overflow-tooltip="true">
        <span>1,2,3...</span>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleDetail(scope.row)"
            v-hasPermi="['biz:component:query']"
          >查看详情<i class="el-icon-more"></i></el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-star-off"
            @click="getevaluationdata(scope.row)"
          >评分</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"

      @pagination="getList"
    />
    <el-dialog title="评分" :visible.sync="showevalatedialog" width="30%">
      <el-rate
        v-model="escore"
        :show-score="true"
        text-color="#ff9900"
        >
      </el-rate>
      <el-input
        type="textarea"
        :rows="2"
        placeholder="请输入备注"
        v-model="eremark">
      </el-input>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showevalatedialog = false">取 消</el-button>
        <el-button type="primary" @click="evaluate">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body @close="closedialog">

      <el-form  style="border:1px solid #C0C0C0;padding: 5px;margin: auto" ref="form" :model="form" :rules="rules" label-width="200px">
        <h2><b>元器件基本信息</b></h2>
        <el-form-item label="元器件名称" prop="componentName">
          <el-input v-model="form.componentName" readonly='true' />
        </el-form-item>
        <el-form-item label="生产厂家" prop="manufacturer">
          <el-input v-model="form.manufacturer" readonly='true' />
        </el-form-item>
        <el-form-item label="型号规格" prop="componentModel">
          <el-input v-model="form.componentModel" readonly='true' />
        </el-form-item>
        <el-form-item label="国产替代型号" prop="replaceDomesticModel">
          <el-input v-model="form.replaceDomesticModel" readonly='true' />
        </el-form-item>
        <el-form-item label="质量等级" prop="qualityGrade">
          <el-input v-model="form.qualityGrade" readonly='true' />
        </el-form-item>
        <el-form-item label="供货周期" prop="deliveryCycle">
          <el-input v-model="form.deliveryCycle" readonly='true' />
        </el-form-item>
        <el-form-item label="供货量(个/年）" prop="supplyQuantity">
          <el-input v-model="form.supplyQuantity" readonly='true' />
        </el-form-item>
        <el-form-item label="价格" prop="unitPrice">
          <el-input v-model="form.unitPrice" readonly='true' />
        </el-form-item>
        <el-form-item label="ZZKK等级" prop="zzkkLevel">
          <el-input v-model="form.zzkkLevel" readonly='true' />
        </el-form-item>
        <el-form-item label="是否在优选目录内" prop="inPreference">
          <el-input v-model="form.inPreference" readonly='true' />
        </el-form-item>
        <el-form-item label="停产断档日期" prop="shutdownDate">
          <el-date-picker clearable
                          v-model="form.shutdownDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          readonly='true'>
          </el-date-picker>
        </el-form-item>
        <el-form-item label="封装形式" prop="encapsulationMode">
          <el-input v-model="form.encapsulationMode" readonly='true' />
        </el-form-item>
        <el-form-item label="最大正向电流IFM" prop="paramIfm" v-if="form.paramIfm!==null && form.paramIfm!==''">
          <el-input v-model="form.paramIfm" readonly='true' />
        </el-form-item>
        <el-form-item label="最大反向工作电压VRRM" prop="paramVrrm" v-if="form.paramVrrm!==null && form.paramVrrm!==''">
          <el-input v-model="form.paramVrrm" readonly='true' />
        </el-form-item>
        <el-form-item label="最大浪涌电流IFSM" prop="paramIfsm" v-if="form.paramIfsm!==null && form.paramIfsm!==''">
          <el-input v-model="form.paramIfsm" readonly='true' />
        </el-form-item>
        <el-form-item label="稳压状态下的动态电阻ZZ" prop="paramZz" v-if="form.paramZz!==null && form.paramZz!==''">
          <el-input v-model="form.paramZz" readonly='true' />
        </el-form-item>
        <el-form-item label="最大额定功率PCM" prop="paramPcm" v-if="form.paramPcm!==null && form.paramPcm!==''">
          <el-input v-model="form.paramPcm" readonly='true' />
        </el-form-item>
        <el-form-item label="最大集电极电流ICM" prop="paramIcm" v-if="form.paramIcm!==null && form.paramIcm!==''">
          <el-input v-model="form.paramIcm" readonly='true' />
        </el-form-item>
        <el-form-item label="最大集电极-基极电压VCBO" prop="paramVcbo" v-if="form.paramVcbo!==null && form.paramVcbo!==''">
          <el-input v-model="form.paramVcbo" readonly='true' />
        </el-form-item>
        <el-form-item label="集电极-发射极电压VCEO" prop="paramVceo" v-if="form.paramVceo!==null && form.paramVceo!==''">
          <el-input v-model="form.paramVceo" readonly='true' />
        </el-form-item>
        <el-form-item label="发射极-基极电压VEBO" prop="paramVebo" v-if="form.paramVebo!==null && form.paramVebo!==''">
          <el-input v-model="form.paramVebo" readonly='true' />
        </el-form-item>
        <el-form-item label="漏源电压VDSS" prop="paramVdss" v-if="form.paramVdss!==null && form.paramVdss!==''">
          <el-input v-model="form.paramVdss" readonly='true' />
        </el-form-item>
        <el-form-item label="漏极直流电流ID" prop="paramId" v-if="form.paramId!==null && form.paramId!==''">
          <el-input v-model="form.paramId" readonly='true' />
        </el-form-item>
        <el-form-item label="漏-源通态电阻RDS(on)" prop="paramRdsOn" v-if="form.paramRdsOn!==null && form.paramRdsOn!==''">
          <el-input v-model="form.paramRdsOn" readonly='true' />
        </el-form-item>
        <el-form-item label="抗静电能力" prop="antistaticCapacity">
          <el-input v-model="form.antistaticCapacity" readonly='true' />
        </el-form-item>
        <el-form-item label="抗辐照能力" prop="radiationResistance">
          <el-input v-model="form.radiationResistance" readonly='true' />
        </el-form-item>
        <el-form-item label="材料" prop="materials">
          <el-input v-model="form.materials" type="textarea" readonly='true'/>
        </el-form-item>
        <el-form-item label="工艺" prop="craft">
          <el-input v-model="form.craft" type="textarea" readonly='true' />
        </el-form-item>
        <el-form-item label="重量" prop="weight">
          <el-input v-model="form.weight" readonly='true' />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" readonly='true' />
        </el-form-item>
      </el-form>


      <el-table style="margin-top: 20px" :header-cell-style="{background:'#87CEEB',color:'#606266'}" :data="useInfoList">
        <el-table-column label="应用场景" align="center" prop="usedBy" />
        <el-table-column label="应用时间" align="center" prop="usedDate" />
      </el-table>

    </el-dialog>

    <el-dialog
      title="推荐"
      :visible.sync="dialogVisible"
      width="20%"
      style="top:225px"

    >
      <div  align="center">
        <el-progress
          type="circle"
          :percentage="Progress"
          width="80"
          :status="progressStatus"
        ></el-progress>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { RecommendlistComponent,getComponent,getBycomponentId,addEvaluation,addTime,getscore } from "@/api/biz/recommend";
import { recommend } from "@/api/biz/train";
export default {
  name: "recommend",
  data() {
    return {
      Progress:0,
      useInfoList:[],
      eremark:'',//评分的备注
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 元器件数据
      componentList: [
      ],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      searchCondition:'',
      // 查询参数  元器件规格 元器件名称 国产替代型号 元器件应用 相关推荐 操作
      queryParams: {
        pageNum: 1,
        pageSize: 10,

          useScene: '',
          searchCondition: '',
          result1Id: null,
          result2Id: null,
          result3Id: null,
          result4Id: null,
          result5Id: null,


      },
      use_scene:'',
      evaluation: {
        id: null,
        searchCondition: null,
        componentId: null,
        mark: null,
        usedBy: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      },
      escore:0,
      showevalatedialog: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      options:[
        {
          value: '质量最优',
          label: '质量最优'
        }, {
          value: '成本最优',
          label: '成本最优'
        }, {
          value: '供货周期最优',
          label: '供货周期最优'
        }, {
          value: '供货量最优',
          label: '供货量最优'
        }
      ],
      currentTime:0,
      startTime:0,
      pageviewTime:{},

      recommmend_args:{

        useScene: '',
        searchCondition: '',
        result1Id: null,
        result2Id: null,
        result3Id: null,
        result4Id: null,
        result5Id: null,
      },
      dialogVisible:false,
    };
  },
  computed: {
    progressStatus() {
      return this.Progress === 100 ? 'success' : '';
    },
  },
  methods: {
    // 得到evaluation
    getevaluationdata(row){

      getscore(row.id.toString()).then(score=>{
        console.log(score)
        this.escore = score.data
        this.showevalatedialog=true
        this.evaluation.componentId = row.id

        this.evaluation.usedBy = row.applications.toString()
      })



    },


    //调用推荐算法
    recommend() {
      this.recommmend_args.useScene = this.use_scene
      this.recommmend_args.searchCondition = this.searchCondition
      let result;
      recommend(this.recommmend_args).then(res=>{
        result = res;
        if(res.code===200) {
          this.recommmend_args = JSON.parse(res.data).data
          console.log(this.recommmend_args)
          // this.queryParams.result1Id = 53
          this.queryParams.result1Id = this.recommmend_args.result1Id
          this.queryParams.result2Id = this.recommmend_args.result2Id
          this.queryParams.result3Id = this.recommmend_args.result3Id
          this.queryParams.result4Id = this.recommmend_args.result4Id
          this.queryParams.result5Id = this.recommmend_args.result5Id

          // this.$modal.msgSuccess("调用推荐算法成功")




        } else {
          this.$modal.msgError(res.msg)
        }

      })

      // 模拟进度变化，每0.0.5秒更新一次，共更新100次
      this.dialogVisible = true

      const intervalId = setInterval(() => {

        this.Progress++;
        // 如果进度达到100%，清除定时器
        if (this.Progress >= 100) {
          this.Progress = 0
          clearInterval(intervalId);
          this.dialogVisible = false

          if(result.code===200){
            this.getList()
            this.$modal.msgSuccess("调用推荐算法成功")
          }

          else {
            this.$modal.msgError(result.msg);
          }
        }
      }, 50);

    },
    /** 查询元器件列表 */
    getList() {
      // console.log(this.queryParams)
      this.loading = true;
      this.evaluation.searchCondition = this.searchCondition
      RecommendlistComponent(this.queryParams).then(response => {//queryParams中分页的信息应该是在session中保存，在请求的时候通过session封装到了pagedomain中了

        this.componentList = response.rows;

        let clist = response.rows;
        for(let i=0; i< clist.length;i++){

          getBycomponentId(clist[i].id).then(res=>{
            // console.log(res.data)
            let apps = []
            for(let one of res.data){

              apps.push(one.usedBy)
            }
            // console.log("apps:")

              if (apps && apps.length > 3) {
                this.$set(this.componentList[i], 'applications', apps.slice(0, 3).join(', ') + '...');

              } else {
                this.$set(this.componentList[i], 'applications', apps.join(', '));
              }


            // this.componentList[i].applications = apps

          })
        }
        this.total = response.total;
        this.loading = false;
      });
    },
    //评分
    evaluate(){
      this.evaluation.mark = this.escore
      this.evaluation.remark = this.eremark
      this.evaluation.useScene = this.use_scene

      addEvaluation(this.evaluation).then(response => {
        // this.$modal.msgSuccess("评分成功");

        this.showevalatedialog = false
      });
    },

    /** 搜索按钮操作 */
    handleDetail(row) {
      const id = row.id || this.ids
      getComponent(id).then(response => {
        this.form = response.data;
        this.title = "元器件详情";

      });
      getBycomponentId(id).then(res=>{
        this.useInfoList = res.data
      })
      this.open = true;
      this.startTime = Date.now()
      this.pageviewTime.componentId = row.id

    },

    //监听元器件详情dialog关闭
    closedialog(){
      this.currentTime = Date.now()
      this.pageviewTime.viewTime = this.currentTime - this.startTime
      this.pageviewTime.useScene = this.use_scene
      console.log(this.use_scene)
      this.pageviewTime.searchCondition = this.searchCondition
      addTime(this.pageviewTime).then(res=>{
        // this.$modal.msgSuccess("详情浏览时间成功")
      })
      console.log("详情关闭")
      this.open=false
    }
  }
};
</script>
<style>

.el-checkbox__inner {
  border-color: #8492a6 !important;
}
.app-container {
  min-height: 100vh;
  background: radial-gradient(circle at 10% 20%, #3A71A8 0%, rgb(239, 249, 249) 90%);
}
</style>
