<!--suppress ALL -->
<template>
  <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" append-to-body :close-on-click-modal="false">
    <el-form ref="dataForm" :rules="rules" :model="contractForm" label-well-code="left" label-width="120px" >
      <el-row :gutter="20">
        <el-col :span="10" :offset="1">
          <el-form-item label="合同编号" prop="contractNumber">
            <el-input v-model.trim="contractForm.contractNumber" type="text" placeholder="必填"/>
          </el-form-item>
        </el-col>
        <el-col :span="10" >
          <el-form-item label="合同名称" prop="contractName">
            <el-input v-model.trim="contractForm.contractName" type="text" placeholder="必填"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="10" :offset="1">
          <el-form-item label="合同金额" prop="contractAmount">
            <el-input v-model.trim="contractForm.contractAmount"  type="text" placeholder="必填" oninput="value=value.replace(/^\.+|[^\d.]/g,'')"  @blur="moneyChange">
              <template slot="append">万元</template>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="10" >
          <el-form-item label="合同乙方" prop="contractFactory">
            <el-select v-model="contractForm.contractFactory" filterable placeholder="合同乙方" clearable value="">
              <el-option
                v-for="item in factoryList"
                :key="item.id"
                :label="item.name"
                :value="item.name"/>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="10" :offset="1">
          <el-form-item label="负责人" prop="principal">
            <el-select v-model="contractForm.principal" filterable placeholder="负责人" clearable value="">
              <el-option
                v-for="item in principalList"
                :key="item.id"
                :label="item.name"
                :value="item.name"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="10" >
          <el-form-item label="合同执行情况" prop="isover">
            <el-select v-model="contractForm.isover" filterable placeholder="合同执行情况" clearable value="">
              <el-option
                v-for="item in stateList"
                :key="item.value"
                :label="item.label"
                :value="item.value"/>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="10" :offset="1">
          <el-form-item label="主要内容概述"   prop="contractContent">
            <el-input v-model.trim="contractForm.contractContent" type="textarea" rows = "5" placeholder="必填" />
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="备注"   prop="description">
            <el-input v-model.trim="contractForm.description" type="textarea" rows = "5"sssssss />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="saveData">提交</el-button>
      <el-button @click="cancel">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>

import { addContract,updateBase } from '@/api/contract'
import { getPrincipalList} from '@/api/devicetype'
import { getFactory } from '@/api/factory'

export default {
  name: 'EditContract',
  data() {
    return {
      dialogFormVisible: false, // 对话框是否显示
      dialogStatus: '', // 对话框类型：create,update
      deviceList: null, // 在产设备列表
      contractForm: {
        id:'',
        isover:'',
        contractName: '',
        contractNumber: '',
        contractContent: '',
        contractAmount: '',
        contractFactory: '',
        principal: '',
        description: ''
      }, // 表单
      stateList: [ {
        value: '0',
        label: '执行中'
      }, {
        value: '1',
        label: '已终结'
      }],
      equipmentFile: null,
      textMap: {
        update: '编辑合同基本信息',
        create: '新增合同',
        detail: '详情'
      }, // 表头显示标题
      deviceTypeList: null, // 设备类型列表
      rules: {
        contractName: [{ required: true, message: '合同名称不能为空', trigger: ['blur', 'change'] }],
        contractNumber: [{ required: true, message: '合同编号不能为空', trigger: ['blur', 'change'] }],
        contractContent: [{ required: true, message: '主要内容概述不能为空', trigger: ['blur', 'change'] }],
        contractAmount: [{ required: true, message: '合同金额为不能为空', trigger: ['blur', 'change'] }],
        contractFactory: [{ required: true, message: '合同乙方不能为空', trigger: [ 'change'] }],
        principal: [{ required: true, message: '负责人不能为空', trigger: [ 'change'] }],
        isover: [{ required: true, message: '合同执行情况不能为空', trigger: [ 'change'] }],
      }, // 前端校验规则
      isEditMode: false,
      principalList: null, // 批产负责人列表
      factoryList:null
    }
  },
  watch: {
  },
  mounted() {
  },
  created() {
    this.fetchPrincipalList()
    this.fetchFactoryList()
  },
  methods: {
    // 初始化对话框
    initDialog: function(dialogStatus, dialogFormVisible, row = null) {
      this.dialogStatus = dialogStatus
      this.dialogFormVisible = dialogFormVisible
      if (dialogStatus === 'create') { // 如果是新增，清除验证
        this.resetForm()
        this.isEditMode = false
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      } else if (dialogStatus === 'update') { // 如果是修改，将row中数据填写到输入框中
        this.contractForm = {
          id: row.id,
          isover: row.isover,
          description: row.description,
          contractName: row.contractName,
          contractNumber: row.contractNumber,
          contractContent: row.contractContent,
          contractAmount: row.contractAmount,
          contractFactory: row.contractFactory,
          principal: row.principal
        }
        this.isEditMode = true
      }
    },
    moneyChange(e){
      this.contractForm.contractAmount = e.target.value
    },
    // 重置表单
    resetForm() {
      this.equipmentFile = null
      this.contractForm = {
        id:'',
        isover:'',
        description:'',
        contractName: '',
        contractNumber: '',
        contractContent: '',
        contractAmount: '',
        contractFactory: '',
        principal: ''
      }
    },
    // 保存数据
    saveData: function() {
      if (this.dialogStatus === 'update') {
        this.updateData()
      } else if (this.dialogStatus === 'create') {
        this.createData()
      }
    },

    // 新增数据
    createData: function() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          addContract(this.contractForm).then(response => {
            if (response.code === 200) {
              this.$confirm('新增成功，是否继续新增?', '提示', {
                confirmButtonText: '是',
                cancelButtonText: '否',
                type: 'info'
              }).then(() => {
                this.resetForm()
                this.$nextTick(() => {
                  this.$refs['dataForm'].clearValidate()
                })
              }).catch(() => {
                this.$emit('watchChild')
                this.dialogFormVisible = false
              })
            }
          })
        }
      })
    },
    // 修改数据
    updateData: function() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          console.log(this.contractForm)
          updateBase(this.contractForm).then(response => {
            if (response.code === 200) {
              this.$message.success('修改成功')
              this.$emit('watchChild')
              this.dialogFormVisible = false
            }
          })
        }
      })
    },
    // 获取批产负责人
    fetchPrincipalList() {
      getPrincipalList('operator,producer').then(response => {
        this.principalList = response.data
      })
    },
    // 获取合同乙方
    fetchFactoryList() {
      getFactory().then(response => {
        this.factoryList = response.data
      })
    },
    cancel: function() {
      this.dialogFormVisible = false
      this.$emit('watchChild')
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .el-select{
    width: 100%;
  }
  .el-date-editor{
    width: 100%;
  }
  .labeltext{
    vertical-align: -webkit-baseline-middle;
  }
</style>
