<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <!--      <el-form-item label="资产编号" prop="aNumber">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.aNumber"-->
      <!--          placeholder="请输入资产编号"-->
      <!--          clearable-->
      <!--          size="small"-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label="资产名称" prop="aName">
        <el-input
          v-model="queryParams.aName"
          placeholder="请输入资产名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="入库类型 " prop="storageType">
        <el-select v-model="queryParams.storageType" placeholder="请选择入库类型 " clearable size="small">
          <el-option
            v-for="dict in storageTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="入库时间" prop="wareTime">
        <el-date-picker clearable size="small" style="width: 200px"
                        v-model="queryParams.wareTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择入库时间">
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['assets:input:add']"
        >新增
        </el-button>
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="success"-->
      <!--          icon="el-icon-edit"-->
      <!--          size="mini"-->
      <!--          :disabled="single"-->
      <!--          @click="handleUpdate"-->
      <!--          v-hasPermi="['assets:input:edit']"-->
      <!--        >修改-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <template>-->
      <!--          <template v-if="scope.row.instanceId==null">-->
      <!--            <el-button-->
      <!--              type="danger"-->
      <!--              icon="el-icon-delete"-->
      <!--              size="mini"-->
      <!--              :disabled="multiple"-->
      <!--              @click="handleDelete"-->
      <!--              v-hasPermi="['assets:input:remove']"-->
      <!--            >删除-->
      <!--            </el-button>-->
      <!--          </template>-->
      <!--        </template>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="warning"-->
      <!--          icon="el-icon-download"-->
      <!--          size="mini"-->
      <!--          @click="handleExport"-->
      <!--          v-hasPermi="['assets:input:export']"-->
      <!--        >导出-->
      <!--        </el-button>-->
      <!--      </el-col>-->
    </el-row>

    <el-table v-loading="loading" :data="inputList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center"/>
      <!--      <el-table-column label="id" align="center" prop="aId" width="50"/>-->
      <el-table-column label="资产编号" align="center" prop="aNumber"/>
      <el-table-column label="资产名称" align="center" prop="aName" width="80"/>
      <el-table-column label="规格型号" align="center" prop="specType"/>
      <el-table-column label="入库类型 " align="center" prop="storageType" :formatter="storageTypeFormat"/>
      <el-table-column label="入库时间" align="center" prop="wareTime" width="180">
        <template slot-scope="scope">
          <span>{{ formDate(scope.row.wareTime) }}</span>
        </template>
      </el-table-column>
      <!--      <el-table-column label="资产类型" align="center" prop="dbMaterielId">-->
      <el-table-column label="资产类型" prop="dbMaterielName" width="80">
      </el-table-column>
      <el-table-column label="供应商" align="center" prop="dbSupplierId" :formatter="selectVo"/>
      <el-table-column label="状态" align="center" prop="state" :formatter="statusFormat">
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime"/>
      <el-table-column label="操作" align="center" width="300" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <template v-if="scope.row.instanceId==null">
            <el-button
              type="success"
              icon="el-icon-edit"
              size="small"
              @click="commit(scope.row)"
              v-hasPermi="['assets:input:edit']"
            >提交审核
            </el-button>
            <el-button
              type="success"
              icon="el-icon-edit"
              size="small"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['assets:input:edit']"
            >修改
            </el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="small"
              @click="handleDelete(scope.row)"
              v-hasPermi="['assets:input:remove']"
            >删除
            </el-button>
          </template>
          <template v-else-if="scope.row.instanceId!=undefined">
            <template v-if="scope.row.taskId!=undefined">
              <el-button
                type="success"
                icon="el-icon-edit"
                size="small"
                @click="showReForm(scope.row)"
                v-hasPermi="['assets:input:edit']"
              >重新提交审核
              </el-button>
            </template>
            <el-button
              size="small" type="success"
              @click="showForms(scope.$index, scope.row)">查看
            </el-button>
            <el-button
              size="small" type="warning"
              @click="showImage(scope.$index, scope.row)">进度查看
            </el-button>
          </template>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改资产入库对话框 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="800px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="入库类型 " prop="storageType">
          <el-select v-model="form.storageType" placeholder="请选择入库类型 ">
            <el-option
              v-for="dict in storageTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="入库时间" prop="wareTime">
              <el-date-picker clearable
                              v-model="form.wareTime"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="选择入库时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="制单人" prop="sysUser">
              <el-input v-model="form.sysUser" placeholder="制单人" :disabled="true"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="资产类型" prop="dbMaterielId">
          <treeselect
            v-model="form.dbMaterielId"
            :options="treeOptions"
            :show-count="true"
            placeholder="选择上级菜单"
          />
        </el-form-item>
        <!--        <el-form-item label="资产编号" prop="aNumber">-->
        <!--          <el-input maxlength="20" v-model="form.aNumber" placeholder="请输入资产编号"/>-->
        <!--        </el-form-item>-->
        <el-form-item label="资产名称" prop="aName">
          <el-input maxlength="20" v-model="form.aName" placeholder="请输入资产名称"/>
        </el-form-item>
        <el-form-item label="规格型号">
          <el-input maxlength="20" v-model="form.specType" placeholder="请输入型号"/>
        </el-form-item>
        <el-form-item label="供应商" prop="dbSupplierId">
          <el-select v-model="form.dbSupplierId" placeholder="请选择入库类型 " clearable size="small">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="保质期" prop="shelfLifeEnd">
              <el-date-picker clearable style="width: 200px"
                              v-model="form.shelfLifeStart"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="选择保质期开始">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结束" prop="shelfLifeEnd">
              <el-date-picker clearable style="width: 200px"
                              v-model="form.shelfLifeEnd"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="选择保质期结束">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注" prop="remark">
          <el-input maxlength="200" type="textarea" v-model="form.remark" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <template>
          <template v-if="form.taskId==undefined">
            <el-button type="primary" @click="submitForm">确 定</el-button>
          </template>
          <template v-if="form.taskId!=undefined">
            <el-button type="primary" @click="recommit">提交审核</el-button>
          </template>
          <template>
            <el-button @click="cancel">取 消</el-button>
          </template>
        </template>
      </div>
    </el-dialog>
    <!--查看审核流程表单-->
    <el-dialog :close-on-click-modal="false" :title="hforms.title" :visible.sync="hforms.open" width="1000px">
      <!--历史表单组件-->
      <addEnterprise ref="addEnterprise"/>
    </el-dialog>
    <el-dialog :close-on-click-modal="false" :title="activitiImg.title" :visible.sync="activitiImg.open" width="1000px">
      <el-image :src="activitiImg.src"></el-image>
    </el-dialog>

  </div>
</template>

<script>
  import {
    listInput,
    getInput,
    commit,
    recommit,
    delInput,
    addInput,
    updateInput,
    exportInput
  } from '@/api/assets/input'

  import { treelist } from '@/api/system/materiel'

  import { select } from '@/api/system/supplier'

  import { getUserProfile } from '@/api/system/user'

  import Treeselect from '@riophae/vue-treeselect'
  import '@riophae/vue-treeselect/dist/vue-treeselect.css'
  //历史流程表单
  import addEnterprise from '@/views/activiti/formsActiviti/index.vue'

  //流程开始
  import { getByKey } from '@/api/activiti/definition'

  export default {
    components: {
      addEnterprise,
      Treeselect
    },
    name: 'Input',
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 资产入库表格数据
        inputList: [],
        // 弹出层标题
        title: '',
        // 是否显示弹出层
        open: false,
        // 入库类型 字典
        storageTypeOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          aNumber: undefined,
          aName: undefined,
          storageType: undefined,
          wareTime: undefined,
          dbMaterielId: undefined,
          dbSupplierId: undefined,
          specType: undefined,
          sysUserId: undefined,
          instanceId: undefined,
          shelf: undefined,
          shelfLifeStart: undefined,
          shelfLifeEnd: undefined,
          state: undefined
        },
        // 表单参数
        form: {},
        user: {
          id: undefined,
          name: undefined
        },
        // 表单校验
        rules: {
          aNumber: [
            { required: true, message: '资产编号不能为空', trigger: 'blur' },
            { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
          ],
          aName: [
            { required: true, message: '资产名称不能为空', trigger: 'blur' },
            { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
          ],
          storageType: [
            { required: true, message: '入库类型 不能为空', trigger: 'blur' }
          ],
          wareTime: [
            { required: true, message: '入库时间 不能为空', trigger: 'blur' }
          ],
          dbMaterielId: [
            { required: true, message: '资产类型不能为空', trigger: 'blur' }
          ],
          dbSupplierId: [
            { required: true, message: '供应商不能为空', trigger: 'blur' }
          ],
          sysUser: [
            { required: true, message: '制单人不能为空', trigger: 'blur' }
          ]
        },
        treeOptions: [],
        options: [],
        hforms: {
          title: '查看审核流程',
          open: false,
          data: []
        },
        activitiImg: {
          title: '查看审核流程图',
          open: false,
          src: ''
        },
        instanceId: ''
      }
    },
    created() {
      this.processDefinitionKey()
      this.getSysUser()
      this.getList()
      this.getDicts('storage_type').then(response => {
        this.storageTypeOptions = response.data
      })
      this.treelist()
      this.select()
    },
    methods: {
      /** 查询资产入库列表 */
      getList() {
        this.loading = true
        listInput(this.queryParams).then(response => {
          this.inputList = response.rows
          this.total = response.total
          this.loading = false
        })
      },

      // 入库类型 字典翻译
      storageTypeFormat(row, column) {
        return this.selectDictLabel(this.storageTypeOptions, row.storageType)
      },
      // 入库类型 字典翻译
      selectVo(row, column) {
        return this.selectFormat(this.options, row.dbSupplierId + '')
      },
      statusFormat(row, column) {
        var restatus = ''
        let obj = row.state
        switch (obj) {
          case 0:
            restatus = '审核中'
            break
          case 1:
            restatus = '不通过'
            break
          case 2:
            restatus = '通过'
            break
          default:
            restatus = '未提交审核'
        }
        return restatus
      }
      ,
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      }
      ,
      getSysUser() {
        getUserProfile().then(response => {
          this.user.name = response.data.userName
          this.user.id = response.data.userId
          console.log(this.user)
        })
      }
      ,
      // 表单重置
      reset() {
        this.form = {
          aId: undefined,
          aNumber: undefined,
          aName: undefined,
          storageType: undefined,
          wareTime: undefined,
          dbMaterielId: undefined,
          dbSupplierId: undefined,
          specType: undefined,
          sysUserId: undefined,
          sysUser: undefined,
          remark: undefined,
          instanceId: undefined,
          taskId: undefined,
          shelf: undefined,
          shelfLifeStart: undefined,
          shelfLifeEnd: undefined,
          state: undefined,
          createBy: undefined,
          createTime: undefined,
          updateBy: undefined,
          updateTime: undefined
        }
        this.resetForm('form')
      }
      ,
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList()
      }
      ,
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm('queryForm')
        this.handleQuery()
      }
      ,
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.aId)
        this.single = selection.length != 1
        this.multiple = !selection.length
      }
      ,
      /** 新增按钮操作 */
      handleAdd() {
        this.reset()
        this.open = true
        this.form.sysUser = this.user.name
        this.form.sysUserId = this.user.id
        this.title = '添加资产入库'
      }
      ,
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset()
        const aId = row.aId || this.ids
        getInput(aId).then(response => {
          this.form = response.data
          this.form.dbSupplierId = this.form.dbSupplierId + ''
          this.form.storageType = this.form.storageType + ''
          this.open = true
          this.title = '修改资产入库'
        })
      }
      ,
      /**
       * 提交审核*
       * */
      commit(row) {
        getByKey('assets_stock_in').then(response => {
          this.instanceId = response.data.id
          commit(row.aId, this.instanceId).then(response => {
            this.msgSuccess('ok')
            this.getList()
          })
        })
      }
      ,
      /**
       * 打开再次提交审核界面
       * */
      showReForm(row) {
        this.reset()
        const aId = row.aId || this.ids
        getInput(aId).then(response => {
          this.form = response.data
          this.form.dbSupplierId = this.form.dbSupplierId + ''
          this.form.storageType = this.form.storageType + ''
          this.form.taskId = row.taskId
          this.open = true
          this.title = '修改资产入库'
        })
      }
      ,
      /**
       * 再次提交审核
       * */
      recommit() {
        this.$refs['form'].validate(valid => {
          if (valid) {
            recommit(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess('修改成功')
                this.open = false
                this.getList()
              } else {
                this.msgError(response.msg)
              }
            })
          }
        })
      }
      ,
      /** 提交按钮 */
      submitForm: function() {
        this.$refs['form'].validate(valid => {
          if (valid) {
            if (this.form.aId != undefined) {
              updateInput(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess('修改成功')
                  this.open = false
                  this.getList()
                } else {
                  this.msgError(response.msg)
                }
              })
            } else {
              addInput(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess('新增成功')
                  this.open = false
                  this.getList()
                } else {
                  this.msgError(response.msg)
                }
              })
            }
          }
        })
      }
      ,
      /** 删除按钮操作 */
      handleDelete(row) {
        const aIds = row.aId || this.ids
        this.$confirm('是否确认删除资产入库编号为"' + aIds + '"的数据项?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return delInput(aIds)
        }).then(() => {
          this.getList()
          this.msgSuccess('删除成功')
        }).catch(function() {
        })
      }
      ,
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams
        this.$confirm('是否确认导出所有资产入库数据项?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return exportInput(queryParams)
        }).then(response => {
          this.download(response.msg)
        }).catch(function() {
        })
      }
      ,
      treelist() {
        treelist().then(response => {
          this.treeOptions = response.data
        })
      }
      ,
      select() {
        select().then(response => {
          this.options = response.data
        })
      }
      ,
      /*
     *显示流程表单
     * */
      showForms(index, row) {
        this.hforms.open = false
        this.hforms.open = true
        this.hforms.title = '查看审核流程'
        this.$nextTick(() => {
          this.$refs.addEnterprise.showForms(row.instanceId)
        })
      }
      ,
      showImage(index, row) {
        let instanceId = row.instanceId
        instanceId = process.env.VUE_APP_BASE_API + '/leaveNew/read-resource?processInstanceId=' + instanceId + '&t=' + Math.random()
        this.activitiImg.open = true
        this.activitiImg.src = instanceId
      },
      processDefinitionKey() {
        getByKey('assets_stock_in').then(response => {
          this.instanceId = null
          this.instanceId = response.data.id
          console.log(this.instanceId)
        })
      }

    }
  }
</script>
