<!-- 项目编辑 -->
<template>
  <el-main class="bg_white wrap_box lcpg">
    <!-- 按钮组-->
    <LcButtons ref="lcButtons" :type="1" @btsEmit="btsEmit($event)" :bts="btsArry"></LcButtons>
    <!-- 基本信息-->
    <lcBaseInfo ref="addBaseInfo" type="info"></lcBaseInfo>
    <!--任务信息-->
    <div class="lc-bsinfo">
      <div class="lc_title">任务信息列表</div>
      <vxe-table resizable id="tasktbale" :scroll-x="{ enabled: false }" :row-config="{ isHover: true }"
        :scroll-y="{ enabled: false }" keepSource :custom-config="{ storage: true }" border show-overflow
        :data="data.tasktable" :edit-config="{ trigger: 'click', mode: 'cell', showStatus: true }"
        :column-config="{ resizable: true }" class="ggdycss" max-height="500">
        <vxe-column field="name" title="任务名称" width="200" align="left">
          <template #default="{ row }">
            <span v-if="row.is_push == '1'" @click="getRouteInfo(row)" style="color: #409EFF;cursor: pointer;">{{ row.name
            }}</span>
            <span v-else style="color: #c0c4cc;">{{ row.name }}</span>
          </template>
        </vxe-column>
        <vxe-column field="leader_user_name" title="任务负责人" width="145" align="left"></vxe-column>
        <vxe-column field="reviewer_user_id" title="任务审核人" width="200" align="left"
          :edit-render="{ autofocus: '.vxe-input--inner' }">
          <template #default="{ row }">
            <span>{{ formatMulti(row.reviewer_user_id) }}</span>
          </template>
          <template #edit="{ row }">
            <vxe-select v-model="row.reviewer_user_id" transfer placeholder="请选择">
              <vxe-option v-for="item in data.userlist" :key="item.id" :label="item.nickname"
                :value="item.id"></vxe-option>
            </vxe-select>
          </template>
        </vxe-column>
        <vxe-column field="committed_step_text" title="是否关键步骤" width="145" align="left"></vxe-column>
        <vxe-column field="examine_status_text" title="任务审核状态" width="145" align="left"></vxe-column>
        <vxe-column field="plan_start_time_text" title="预计开始时间" width="145" align="left"></vxe-column>
        <vxe-column field="plan_end_time_text" title="预计结束时间" width="145" align="left"></vxe-column>
        <vxe-column field="start_time_text" title="开始时间" width="145" align="left"></vxe-column>
        <vxe-column field="end_time_text" title="结束时间" width="145" align="left"></vxe-column>
        <vxe-column field="msg" title="驳回原因" width="250" align="left"></vxe-column>
      </vxe-table>
      <!--规格定义表-->
      <div class="lc_title lc_ttop lc_add_ggdyb">规格定义表</div>
      <lcggdytab v-model:ggdyTableData='data.ggdyTableData' v-model:ggdybts='data.ggdybts' :proojectId="baseInfo.id"
        @getPageInfo="getPageInfo" :deppgetPageInfo="getPageInfo" :skufield="baseInfo.sku_field">
      </lcggdytab>
      <!--备注-->
      <div class="lc_ttop lc_bzt">备注</div>
      <div style="padding-bottom: 50px;">
        <lctinymce ref="editor" :height="data.height" :cur-value="remark" @input="inputContent" :id="data.id"
          :key="data.key"></lctinymce>
      </div>
    </div>
    <!-- 下推-->
    <lcDailogxt @handelClose="handelClose" v-model:xtshow="data.xtshow" :projectId="data.projectId" v-if="data.xtshow"
      @getinit="getPageInfo">
    </lcDailogxt>
  </el-main>
</template>
<script setup name="editProject">
import { ElMessage } from 'element-plus'
import { reactive, ref, getCurrentInstance, provide } from 'vue';
import { useRoute, useRouter } from 'vue-router'
import { routeLinksConversion } from '../../../utils/lc_jx_utils/lc_jx.js'
import { prlisttopBt } from '../../../utils/lc_data'// 获取人员
import { getUsers, } from '../../../utils/lc'// 获取人员
/* 数据 */
const route = useRoute()
const router = useRouter()
let { proxy } = getCurrentInstance()// this替代
let remark = ref('')
let baseInfo = ref('')
provide('project_id', route.query.id)
console.log('项目id', route.query.id)
// 任务信息列表
let data = reactive({
  tasktable: [],
  userlist: [],
  xtshow: false,
  projectId: route.query.id,
  ggdyTableData: [],// 规格定义表
  //富文本
  height: 300,
  key: '',
  id: '',
  ggdybts: 'edit'
})
const btsArry = reactive(JSON.parse(JSON.stringify(prlisttopBt)))
// 基本信息组件值
const addBaseInfo = ref()
const lcButtons = ref()
/* 初始化获取数据 */
getUsers({ page: 1, size: 999999, nickname: '' }).then(res => {
  data.userlist = res.data
  getPageInfo()
})
provide('project_id', route.query.id)
/* 方法 */
// function getlistName(id =>{
//   ret
// })
//获取项目详情
function getPageInfo () {
  if (route.query.id) {
    proxy.$http.showLoading(true).get('project/project/info?id=' + route.query.id).then(res => {
      if (res.errno == 200) {
        baseInfo.value = JSON.parse(JSON.stringify(res.data))
        data.tasktable = res.data.task ? res.data.task : []
        data.ggdyTableData = []
        data.ggdyTableData = res.data.skus ? res.data.skus : []
        remark.value = res.data.remark
        res.data.skus.forEach((it, index) => {
          res.data.skus[index].index = index + 1
          res.data.skus[index].links_qd_bom = ''
          res.data.skus[index].links_bz_bom = ''
        })
        let keys = ['id', 'sn', 'image', 'name', 'leader_user_id', 'status', 'issue_time', 'status_text', 'level', 'code', 'bh', 'cate_id', 'plan_start_time', 'plan_end_time', 'start_time', 'end_time', 'product_time', 'online_time', 'brand_ids']
        keys.forEach(it => {
          addBaseInfo.value.form[it] = res.data[it]
          if (it == 'image') {
            addBaseInfo.value.imageUrl = res.data[it]
          }
          if (it == 'cate_id') {
            let arr = []
            res.data[it].forEach(it => {
              arr.push(parseInt(it))
            })
            addBaseInfo.value.form[it] = arr
          }
          if (it == 'brand_ids') {
            addBaseInfo.value.form[it] = res.data[it] ? res.data[it].split(',').map(Number) : ''
          }
          if (it == 'level') {
            addBaseInfo.value.form[it] = res.data[it] ? parseInt(res.data[it]) : ''
          }
        })
      }
    })
  }
}
function formatMulti (id) {
  let it = []
  if (data.userlist && data.userlist.length > 0 && id) {
    it = data.userlist.filter(i => {
      return i.id == id
    })
    return it && it.length > 0 ? it[0].nickname : ''
  }
}
//富文本编辑器
function inputContent (value) {
  remark.value = value
}
//按钮组操作
function btsEmit (value) {
  // 日志
  if (value == 'logbt') {
    router.push({ path: '/lcProject/log/prlog', query: { project_id: route.query.id, projectName: addBaseInfo.value.form.name } })
  }
  // 点击保存
  if (value == 'saveProject') {
    saveProject()
  }
  // 继续项目goOnPrijectbt   暂停项目stopProjectbt    结束closeProjectbt
  if (value == 'goOnPrijectbt' || value == 'stopProjectbt' || value == 'closeProjectbt') {
    proxy.$http.showLoading(false).get('project/project/ch_status', {
      id: route.query.id,
      status: value == 'goOnPrijectbt' ? 30 : value == 'stopProjectbt' ? 20 : value == 'closeProjectbt' ? 40 : 0
    }).then(res => {
      if (res.errno == 200) {
        ElMessage({
          type: 'success',
          message: '操作成功'
        })
        getPageInfo()
      } else {
        ElMessage({
          type: 'error',
          message: res.errmsg
        })
      }
    })
  }
  if (value == 'pushdown') {
    if (baseInfo.value.id) {
      data.xtshow = true
    }
  }
}
// 保存
function saveProject () {
  let editdata = JSON.parse(JSON.stringify(addBaseInfo.value.form))
  if (editdata.name && editdata.cate_id && editdata.brand_ids) {
    editdata.id = baseInfo.value.id
    editdata.brand_ids = editdata.brand_ids.join(',')
    editdata.cate_id = editdata.cate_id && editdata.cate_id.length > 0 ? editdata.cate_id[editdata.cate_id.length - 1] : ''
    editdata.remark = remark.value
    editdata.task = JSON.parse(JSON.stringify(data.tasktable))
    editdata.skus = JSON.parse(JSON.stringify(data.ggdyTableData))
    proxy.$http.showLoading(false).post('project/project/edit', editdata).then(res => {
      if (res.errno == 200) {
        ElMessage({
          type: 'success',
          message: '保存成功'
        })
        getPageInfo()
      } else {
        ElMessage({
          type: 'error',
          message: res.errmsg
        })
      }
    })
  } else {
    ElMessage({
      type: 'error',
      message: '请填写必填项！'
    })
  }
}
// 获取路由跳转
function getRouteInfo (rowInfo) {
  let item = JSON.parse(JSON.stringify(rowInfo))
  router.push({ path: '/lcProject/taskMange/' + routeLinksConversion(item.basetask_id), query: { id: item.project_id, prBasetaskId: item.id } })
}
function handelClose () {
  data.xtshow = false
}
</script>
<style scoped lang="scss">
@import '../../../assets/style/lc.scss';

.vxe-cell {
  cursor: pointer;
}
</style>
