<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" v-if="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>
          <el-select v-model="scope.row.auditUserId" v-if="scope.row.iffixAuditUser === 'T'"
                     filterable multiple collapse-tags placeholder="审核人" style="width: 100%">
            <el-option
              v-for="item in optionsUsers_s"
              :key="item.userId"
              :label="item.userName"
              :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()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {addCharge,reviewer,modify} from '@/api/system/auditPaths-add-or-update'
import { queryUserVal } from '@/api/warehouseBusiness/agreement'
import { listUser } from '@/api/system/user'

  export default {
    data () {
      return {
        dataForm: {
          status: 'A',
        },
        visible: false,
        dataRule: {},
        dataList: [],
        optionsUsers: [],
        optionsUsers_s:[],
        optionsiffixAudit: [{
          id: 'T',
          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') {
          console.log(val)
          this.queryAuditPath(val)
          this.queryUsers()
        } else {
          this.queryUsers()
        }
      },
      queryUsers () {
        queryUserVal().then((response)=>{
          // this.contrastList.createBy = response.user.userName
          // this.dataForm.opUserName = response.user.userName
          console.log(response)
          this.$set(this.dataForm, "opUserName", response.user.userName);
          this.$set(this.dataForm, "opDate", response.date.slice(0,10));
        })
        console.log(this.dataForm)
        //请求所有审核人角色
        reviewer().then(data =>{
          this.optionsUsers = data
          console.log(data)
        })
        //请求所有用户名
        listUser().then(response => {
          this.optionsUsers_s = response.rows
          console.log(response.rows)
        })
        // 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) {
        modify(val).then(res => {
          console.log(res)
          let data = res.data.data
          this.dataForm = data.auditPaths
          this.dataList = data.auditPathsLevels
          for (let index in this.dataList){
            this.dataList[index].auditUserId = data.auditUserIds[index]
          }
        })
      },
      addRow () {
        let val = {
          levelName: null,
          auditUserId: [],
          // auditUserName: null,
          iffixAuditUser: 'T',
          iffinalItem: 'T',
          remarks: null
        }
        this.dataList.push(val)
      },
      deleteRow (index, rows) { // 删除改行
        rows.splice(index, 1)
      },
      changeIffixAudit (row) {
        row.auditUserId = []
      },
      // 表单提交
      dataFormSubmit () {
        console.log(this.dataForm)
        console.log(this.dataList)
        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
            }
          }
        }
        let formData = new FormData()
        formData.append('auditPaths', JSON.stringify(this.dataForm))
        formData.append('auditPathsLevels', JSON.stringify(this.dataList))
        this.$confirm(`是否提交?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return addCharge(formData)
        }).then((data) => {
          console.log(data)
            if (data && data.code === 200) {
              this.$message({
                message: '操作成功',
                type: 'success'
              });
              this.closeDia()
            } else {
              this.$message.error(data.msg)
            }
        })

        // 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>
