<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :before-close="closeDialog"
    :visible.sync="visible"
    :modal="false"
    width="75%">
    <el-form :inline="true" :model="dataForm" :rules="dataRule" ref="dataForm">
      <div class="form-group dialog">
        <el-form-item label="审核路径名称" prop="pathName">
          <el-input v-model="dataForm.pathName" placeholder="审核路径名称"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="dataForm.status" placeholder="状态" style="width: 100%">
            <el-option
              v-for="item in optionsStatue"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="维护人" prop="opUserName">
          <el-input v-model="dataForm.opUserName" disabled placeholder="维护人"></el-input>
        </el-form-item>
        <el-form-item label="维护时间" prop="opDate">
          <el-input v-model="dataForm.opDate" disabled placeholder="维护时间"></el-input>
        </el-form-item>
        <el-form-item label="备注" class="full" prop="remarks">
          <el-input v-model="dataForm.remarks" placeholder="备注"></el-input>
        </el-form-item>
      </div>
    </el-form>
    <el-button @click.prevent="addRow()" style="float: left">添加</el-button>
    <el-table :data="dataList" tooltip-effect="dark" border stripe style="width: 100%; margin-bottom: 20px">
      <el-table-column
        width="80PX"
        label="级次"
        type="index">
      </el-table-column>
      <el-table-column
        prop="levelName"
        header-align="center"
        align="center"
        label="路径名称">
        <template slot-scope="scope">
          <el-input v-model="scope.row.levelName" placeholder="路径名称" show-word-limit/>
        </template>
      </el-table-column>
      <el-table-column
        prop="iffixAuditUser"
        header-align="center"
        align="center"
        label="是否指定审批人">
        <template slot-scope="scope">
          <el-select v-model="scope.row.iffixAuditUser" placeholder="是否指定审批人" @change="changeIffixAudit(scope.row)"
                     style="width: 100%">
            <el-option
              v-for="item in optionsiffixAudit"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </template>
      </el-table-column>
      <el-table-column
        prop="auditUserId"
        header-align="center"
        align="center"
        label="审核人">
        <template slot-scope="scope">
          <el-select v-model="scope.row.auditUserId" :disabled="scope.row.iffixAuditUser === 'F'"
                     filterable multiple collapse-tags placeholder="审核人" style="width: 100%">
            <el-option
              v-for="item in optionsUsers"
              :key="item.userId"
              :label="item.actualname"
              :value="item.userId">
            </el-option>
          </el-select>
        </template>
      </el-table-column>
      <el-table-column
        prop="iffinalItem"
        header-align="center"
        align="center"
        label="是否最后一级">
        <template slot-scope="scope">
          <el-select v-model="scope.row.iffinalItem" placeholder="是否最后一级" style="width: 100%">
            <el-option
              v-for="item in optionsIfStatus"
              :key="item.val"
              :label="item.name"
              :value="item.val">
            </el-option>
          </el-select>
        </template>
      </el-table-column>
      <el-table-column
        prop="remarks"
        header-align="center"
        align="center"
        label="备注">
        <template slot-scope="scope">
          <el-input v-model="scope.row.remarks" placeholder="备注" show-word-limit/>
        </template>
      </el-table-column>
      <el-table-column
        header-align="center"
        align="center"
        label="操作">
        <template slot-scope="scope">
          <el-button @click.native.prevent="deleteRow(scope.$index, dataList)" size="small">移除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeDia">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-preventReClick>确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        dataForm: {
          status: 'A'
        },
        visible: false,
        dataRule: {},
        dataList: [],
        optionsUsers: [],
        optionsiffixAudit: [{
          id: 'S',
          name: '是'
        }, {
          id: 'F',
          name: '否'
        }],
        optionsIfStatus: [{
          val: 'F',
          name: '否'
        }, {
          val: 'T',
          name: '是'
        }],
        optionsStatue: [{
          id: 'A',
          name: '有效'
        }, {
          id: 'S',
          name: '停用'
        }]
      }
    },
    methods: {
      init (val) {
        this.optionsUsers = []
        this.visible = true
        if (typeof val !== 'undefined') {
          this.queryAuditPath(val)
        } else {
          this.queryUsers()
        }
      },
      queryUsers () {
        this.$http({
          url: this.$http.adornUrl('/sys/user/selectUser'),
          method: 'post',
          data: ({mobile: 'auditUser'})
        }).then(({data}) => {
          if (typeof data.list !== 'undefined' && data.list !== null) {
            if (typeof data.list.users !== 'undefined' && data.list.users !== null) {
              this.optionsUsers = data.list.users
            }
          }
        })
      },
      queryAuditPath (val) {
        this.$http({
          url: this.$http.adornUrl('/engineering/auditPaths/queryAuditPath'),
          method: 'post',
          data: ({id: val})
        }).then(({data}) => {
          if (typeof data.list !== 'undefined' && data.list !== null) {
            if (typeof data.list.users !== 'undefined' && data.list.users !== null) {
              this.optionsUsers = data.list.users
            }
            if (typeof data.list.audit !== 'undefined' && data.list.audit !== null) {
              this.dataForm = data.list.audit
            }
            if (typeof data.list.auditPathsLevels !== 'undefined' && data.list.auditPathsLevels !== null) {
              this.dataList = data.list.auditPathsLevels
              if (typeof data.list.auditUserIds !== 'undefined' && data.list.auditUserIds !== null) {
                for (let use in data.list.auditUserIds) {
                  this.$set(this.dataList[use], 'auditUserId', data.list.auditUserIds[use])
                }
              }
            }
          }
        })
      },
      addRow () {
        let val = {
          levelName: null,
          auditUserId: null,
          auditUserName: null,
          iffixAuditUser: 'S',
          iffinalItem: 'T',
          remarks: null
        }
        this.dataList.push(val)
      },
      deleteRow (index, rows) { // 删除改行
        rows.splice(index, 1)
      },
      changeIffixAudit (row) {
        if (row.iffixAuditUser === 'F') {
          row.auditUserId = null
          row.auditUserName = null
        }
      },
      // 表单提交
      dataFormSubmit () {
        if (this.dataList.length === 0) {
          this.$message.error('请维护审批级次')
          return false
        }
        for (let x in this.dataList) {
          if (this.dataList[x].iffixAuditUser === 'S') {
            if (this.dataList[x].auditUserId === null || this.dataList[x].auditUserId === '' || this.dataList[x].auditUserId.length === 0) {
              this.$message.error('请维护第' + (Number(x) + 1) + '级的审核人')
              return false
            }
          }
        }
        this.$confirm(`是否提交?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let formData = new FormData()
          formData.append('auditPaths', JSON.stringify(this.dataForm))
          formData.append('auditPathsLevels', JSON.stringify(this.dataList))
          this.$http({
            url: this.$http.adornUrl(`/engineering/auditPaths/save`),
            method: 'post',
            data: formData
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 600,
                onClose: () => {
                  this.closeDia()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      closeDialog (done) {
        done()
        // location.reload()
        this.visible = false
        this.$emit('refreshDataList')
        Object.assign(this.$data, this.$options.data.call(this))
      },
      closeDia () {
        this.visible = false
        this.$emit('refreshDataList')
        Object.assign(this.$data, this.$options.data.call(this))
      }
    }
  }
</script>
