<template>
  <div>
    <xy-dialog ref="dialog" :is-show.sync="isShow" :width='50' :type="'form'"
      :title="mission_name" :form="form" :rules='rules' @submit="submit">
      <template v-slot:carry_type v-if="type!='check'">
        <div class="xy-table-item">
          <div class="xy-table-item-label" style="font-weight: bold">
            <span style="color: red;font-weight: bold;padding-right: 4px;">*</span>执行方式：
          </div>
          <div class="xy-table-item-content">
            <el-select v-if="formObj.admin_type==2 || formObj.from==1" :disabled="isdisabled || isEditor" v-model="formObj.carry_type"
              style="width: 400px;" placeholder="请选择">
              <el-option v-for="item in carryPidList" :key="item.id" :label="item.value" :value="item.id">
              </el-option>
            </el-select>

            <el-select v-else :disabled="isdisabled || isEditor" v-model="formObj.carry_type" style="width: 400px;"
              placeholder="请选择">
              <el-option v-for="item in carryList" :key="item.id" :label="item.value" :value="item.id">
              </el-option>
            </el-select>

          </div>
        </div>
      </template>
      <template v-slot:start_date v-if="type!='check'">
        <div class="xy-table-item">
          <div class="xy-table-item-label" style="font-weight: bold">
            <span style="color: red;font-weight: bold;padding-right: 4px;">*</span>开始时间：
          </div>
          <div class="xy-table-item-content">
            <el-date-picker :disabled="isdisabled" style="width: 400px;" v-model="formObj.start_date"
              value-format="yyyy-MM-dd" type="date" placeholder="选择">
            </el-date-picker>
          </div>
        </div>
      </template>
      <template v-slot:end_date v-if="type!='check'">
        <div class="xy-table-item">
          <div class="xy-table-item-label" style="font-weight: bold">
            <span style="color: red;font-weight: bold;padding-right: 4px;">*</span>预计完成时间：
          </div>
          <div class="xy-table-item-content">
            <el-date-picker :disabled="isdisabled" style="width: 400px;" v-model="formObj.end_date"
              value-format="yyyy-MM-dd" type="date" placeholder="选择">
            </el-date-picker>
          </div>
        </div>
      </template>
      <template v-slot:ing_introduce v-if="type!='check'">
        <div class="xy-table-item">
          <div class="xy-table-item-label" style="font-weight: bold">
            <span style="color: red;font-weight: bold;padding-right: 4px;"></span>当前进展概述：
          </div>
          <div class="xy-table-item-content">
            <el-input :disabled="isdisabled" v-model="formObj.ing_introduce" type="textarea" :rows="8"
              placeholder="请输入当前进展概述" clearable style="width: 400px;"></el-input>
          </div>
        </div>
      </template>
      <template v-slot:finish_status v-if="type=='editor' || type=='check'">
        <div class="xy-table-item">
          <div class="xy-table-item-label" style="font-weight: bold">
            <span style="color: red;font-weight: bold;padding-right: 4px;">*</span>完成情况：
          </div>
          <div class="xy-table-item-content">
            <el-select  :disabled="isdisabled" v-model="formObj.finish_status" style="width: 400px;"
              placeholder="请选择">
              <el-option v-for="item in finish_status_list" :key="item.id" :label="item.value" :value="item.id">
              </el-option>
            </el-select>
          </div>
        </div>
      </template>
      <template v-slot:file_ids v-if="type=='editor' || type=='check'">
        <div class="xy-table-item">
          <div class="xy-table-item-label" style="font-weight: bold">
            <span style="color: red;font-weight: bold;padding-right: 4px;"></span>成果资料：
          </div>
          <div class="xy-table-item-content">
            <el-upload v-if="!isdisabled" :action="action" multiple class='upload-demo' :file-list="fileList"
              ref="pictureUpload" style="width:400px" :auto-upload="true" :data="uploadOther"
              :on-success="handlesuccess" :on-remove="handleRemove">
              <el-button size="small" type="primary">点击上传</el-button>
            </el-upload>
            <div v-else>
              <div v-for="item in fileList">
                <div style="color:#2D8cF0;cursor: pointer;">
                {{item.name}}
                <el-link target="_blank" style="margin:0 10px;" @click="toshowFile(item.url)" type="primary">
                  预览
                </el-link>
                <el-link target="_blank" @click="base.downLoadFileUrl(item.id)" type="primary">
                  下载
                </el-link>
                </div>
              </div>
            </div>
          </div>
        </div>
      </template>


      <template v-slot:end_introduce v-if="type=='editor' || type=='check'">
        <div class="xy-table-item">
          <div class="xy-table-item-label" style="font-weight: bold">
            <span style="color: red;font-weight: bold;padding-right: 4px;"></span>成果概述：
          </div>
          <div class="xy-table-item-content">
            <el-input :disabled="isdisabled" v-model="formObj.end_introduce" type="textarea" :rows="8"
              placeholder="请输入成果概述" clearable style="width: 400px;"></el-input>
          </div>
        </div>
      </template>
      <template v-slot:check_status v-if="type=='check'">
        <div class="xy-table-item">
          <div class="xy-table-item-label" style="font-weight: bold">
            <span style="color: red;font-weight: bold;padding-right: 4px;">*</span>审核：
          </div>
          <div class="xy-table-item-content">
            <el-select v-model="check_status" style="width: 400px;" placeholder="请选择">
              <el-option v-for="item in checkStatusList" :key="item.id" :label="item.value" :value="item.id">
              </el-option>
            </el-select>
          </div>
        </div>
      </template>
      <template v-slot:check_contents v-if="type=='check'">
        <div class="xy-table-item">
          <div class="xy-table-item-label" style="font-weight: bold">
            <span style="color: red;font-weight: bold;padding-right: 4px;">*</span>审核意见：
          </div>
          <div class="xy-table-item-content">
            <el-input v-model="formObj.check_contents" type="textarea" :rows="8"
              placeholder="请输入审核意见" clearable style="width: 400px;"></el-input>
          </div>
        </div>
      </template>
      <template v-slot:footerContent>
        <Button v-if="type=='add'" type="primary" style='margin-left:5px;margin-bottom:5px;' @click="submit">确认</Button>
        <Button v-if="type=='check'" type="primary" style='margin-left:5px;margin-bottom:5px;'
          @click="submit">审核</Button>
        <Button v-if="type=='editor'" type="primary" style='margin-left:5px;margin-bottom:5px;'
          @click="submit(0)">暂存</Button>
        <Button v-if="type=='editor'" type="primary" style='margin-left:5px;margin-bottom:5px;'
          @click="submit(1)">提交</Button>
        <Button type="primary" ghost style='margin-left:5px;margin-bottom:5px;' @click="isShow=false">取消</Button>
      </template>

    </xy-dialog>
            <viewFile ref='viewFile'></viewFile>
  </div>
</template>

<script>
    import viewFile from '@/components/viewFile/viewFile.vue'
  import {
    Message
  } from 'element-ui'
  import {
    save
  } from '@/api/task/newplan.js'
  import {
    getToken
  } from '@/utils/auth'
  import {
    deepCopy
  } from "@/utils/index.js"
  import {
    updatemission
  } from "@/api/task/unit.js";
  export default {
    components: {
      viewFile,
    },
    data() {
      return {
        isShow: false,
        type: 'add',
        mission_name:'开始任务',
        id: '',
        pid: 0,
        isdisabled: false,
        isEditor: false,
        check_status: 2,
        checkStatusList: [{
          id: 2,
          value: '通过'
        }, {
          id: 3,
          value: '退回'
        }],
        carryList: [{
          id: 0,
          value: '本人执行'
        }, {
          id: 1,
          value: '任务分解'
        }, {
          id: 2,
          value: '任务转办'
        }],
        carryPidList: [{
          id: 0,
          value: '本人执行'
        }],
        uploadOther: {
          token: ""
        },
        action: `${process.env.VUE_APP_BASE_API}api/admin/upload-file`,
        fileList: [],
        formObj: {},
        unitStartDate: '',
        unitEndDate: '',
        form: {
          carry_type: 0,
          start_date: '',
          end_date: '',
          ing_introduce: '',
          finish_status:'',
          file_ids: [],
          end_introduce: '',
          check_status: '',
          check_contents:''
        },
        finish_status_list:[{
          id:1,
          value:'已完成'
        },{
          id:2,
          value:'已列计划'
        }],
        rules: {
          // carry_type: [{
          //   required: true,
          //   message: '请选择'
          // }],
          // start_date: [{
          //   required: true,
          //   message: '请选择'
          // }],
          // end_date: [{
          //   required: true,
          //   message: '请选择'
          // }],
        }
      }
    },
    created() {
      this.uploadOther.token = getToken();
    },
    methods: {
      toshowFile(url) {
        console.log("url", url)
        this.$refs.viewFile.url = url
        this.$refs.viewFile.diaShow = true
      },
      setDate(e1, e2) {

      },
      setForm(e,e1,e2,name) {
        if (e) {
          this.formObj = deepCopy(e)
          this.unitStartDate = e1

          this.unitEndDate = e2
          this.formObj.start_date = e1
          this.formObj.end_date = e2
          this.mission_name = name
          let arr = []
          e.files.map(item => {
            arr.push({
              id: item.id,
              url: item.url,
              name: item.original_name
            })
          })
          this.fileList = arr
        }
      },
      handleRemove(file, fileList) {
        this.fileList = fileList
      },
      handlesuccess(response, file, fileList) {
        this.fileList = fileList
      },
      // 执行开始时间不能小于 任务时间
      isLessThanOrEqualToToday(startDate,unitStartDate) {
        const start = this.$moment(startDate,'YYYY-MM-DD');
        const unitStart = this.$moment(unitStartDate, 'YYYY-MM-DD');

        return start.isBefore(unitStart, 'day'); //
      },
      isMoreThanOrEqualToToday(endDate,unitEndDate) {
        const end = this.$moment(endDate,'YYYY-MM-DD');
        const unitEnd = this.$moment(unitEndDate, 'YYYY-MM-DD');

        return end.isAfter(unitEnd, 'day'); //
      },
      submit(keep_status) {
        if(!this.formObj.start_date || this.isLessThanOrEqualToToday(this.formObj.start_date,this.unitStartDate)){
            this.$Message.warning('开始时间不能为空或早于任务开始时间')
            return
        }
        if(!this.formObj.end_date || this.isMoreThanOrEqualToToday(this.formObj.end_date,this.unitEndDate)){
            this.$Message.warning('预计完成时间不能为空或晚于任务完成时间')
            return
        }
        if (this.type == 'add') {
          this.formObj.carry_status = 1
          this.formObj.keep_status = 0
        }
        if (this.type == 'editor') {
          if(!this.formObj.finish_status){
            this.$Message.warning('请选择完成情况')
            return
          }
          this.formObj.keep_status = keep_status ? keep_status : 0
        }
        if (this.type == 'check') {
          this.formObj.keep_status = this.check_status
          this.formObj.carry_status = this.check_status == 2 ? 2 : 1
        }
        let _files = []
        if (this.fileList.length > 0) {
          for (var h of this.fileList) {
            if (h.response) {
              _files.push(h.response.id)
            } else {
              _files.push(h.id)
            }
          }
          this.formObj.file_ids = _files
        } else {
          this.formObj.file_ids = []
        }
        save({
          table_name: 'mission_distributes',
          ...this.formObj
        }).then(res => {
          Message({
            type: 'success',
            message: '提交成功'
          })
          if (this.type == 'add') {
            if (this.formObj.carry_type == 0) {
              this.$emit('refresh', 'carry_type0')
            } else if (this.formObj.carry_type == 1) {
              this.$emit('refresh', 'carry_type1')
            } else if (this.formObj.carry_type == 2) {
              this.$emit('refresh', 'carry_type2')
            }
          }
          if (this.type == 'check') {
            updatemission({
              id: this.id
            }).then(r => {
              this.$emit('reloadList')
            })
          }
          this.$emit('reload')
          this.isShow = false

        })
      },
    },
    watch: {
      isShow(newVal) {
        if (newVal) {
          if (this.type === 'editor') {
            this.isEditor = true
          }
          if (this.type === 'add') {

          }
          if (this.type === 'check') {
            this.isdisabled = true
          }
        } else {
          this.id = ''
          this.formObj = {}
          this.unitStartDate = ''
          this.unitEndDate = ''
          this.isdisabled = false
          this.isEditor = false
          this.check_status = 2
          this.mission_name='开始任务'
          this.$refs['dialog'].reset()
        }
      },
    }

  }
</script>

<style scoped lang="scss">
  ::v-deep .carry_type,
  ::v-deep .start_date,
  ::v-deep .end_date,
  ::v-deep .ing_introduce,
  ::v-deep .end_introduce,
  ::v-deep .file_ids {
    flex-basis: 100%;
  }

  ::v-deep .el-form>div {
    align-items: flex-start !important;
  }

  .xy-table-item {
    padding-right: 0
  }

  ::v-deep .xy-table-item-label {
    padding-right: 0
  }
</style>
