<template>
    <div style="background: white;padding: 20px;height: 100%;">
      <div style="float:right" v-if="downloadId==9">
        <el-button type="primary" @click="settingUser">账号配置</el-button>
      </div>
      <div style="width: 800px;margin: 0 auto;">
        <div v-if="downloadId==9" >
          <el-form :inline="true" :model="queryParams" :rules="rules" ref="from" class="demo-form-inline" style="display: flex;justify-content: space-between;">
            <el-form-item label="来源网站" prop="configId">
              <el-select v-model="queryParams.configId" placeholder="请选择网站" >
                  <el-option v-for="item in website"
                    :key="item.id"
                    :label="item.webName"
                    :value="item.id">
                  </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="下载内容" prop="configCells">
              <el-select v-model="queryParams.configCells"  multiple collapse-tags placeholder="请选择下载内容" style="width: 100%;">
                  <el-option v-for="item in download"
                    :key="Number(item.cellValue)"
                    :label="item.cellName"
                    :value="Number(item.cellValue)">
                  </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="是否更新" prop="taskType">
              <el-switch v-model="queryParams.taskType" :active-value="1" :inactive-value="0" active-color="#13ce66" inactive-color="#ff4949" @change="change3">  </el-switch>
            </el-form-item>
          </el-form>
          <div v-show="queryParams.taskType"  >
            <el-form :inline="true" ref="from" :model="queryParams" :rules="rules" class="demo-form-inline" style="display: flex;justify-content: space-between;">
              <el-form-item label="更新间隔" prop="dateType">
                <el-select v-model="queryParams.dateType" @change="getDateType" clearable placeholder="请选择更新时间间隔" style="margin-right: 20px;">
                  <el-option v-for="item in options"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="选择更新具体时间" v-if="queryParams.dateType" >
                <timeChoose :type="queryParams.dateType" @value="handleData" :cron="queryParams.crons" style="width:300px"></timeChoose>
              </el-form-item>
            </el-form>
          </div>
          </div>
          <!-- <div style="display: flex;margin-bottom: 10px;" v-if="queryParams.configType==2">
            <p style="width:150px;margin-right:10px;margin-bottom: 10px;">数据库：</p>
            <div>
              <el-checkbox-group v-model="queryParams.database">
                <el-checkbox v-for="item in database" :key="item.value" :label="item.value" style="margin:5px">{{ item.label }}</el-checkbox>
              </el-checkbox-group>
            </div>
          </div> -->
          <!-- 搜索 -->
          <div style="display: flex;justify-content: center;width: 100%;align-items:center">
            <span style="width: 100%;">
               <el-input v-model="queryParams.conditions" :placeholder="downloadId==9?'请输入检索信息':'请输入专利号（多个专利号同时添加中间请用符号“|”隔开）'" ></el-input>
            </span>
            <span style="margin-left:8px" v-if="queryParams.configType==2">
              <el-tooltip class="item" effect="dark" content="检索说明" placement="top">
                <i class="el-icon-question" style="font-size:28px" @click="checkConceal(3)" ></i>
              </el-tooltip>
            </span>
              <el-button @click="imports" type="primary" icon="el-icon-upload el-icon--right" style="margin-left:20px"> 导入 </el-button>
          </div>  
      </div>
      
      <div>
         <SystemTask :form="9" :reportId="reportId" :productId="productId" :projectId="projectId"></SystemTask> 
      </div>
    </div>
</template>

<script>
import SystemTask from "@/views/task/index.vue";
import { cron } from "@/views/workspace/components/common/mixins";
import timeChoose from "@/views/workspace/components/common/timeChoose.vue";

export default {
  mixins:[cron],
  props:['reportId','downloadId','asCompare','productId','projectId'],
  components: {
    SystemTask,
    timeChoose,
  },
  data() {
    const dateTypeRule = (rule, value, callback) => {
      // console.log(value)
      if (this.queryParams.taskType == 1) {
        if (value) {
          callback()
        } else {
          callback(new Error('请选择更新周期'))
        }
      } else {
        callback()
      }
    }
    return {
      database:[
        {
          label:'中国发明专利',
          value:'FMZL'
        },
        {
          label:'中国发明授权',
          value:'FMSQ'
        },
        {
          label:'中国实用新型',
          value:'SYXX'
        },
        {
          label:'中国外观专利',
          value:'WGZL'
        },
        {
          label:'美国',
          value:'USPATENT'
        },
        {
          label:'英国',
          value:'GBPATENT'
        },
        {
          label:'法国',
          value:'FRPATENT'
        },
        {
          label:'德国',
          value:'DEPATENT'
        },
        {
          label:'瑞士',
          value:'CHPATENT'
        },
        {
          label:'日本',
          value:'JPPATENT'
        },
        {
          label:'俄罗斯',
          value:'RUPATENT'
        },
        {
          label:'韩国',
          value:'KRPATENT'
        },
        {
          label:'欧洲专利局',
          value:'EPPATENT'
        },
        {
          label:'世界知识产权组织',
          value:'WOPATENT'
        },

      ],
      website:[],//选择网站
      download: [],//选择下载内容
      options: [
        {label:'每天',value:'day'},
        {label:'每周',value:'week'},
        {label:'每月',value:'month'},
        {label:'每年',value:'year'},
      ],
      queryParams: {
        database:[],
        reportId: this.reportId,
        productId: this.productId,
        projectId: this.projectId,
        crons:'',
        conditions:'',//搜索条件
        configId: '',//网站
        configType:'',
        configCells: [],//下载内容
        dateType:'',
        dates: '',
        taskType:0,//0普通任务1定时任务
        taskState:0,//任务状态 0等待1进行2成功3失败
      },
      isGetList:false,
      rules: {
        configId: [{ required: true, message: '请选择来源网站', trigger: 'change' },],
        configCells: [{ required: true, message: '请选择下载内容', trigger: 'change' },],
        taskType: [{ required: true, message: '请选择', trigger: 'change' },],
        dateType: [{ required: true,  validator:dateTypeRule, trigger: 'change' },],
      },
    }
  },
  created() {

  },
  watch: {
    'queryParams.configId'(val) {//更换网站
      let a= this.website.filter(item => {
        return item.id==val
      })
      this.queryParams.configType = a[0].webType
      this.downloads()
    },

  },
  mounted() {
    // this.getList()
  },
  methods: {
    checkConceal(id) {
      const router = this.$router.resolve({
        path: '/agreeConceal',
        query: {
          id:id
        }
      })
      window.open(router.href, '_blank')
      // const router1 = this.$router.resolve({
      //   path: '/downPatentList',
        
      // })
      // window.open(router1.href, '_blank')
    },
    //打开账号配置
    settingUser(){
      this.$refs.settingUser.open(this.website)
    },
    change3(val) {
      // console.log(val);
    },
    getDateType(val) {
      // console.log(val)
      this.queryParams.crons = ''
    },
    // 获取时间
    handleData(val) {
      if (this.queryParams.dateType=='') {
        this.$message.error('请先选择更新周期')
        return false
      }
      this.queryParams.crons=val
    },
    // 请求全部网站
    getList() {
      this.$api.getAllConfig({}).then(res => {
        if (res.code==200) {
          this.website = res.data
        }
      })
    },
    // 请求当前网站可下载项
    async downloads() {
      await this.$api.getConfigCell({type:this.queryParams.configType}).then(res => {
        if (res.code==200) {
          this.download = res.data
          this.queryParams.configCells=this.download.map(item=>{ return Number(item.cellValue) })
        }
      })
    },
    // 导入
    imports() {
      if (this.queryParams.conditions == '') {
        if (this.downloadId == 9) {
          this.$message.error('请输入检索信息')
        } else {
          this.$message.error('请输入专利号')
        }
        return false
      }
      if (this.downloadId == 8) {
        this.queryParams.patentNo = this.queryParams.conditions
        this.queryParams.conditions = ''
        this.queryParams.asCompare=this.asCompare
        this.$api.importByNos(this.queryParams).then(res => {
          // console.log(res.code);
          if (res.code == 200) {
            if (res.data && res.data.length > 0) {
              // if (!res.data.includes('')) {
              // this.alterMessage(res.data)
              // }
            } else {
              this.$message.success('创建任务完成，正在导入！')
            }
           
            this.queryParams.patentNo = ''
            // this.getList()
          }
        }).catch(error => {
        })
        return false
      }
      this.$refs.from.validate((valid) => {
        if (valid) {
          this.$api.addTask(this.queryParams).then(res => {
            if (res.code==200) {
              this.$message.success('创建任务完成,正在导入！')
              this.queryParams.conditions = ''
              this.isGetList=!this.isGetList
            }
          })
        }
        
      })
     
    },
  },
}
</script>

<style lang="scss" scoped>

</style>