<template xmlns:v-slot="http://www.w3.org/1999/XSL/Transform">
  <div>
    <div v-if="inputForm.status === '5'&&tabName === '报告签发单'">
      <el-row type="flex" justify="end">
        <el-button @click="downloadReportAusstellungTpl">下载</el-button>
      </el-row>
      <el-divider content-position="left"><i class="el-icon-document"></i> 报告签发单</el-divider>
    </div>
    <el-form size="middle" :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="formReadOnly"
             label-width="150px" @submit.native.prevent>
      <el-row  :gutter="15">
        <el-col :span="24">
          <el-form-item label="项目名称" prop="projectName">
            <el-input size="medium" :disabled="true" v-model="inputForm.projectName" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="评估基准日" prop="assessmentDate">
            <el-input size="medium" :disabled="true" v-model="inputForm.assessmentDate" clearable></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="报告文号" prop="reportNo">
            <el-input size="medium" :disabled="true" v-model="inputForm.reportNo" clearable></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="项目负责人" prop="projectManager">
            <SelectUserTree
              ref="companyTree"
              size="medium"
              :props="{
                  value: 'id',             // ID字段名
                  label: 'name',         // 显示名称
                  children: 'children'    // 子级字段名
                }"
              :url="`/sys/user/treeUserDataAllOffice?type=2`"
              :value="inputForm.projectManager"
              :disabled="true"
              :clearable="true"
              :accordion="true"
              @getValue="(value) => {inputForm.projectManager=value}"/>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="所属部门" prop="department">
            <el-input size="medium" :disabled="true" v-model="inputForm.department" clearable></el-input>
          </el-form-item>
        </el-col>

       </el-row>

      <el-divider>业务类型</el-divider>
      <el-divider content-position="left">A类</el-divider>
      <el-row>
        <vxe-table
          border
          show-overflow
          ref="details"
          class="vxe-table-element"
          :data="AList"
          style="margin-left: 5em"
          @cell-click=""
          @edit-closed=""
          highlight-current-row
          :edit-config="{trigger: 'click', mode: 'cell', showStatus: true, autoClear: true}"
        >
          <vxe-table-column field="name" title="名称" :edit-render="{}">
          </vxe-table-column>
        </vxe-table>
      </el-row>

      <el-divider content-position="left">B类</el-divider>
      <el-row>
        <vxe-table
          border
          show-overflow
          ref="details"
          class="vxe-table-element"
          :data="BList"
          style="margin-left: 5em"
          @cell-click=""
          @edit-closed=""
          highlight-current-row
          :edit-config="{trigger: 'click', mode: 'cell', showStatus: true, autoClear: true}"
        >
          <vxe-table-column field="name" title="名称" :edit-render="{}">
          </vxe-table-column>
        </vxe-table>
      </el-row>

      <el-divider content-position="left">C类</el-divider>
      <el-row>
        <vxe-table
          border
          show-overflow
          ref="details"
          class="vxe-table-element"
          :data="CList"
          style="margin-left: 5em"
          @cell-click=""
          @edit-closed=""
          highlight-current-row
          :edit-config="{trigger: 'click', mode: 'cell', showStatus: true, autoClear: true}"
        >
          <vxe-table-column field="name" title="名称" :edit-render="{}">
          </vxe-table-column>
        </vxe-table>
      </el-row>

      <el-row :gutter="15" style="margin-top: 20px">

        <el-col :span="12">
          <el-form-item label="是否已完善项目信息" prop="isTxtz" :rules="[{required: true, message:'请选择是否已完善项目信息', trigger:'blur'}]">
            <el-radio-group v-model="inputForm.isTxtz">
              <el-radio v-for="item in $dictUtils.getDictList('yes_no')" :label="item.value" >{{item.label}}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="报告电子版是否归档" prop="isBgdzdgd" :rules="[{required: true, message:'请选择报告电子版是否归档', trigger:'blur'}]">
            <el-radio-group v-model="inputForm.isBgdzdgd">
              <el-radio v-for="item in $dictUtils.getDictList('yes_no')" :label="item.value" >{{item.label}}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="是否需要备案" prop="isXyba" :rules="[{required: true, message:'请选择是否需要备案', trigger:'blur'}]">
            <el-radio-group v-model="inputForm.isXyba">
              <el-radio v-for="item in $dictUtils.getDictList('yes_no')" :label="item.value" >{{item.label}}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="是否完成备案" prop="isWcba" :rules="[{required: true, message:'请选择是否完成备案', trigger:'blur'}]">
            <el-radio-group v-model="inputForm.isWcba">
              <el-radio v-for="item in $dictUtils.getDictList('yes_no')" :label="item.value" >{{item.label}}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
<!--        评估报告-->
        <div v-if="inputForm.reportType === '1'">
          <el-col :span="12">
            <el-form-item label="评估报告(份)" prop="assessmentReport" :rules="[{required: true, message:'请输入评估报告（份）', trigger:'blur'}]">
              <el-input size="medium" v-model="inputForm.assessmentReport" clearable></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="评估说明(份)" prop="assessmentExplain" :rules="[{required: true, message:'请输入评估说明（份）', trigger:'blur'}]">
              <el-input size="medium" v-model="inputForm.assessmentExplain" clearable></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="评估明细表(份)" prop="assessmentDetail" :rules="[{required: true, message:'请输入评估明细表（份）', trigger:'blur'}]">
              <el-input size="medium" v-model="inputForm.assessmentDetail" clearable></el-input>
            </el-form-item>
          </el-col>
        </div>
<!--        咨询报告-->
        <div v-if="inputForm.reportType === '2'">

          <el-col :span="12">
            <el-form-item label="咨询报告(份)" prop="consultingReport" :rules="[{required: true, message:'请输入咨询报告（份）', trigger:'blur'}]">
              <el-input size="medium" v-model="inputForm.consultingReport" clearable></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="评估说明(份)" prop="assessmentExplain" :rules="[]">
              <el-input size="medium" v-model="inputForm.assessmentExplain" clearable></el-input>
            </el-form-item>
          </el-col>

        </div>


        <el-col :span="12">
          <el-form-item label="用印类型" prop="sealType" :rules="[{required: true, message:'请输入用印类型', trigger:'blur'}]">
            <el-input size="medium" v-model="inputForm.sealType" clearable></el-input>
          </el-form-item>
        </el-col>

      </el-row>

    </el-form>
  </div>
</template>

<script>
  import SelectUserTree from '@/views/modules/utils/treeUserSelect'
  import ProofreadIssuedService from '@/api/sys/ProofreadIssuedService'
  import ProgramProjectListInfoService from '@/api/program/ProgramProjectListInfoService'
  export default {
    props: {
      businessId: {
        type: String,
        default: ''
      },
      formReadOnly: {
        type: Boolean,
        default: false
      }
    },
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        inputForm: {
          id: '',
          projectId: '',
          projectName: '',
          assessmentDate: '',
          reportNo: '',
          projectManager: '',
          department: '',
          isTxtz: '',
          isBgdzdgd: '',
          isXyba: '',
          isWcba: '',
          assessmentReport: '',
          assessmentExplain: '',
          assessmentDetail: '',
          consultingReport: '',
          sealType: '',
          status: '',
          agreeTime: '',
          agreeUserId: '',
          reportType: ''
        },
        keyWatch: '',
        yesOrNo: [
          {value: 0, label: '否'},
          {value: 1, label: '是'}
        ],
        AList: [
          {name: '经济行为涉及上市公司及其子公司的资产组合、无形资产、企业价值（包括业务及资产组）评估业务'},
          {name: '上市公司及其合并报表范围内的下属公司财务报告目的的评估及咨询业务'},
          {name: '经济行为涉及上市公司及其合并报表范围内的下属公司的除本条第（一）款规定之外的评估业务'},
          {name: '经济行为涉及非上市证券公司、非上市期货公司、非上市公众公司、拟上市公司及其合并报表范围内的下属公司的评估业务'},
          {name: '其中：非国有企业改制设立股份公司评估业务'}
        ],
        BList: [
          {name: '除A类业务以外的无形资产评估业务'},
          {name: '除A类业务以外的资产组合评估业务'},
          {name: '除A类业务以外的企业价值评估业务'},
          {name: '除A类业务以外的报务报告目的评估业务'},
          {name: '除A类业务以外的抵押担保目的评估业务'},
          {name: '除A类以外的，涉及上市公司、证券公司、期货公司、非上市公众公司、拟上市公司及其下属公司不用于市场交易的复核评估、追溯评估和咨询评估业务'},
          {name: '除A类以外的涉及国有企业与非国有企业之间的交易行为的评估业务及相关的复核评估、追溯评估和咨询评估业务'},
          {name: '金融企业（包括银行、保险公司、信托投资公司、基金管理公司、租赁公司等）的评估业务'},
          {name: '评估经验较少的新领域评估业务'}
        ],
        CList: [
          {name: '除A、B类业务以外的经济行为涉及单项资产的评估业务'},
          {name: '除A、B类业务以外的不用于市场交易的复核评估、追溯评估和咨询评估业务'}
        ],
        tabName: ''
      }
    },
    proofreadIssuedService: null,
    programProjectListInfoService: null,
    created () {
      this.proofreadIssuedService = new ProofreadIssuedService()
      this.programProjectListInfoService = new ProgramProjectListInfoService()
      this.proofreadIssuedService.findProjectInfoById(this.businessId).then(({data}) => {
        this.inputForm = this.recover(this.inputForm, data)
        this.inputForm = JSON.parse(JSON.stringify(this.inputForm))
      })
    },
    computed: {
      bus: {
        get () {
          return this.businessId
        },
        set (val) {
          this.businessId = val
        }
      }
    },
    watch: {
      'keyWatch': {
        handler (newVal) {
          if (this.bus) {
            if (this.bus !== 'false') {
              this.init('', this.bus)
            }
          } else {
            this.$nextTick(() => {
              this.$refs.inputForm.resetFields()
            })
          }
        }
      }
    },
    components: {
      SelectUserTree
    },
    methods: {
      getKeyWatch (keyWatch) {
        this.keyWatch = keyWatch
      },
      init (method, id, tabName) {
        this.tabName = tabName
        this.method = method
        this.inputForm = {
          id: '',
          projectId: '',
          projectName: '',
          assessmentDate: '',
          reportNo: '',
          projectManager: '',
          department: '',
          isTxtz: '',
          isBgdzdgd: '',
          isXyba: '',
          isWcba: '',
          assessmentReport: '',
          assessmentExplain: '',
          assessmentDetail: '',
          consultingReport: '',
          sealType: '',
          status: '',
          agreeTime: '',
          agreeUserId: '',
          reportType: ''
        }
        this.inputForm.projectId = id
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.loading = true
          this.proofreadIssuedService.findById(this.inputForm.projectId).then(({data}) => {
            if (!this.commonJS.isEmpty(data.id)) {
              this.$refs.inputForm.resetFields()
              this.inputForm = this.recover(this.inputForm, data)
              this.inputForm = JSON.parse(JSON.stringify(this.inputForm))
              this.loading = false
            } else {
              this.proofreadIssuedService.findProjectInfoById(this.inputForm.projectId).then(({data}) => {
                this.inputForm = this.recover(this.inputForm, data)
                this.inputForm = JSON.parse(JSON.stringify(this.inputForm))
                this.loading = false
              })
            }
          })
        })
      },
      saveForm (callback) {
        this.loading = true
        this.inputForm.status = '1'
        this.proofreadIssuedService.save(this.inputForm).then(({data}) => {
          callback(data.businessTable, data.businessId, this.inputForm)
          this.loading = false
        }).catch(() => {
          this.loading = false
        })
      },
      startForm (callback) {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.inputForm.status = '2'
            this.proofreadIssuedService.save(this.inputForm).then(({data}) => {
              callback(data.businessTable, data.businessId, this.inputForm)
              this.loading = false
            }).catch(() => {
              this.loading = false
            })
          }
        })
      },
      async agreeForm (callback) {
        await this.proofreadIssuedService.findById(this.inputForm.id).then(({data}) => {
          if (data.status !== '2') { // status的值不等于“审核中”，就弹出提示
            this.$message.error('任务数据已发生改变或不存在，请在待办任务中确认此任务是否存在')
            throw new Error()
          } else {
            this.$refs['inputForm'].validate((valid) => {
              if (valid) {
                this.loading = true
                this.inputForm.status = '5'
                this.inputForm.agreeTime = this.moment(new Date()).format('YYYY-MM-DD HH:mm:ss')
                let userId = JSON.parse(localStorage.getItem('user')).id
                this.inputForm.agreeUserId = userId
                this.proofreadIssuedService.save(this.inputForm).then(({data}) => {
                  callback(data.businessTable, data.businessId, this.inputForm)
                  this.loading = false
                }).catch(() => {
                  this.loading = false
                })
              }
            })
          }
        })
      },
      async updateStatusById (type) {
        await this.proofreadIssuedService.findById(this.inputForm.id).then(({data}) => {
          if (data.status !== '2') { // status的值不等于“审核中”，就弹出提示
            this.$message.error('任务数据已发生改变或不存在，请在待办任务中确认此任务是否存在')
            throw new Error()
          } else {
            if (type === 'reject') {
              // 驳回
              this.inputForm.status = '4'
              this.proofreadIssuedService.updateStatus(this.inputForm)
            }
            if (type === 'reback') {
              // 撤回
              this.inputForm.status = '3'
              this.proofreadIssuedService.updateStatus(this.inputForm)
            }
          }
        })
      },
      close () {
        this.inputForm = {
          id: '',
          projectId: '',
          projectName: '',
          assessmentDate: '',
          reportNo: '',
          projectManager: '',
          department: '',
          isTxtz: '',
          isBgdzdgd: '',
          isXyba: '',
          isWcba: '',
          assessmentReport: '',
          assessmentExplain: '',
          assessmentDetail: '',
          consultingReport: '',
          sealType: '',
          status: '',
          agreeTime: '',
          agreeUserId: '',
          reportType: ''
        }
        this.$refs.inputForm.resetFields()
        this.visible = false
      },
      // 下载报告签发单
      downloadReportAusstellungTpl () {
        // this.$utils.downloadExcel('/sys/project/import/template')
        this.loading = true
        this.programProjectListInfoService.downloadReportAusstellungTpl(this.inputForm.projectId).then((res) => {
          // 将二进制流文件写入excel表，以下为重要步骤
          this.$utils.downloadWord(res.data, '报告签发单')
          this.loading = false
        }).catch(function (err) {
          this.loading = false
          if (err.response) {
            console.log(err.response)
          }
        })
      }
    }
  }
</script>
<style scoped>
  /deep/ .w-e-text-container {
    z-index: 1 !important;
  }
  /deep/ .wtext {
    height: 200px
  }
</style>
