<!--认证需求-->
<template>
  <el-main class="bg_white wrap_box lc_rw">
    <!-- 按钮组 -->
    <LcButtons ref="lcButtons" :type="2" @btsEmit="btsEmit($event)" :planStratTime="data.baseInfo.plan_start_time_text"
      :ids="ids" :projectName="data.baseInfo.project_name" :reviewerUserId="data.baseInfo.reviewer_user_id"
      :userId="data.baseInfo.leader_user_id" :msg="data.baseInfo.msg" @getPageInfo="getPageInfo"
      :deppgetPageInfo="getPageInfo" />
    <!-- 基础信息 -->
    <div class="lc_ttop">
      <lcBaseInfo ref="addBaseInfo" type="taskInfo"></lcBaseInfo>
    </div>
    <div class="lc-bsinfo">
      <!--认证需求-->
      <div class="lc_title lc_ttop lc_add_ggdyb">认证需求</div>
      <div>
        <div class="lc_title1">需求国家/类型</div>
        <div class="lc_title1">国家</div>
        <el-checkbox-group v-model="data.countrychecks" style="padding-bottom: 10px;" @change="changeCountry">
          <el-checkbox v-for="(item, index) in data.country" :key="index" :label="item.code">{{ item.country
          }}</el-checkbox>
        </el-checkbox-group>
        <div class="rzlx">
          <div style="margin-right: 20px;">认证类型</div>
          <el-select v-model="data.auth" multiple placeholder="请选择" style="width: 600px;"
            :disabled="data.countrychecks.length < 1" class="rzselect">
            <el-option v-for="item in data.authlist" :key="item.id" :label="item.auth" :value="item.id">
            </el-option>
          </el-select>
        </div>
      </div>
      <!--备注-->
      <div class="lc_ttop lc_bzt">备注</div>
      <div style="padding-bottom: 50px;">
        <lctinymce :id="data.id" ref="editor" :height="data.height" :cur-value="data.remark" @input="inputContent"
          :key="data.key"></lctinymce>
      </div>
    </div>
  </el-main>
</template>
<script>
import { defineComponent, reactive, ref, onMounted, getCurrentInstance, provide } from 'vue';
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
export default defineComponent({
  name: 'rzxq',
  setup (props) {
    let route = useRouter()
    provide('project_id', route.currentRoute.value.query.id)
    const addBaseInfo = ref()
    const lcButtons = ref()
    let { proxy } = getCurrentInstance()
    const ids = ref(route.currentRoute.value.query.prBasetaskId)
    let data = reactive({
      baseInfo: {},//基础信息
      country: [],//国家
      countrychecks: [],//国家选择中值
      auth: [],//认证类型
      authlist: [],// 认证类型集合
      //富文本
      height: 300,
      key: '',
      id: '',
      remark: '',// 备注
    })
    // 基本信息字段值
    let baseInfokeys = reactive({
      keyArry: ['sn', 'project_sn', 'name', 'required_time_text', 'status', 'leader_user_id', 'reviewer_user_id', 'task_status_text', 'examine_status_text', 'project_id', 'project_name', 'plan_start_time', 'plan_start_time_text', 'plan_end_time', 'plan_end_time_text', 'end_time_text']
    })
    onMounted(() => {
      getPageInfo()
    })
    // 获取页面信息
    function getPageInfo (value) {
      proxy.$http.get('project/task/cert_auth/info', { id: route.currentRoute.value.query.prBasetaskId }).then(res => {
        if (res.errno == 200) {
          data.baseInfo = JSON.parse(JSON.stringify(res.data))
          ids.value = res.data.id
          // 国家
          data.country = JSON.parse(JSON.stringify(res.data.country))
          // 类型
          let newauth = []
          JSON.parse(JSON.stringify(res.data.auth)).forEach(it => {
            newauth.push(Number(it))
          })
          data.auth = newauth
          let a1 = []
          if (data.country && data.country.length > 0) {
            data.country.forEach(it => {
              if (it.status == 1) {
                a1.push(it.code)
              }
            })
          }
          data.countrychecks = a1
          getAuthlist()
          //备注
          data.remark = res.data.remark
          // 驳回原因
          addBaseInfo.value.data.reject = res.data && res.data.reject ? res.data.reject : ''
          // 基础信息
          baseInfokeys.keyArry.forEach(it => {
            addBaseInfo.value.taskform[it] = res.data[it]
          })
        } else {
          ElMessage({
            type: 'success',
            message: res.errmsg
          })
        }
      })
    }
    //改变国家时
    function changeCountry (value) {
      data.auth = []
      getAuthlist()
    }
    // 获取认证信息列表
    function getAuthlist () {
      proxy.$http.showLoading(false).get('project/task/cert_auth/auth_list', {
        'country': data.countrychecks && data.countrychecks.length > 0 ? data.countrychecks.join() : ''
      }).then(res => {
        if (res.errno == 200) {
          data.authlist = []
          data.authlist = res.data
        } else {
          ElMessage({
            type: 'error',
            message: res.errmsg
          })
        }
      })
    }
    //按钮组操作
    function btsEmit (value) {
      // 提交和审核先做保存的动作
      if (value == 'passBt' || value == 'submitBt') {
        handelSavePage(value)
      }
      // 保存
      if (value == 'saveBt') {
        handelSavePage()
      }
    }
    // 保存页面
    function handelSavePage (bttype) {
      proxy.$http.showLoading(false).post('project/task/cert_auth/edit', {
        id: data.baseInfo.id,
        leader_user_id: addBaseInfo.value.taskform['leader_user_id'],
        reviewer_user_id: addBaseInfo.value.taskform['reviewer_user_id'],
        plan_start_time: addBaseInfo.value.taskform['plan_start_time_text'],
        plan_end_time: addBaseInfo.value.taskform['plan_end_time_text'],
        remark: data.remark,
        country: data.countrychecks,
        auth: data.auth,
      }).then(res => {
        if (res.errno == 200) {
          ElMessage({
            type: 'success',
            message: res.errmsg
          })
          if (bttype == 'submitBt') {
            lcButtons.value.thesubmit(bttype)
          }
          if (bttype == 'passBt') {
            lcButtons.value.thepass(bttype)
          }
          getPageInfo()
        } else {
          ElMessage({
            type: 'error',
            message: res.errmsg
          })
        }
      })
    }
    //富文本编辑器
    function inputContent (value) {
      data.remark = value
    }
    return {
      data,
      ids,
      addBaseInfo, lcButtons,
      btsEmit,
      getPageInfo,
      inputContent,
      changeCountry,
      getAuthlist
    }
  }
})
</script>
<style scoped lang="scss">
@import '../../../assets/style/lc.scss';
</style>