<template>
  <div>
    <!-- <el-dialog
      :title="!id ? '发起评审': display ? '评审详情' : '修改'"
      width="70%"
      :close-on-click-modal="false"
      :visible.sync="visible"> -->
      <div class="my-title">{{ !id ? '发起评审': display ? '评审详情' : '修改' }}</div>
      <!-- 工作流 -->
      <div v-show="display && dataForm.workFlowBusinessExt">
        <el-steps :active="dataForm.workFlowBusinessExt&&dataForm.workFlowBusinessExt.workFlowProcessStepList?dataForm.workFlowBusinessExt.workFlowProcessStepList.length + 2:0" align-center style="margin-bottom: 20px">
          <template v-for="(item, i) in stepList">
            <el-step :icon="item.icon" :title="item.title" :description="item.description"></el-step>
          </template>
        </el-steps>
        <el-collapse style="margin-bottom: 20px">
          <el-collapse-item>
            <template slot="title">
              <span style="color: red">审批日志（展开查看更多）：</span>
            </template>
            <template v-for="(item, i) in logList">
              <div>{{++i}}：{{item.approverName}}  {{item.createTime}}  {{item.approvalValue}}</div>
            </template>
          </el-collapse-item>
        </el-collapse>
      </div>
      <!-- 表单 -->
      <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="auto">
        <el-row class="my-row">
          <el-col :span="8">
            <el-form-item label="评审编码" prop="reCode">
              <el-input v-model="dataForm.reCode" :disabled="true" placeholder="系统自动生成，无需填写"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8" style="padding-left: 20px">
            <el-form-item label="沟通编码" prop="coCode">
              <el-select
                v-model="dataForm.coCode"
                :disabled="display"
                remote
                placeholder="请选择"
                @change="coListSelected">
                <el-option
                  v-for="item in options1"
                  :key="item.value"
                  :label="item.coCode + ' (' + item.customerName + ')'"
                  :value="item.coCode">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8" style="padding-left: 20px">
            <el-form-item label="评审类别" prop="reType">
              <el-select
                v-model="dataForm.reType"
                :disabled="display"
                remote
                placeholder="请选择">
                <el-option
                  v-for="item in options"
                  :key="item.code"
                  :label="item.value"
                  :value="item.code">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row class="my-row">
          <el-col :span="8">
            <el-form-item label="客户名称" prop="name">
              <el-input v-model="dataForm.name" disabled placeholder="客户名称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8" style="padding-left: 20px">
            <el-form-item label="联系人" prop="cellName">
              <el-input v-model="dataForm.cellName" disabled placeholder="联系人"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8" style="padding-left: 20px">
          </el-col>
        </el-row>
        <el-row class="my-row">
          <el-form-item label="备注说明">
              <el-input v-model="dataForm.notes" :disabled="display" placeholder="备注说明"></el-input>
            </el-form-item>
        </el-row>
        <el-row class="my-row">
          <upload-component :display="true" :title="'沟通信息表附件'" :accept="'*'" :file-obj-list="fileList1"/>
        </el-row>
        <el-row class="my-row">
          <upload-component :display="display" :title="'合同评审表'" :accept="'*'" :file-obj-list="fileList" @uploadSuccess="uploadSuccess"/>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="onChose">取消</el-button>
        <el-button v-if="!display" type="primary" @click="dataFormSubmit()">确定</el-button>
      </span>
    <!-- </el-dialog> -->
  </div>
</template>

<script>
  import { getCoCode, geTreDetail } from '@/api/cus'
  import { getDictList } from '@/api/dict'
  import { dealStepData, dealStepLogs } from '@/api/util'
  import UploadComponent from '../common/upload-component'
  export default {
    name: 'stock-order-inbound',
    components: {UploadComponent},
    computed: {
      orgId: {
        get () { return this.$store.state.user.orgId }
      }
    },
    data () {
      return {
        visible: false,
        dictType: 'material_type',
        options: [],
        options1: [],
        dataList: [],
        fileList: [],
        fileList1: [],
        stepList: [],
        logList: [],
        id: 0,
        dataForm: {
          coCode: '',
          name: '',
          cellName: '',
          reType: '',
          notes: '',
          attachList: []
        },
        dataRule: {
          coCode: [{ required: true, message: '沟通编码不能为空', trigger: 'change' }],
          reType: [{ required: true, message: '评审类别不能为空', trigger: 'change' }]
          // name: [{ required: true, message: '客户名称不能为空', trigger: 'blur' }],
          // cellName: [{ required: true, message: '联系人不能为空', trigger: 'blur' }]
        },
        display: false
      }
    },
    watch: {
      'dataForm.coCode' (value) {
        let v = this.options1.find((item) => { return item.coCode === value })
        if (v) {
          this.dataForm.name = v.customerName
          this.dataForm.cellName = v.contact
          if (v.attachList) {
            this.fileList1 = []
            v.attachList.forEach((item) => {
              this.fileList1.push({
                name: item.fileName,
                url: item.url,
                id: item.url
              })
            })
          }
        }
      }
    },
    methods: {
      onChose () {
        this.$emit('onChose')
      },
      async init (id, disable) {
        this.dataForm = {}
        this.display = disable
        this.fileList = []
        this.stepList = []
        this.logList = []
        this.visible = true
        this.id = id || 0
        // 获取评审类别
        await getDictList({type: 'review_type'}).then(({data}) => {
          if (data) {
            this.options = data
          }
        })
        await getCoCode().then(({data}) => {
          if (data && data.code === '200') {
            this.options1 = data.data
          }
        })
        if (!id) return
        await geTreDetail(id).then(({data}) => {
          if (data && data.code === '200') {
            this.dataForm = data.data
            // 流程图展示
            dealStepData(data.data.workFlowBusinessExt.workFlowProcessStepList, this.stepList)
            dealStepLogs(data.data.workFlowBusinessExt.processLogList, this.logList)
            // 附件显示
            this.fileList = []
            data.data.attachList.forEach((item) => {
              this.fileList.push({
                name: item.fileName,
                url: item.url,
                id: item.url
              })
            })
          }
        })
      },
      uploadSuccess (fileList) {
        this.fileList = fileList
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            let fList = this.fileList
            if (fList.length > 0) {
              this.dataForm.attachList = []
              for (let i = 0; i < fList.length; i++) {
                this.dataForm.attachList.push({
                  fileName: fList[i].name,
                  url: fList[i].url
                })
              }
            } else {
              this.$message.error('请上传文件')
              return
            }
            this.$http({
              url: this.$http.adornUrl(`/biz-service/flow/cusContractReview/submit`),
              method: 'post',
              data: this.$http.adornData({...this.dataForm, orgId: this.orgId})
            }).then(({data}) => {
              if (data && data.code === '200') {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.onChose()
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      validateField (type) {
        this.$refs.dataForm.validateField(type)
      },
      coListSelected (item) {
        // todo
        // console.log('item = ' + JSON.stringify(item))
      }
    }
  }
</script>

<style scoped>
.my-line{
  border-bottom: 1px solid #c0c4cc;
  margin-bottom: 10px;
}
.title{
  padding: 10px 0 ;
}
</style>
