<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item label="用户名称" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="机构名称" prop="orgName">
        <el-input
          v-model="queryParams.orgName"
          placeholder="请输入机构名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item v-for="(item,index) in queDataList" :key="item.label" :label="item.label" :prop="item.prop">
        <el-select v-model="queryParams.queData[index]" placeholder="请选择" clearable>
          <el-option v-for="o in item.resultList" :key="o.id" :label="o.value" :value="o.value"></el-option>
        </el-select>
      </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"-->
      <!--          plain-->
      <!--          icon="el-icon-plus"-->
      <!--          size="mini"-->
      <!--          @click="handleAdd"-->
      <!--          v-hasPermi="['yiqing:yiqing:add']"-->
      <!--        >新增-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="success"-->
      <!--          plain-->
      <!--          icon="el-icon-edit"-->
      <!--          size="mini"-->
      <!--          :disabled="single"-->
      <!--          @click="handleUpdate"-->
      <!--          v-hasPermi="['yiqing:yiqing:edit']"-->
      <!--        >修改-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="danger"-->
      <!--          plain-->
      <!--          icon="el-icon-delete"-->
      <!--          size="mini"-->
      <!--          :disabled="multiple"-->
      <!--          @click="handleDelete"-->
      <!--          v-hasPermi="['yiqing:yiqing:remove']"-->
      <!--        >删除-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['yiqing:yiqing:export']"
        >导出
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-check"
          size="mini"
          :loading="exportLoading"
          @click="belongSubmit"
        >提交
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          plain
          icon="el-icon-close"
          size="mini"
          :loading="exportLoading"
          @click="open = false;"
        >取消
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="yiqingList"
              @selection-change="handleSelectionChange" border>
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="登记时间" align="center" prop="createTime"/>
      <el-table-column label="用户编号" align="center" prop="userNo" width="100px"/>
      <el-table-column label="用户名称" align="center" prop="userName" width="100px"/>
      <el-table-column label="机构名称" align="center" prop="orgName"/>
      <el-table-column label="空闲情况" align="center" prop="vacateTime" width="290">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.vacateTime === '空闲中'" type="success">{{scope.row.vacateTime}}</el-tag>
          <el-tag v-else type="warning">{{scope.row.vacateTime}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column v-for="item in queDataList" :key="item.label" :label="item.label" :prop="item.prop"/>
      <!--      <el-table-column label="登记问题" width="600">-->
      <!--        <template slot-scope="scope">-->
      <!--          <div v-for="(item,i) in yiqingList[scope.$index].list" :label="item.queText">-->
      <!--            <div>-->
      <!--              {{i+1 +"." +item.queText}}-->
      <!--            </div>-->
      <!--            <div style="padding: 0 0 5px 20px">-->
      <!--              {{item.queResultText}}-->
      <!--            </div>-->
      <!--          </div>-->
      <!--        </template>-->
      <!--      </el-table-column>-->

      <!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
      <!--        <template slot-scope="scope">-->
      <!--          <el-button-->
      <!--            size="mini"-->
      <!--            type="text"-->
      <!--            icon="el-icon-edit"-->
      <!--            @click="handleUpdate(scope.row)"-->
      <!--            v-hasPermi="['yiqing:yiqing:edit']"-->
      <!--          >修改-->
      <!--          </el-button>-->
      <!--          <el-button-->
      <!--            size="mini"-->
      <!--            type="text"-->
      <!--            icon="el-icon-delete"-->
      <!--            @click="handleDelete(scope.row)"-->
      <!--            v-hasPermi="['yiqing:yiqing:remove']"-->
      <!--          >删除-->
      <!--          </el-button>-->
      <!--        </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 :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item label="用户编号" prop="userNo">
          <el-input v-model="form.userNo" placeholder="请输入用户编号"/>
        </el-form-item>
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入用户名"/>
        </el-form-item>
        <el-form-item label="机构编号" prop="orgNo">
          <el-input v-model="form.orgNo" placeholder="请输入机构编号"/>
        </el-form-item>
        <el-form-item label="机构名称" prop="orgName">
          <el-input v-model="form.orgName" placeholder="请输入机构名称"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    addYiqing,
    beLongSave,
    delYiqing,
    exportYiqing,
    getYiqing,
    listYiqing,
    updateYiqing
  } from '@/api/yiqing/yiqing'
  import user from '@/store/modules/user'

  export default {
    name: 'Yiqing',
    dicts: ['is_delete'],
    props: {
      taskId: {
        type: String,
        default: null
      },
      level: {
        type: Number,
        default: 0
      },
      fatherOpen: {
        type: Boolean,
        default: false
      }
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 导出遮罩层
        exportLoading: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 疫情登记用户表格数据
        yiqingList: [],
        queDataList: [],
        // 弹出层标题
        title: '',
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 20,
          userNo: null,
          userName: null,
          orgNo: null,
          orgName: null,
          queData: [],
          queList: []
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          userName: [
            { required: true, message: '主键不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    created() {
      this.getList()
    },
    methods: {
      /** 任务负责人提交 */
      belongSubmit() {
        const level = this.level
        if (0 === level && 1 !== this.ids.length) {
          this.$modal.msgError('只能选一个主要负责人')
          return
        }
        beLongSave({
          'taskId': this.taskId,
          'belongLevel': this.level,
          'list': this.ids
        }).then(re => {
          this.$modal.msgSuccess(re.msg)
          this.$emit('getList')
        })
      },
      /** 查询疫情登记用户列表 */
      getList() {
        this.yiqingList = []
        this.queryParams.orgNo = user.state.organ
        this.loading = true
        listYiqing(this.queryParams).then(response => {
          if (0 === response.rows.length) {
            this.$modal.msgError('查询为空')
            this.loading = false
            return
          }
          this.yiqingList = response.rows
          this.queDataList = response.rows[0].list
          this.queryParams.queList = []
          this.queDataList.forEach((q) => {
            this.queryParams.queList.push(q.id)
          })
          this.total = response.total
          this.loading = false
        })
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          userNo: null,
          userName: null,
          orgNo: null,
          orgName: null,
          isDelete: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null
        }
        this.resetForm('form')
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.queryParams.queData = ['', '', '', '', '', '', '']
        this.queryParams.queList = []
        this.resetForm('queryForm')
        this.handleQuery()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => {
          return {
            'yqTaskId': this.taskId,
            'belongUserNo': item.userNo,
            'nickName': item.userName,
            'belongLevel': this.level
          }
        })
        const level = this.level
        if (0 === level && 0 !== this.ids.length) {
          if (1 !== this.ids.length) {
            this.$modal.msgError('请选择一个主要负责人')
            return
          }
        }
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset()
        this.open = true
        this.title = '添加疫情登记用户'
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset()
        const id = row.id || this.ids
        getYiqing(id).then(response => {
          this.form = response.data
          this.open = true
          this.title = '修改疫情登记用户'
        })
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs['form'].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateYiqing(this.form).then(response => {
                this.$modal.msgSuccess('修改成功')
                this.open = false
                this.getList()
              })
            } else {
              addYiqing(this.form).then(response => {
                this.$modal.msgSuccess('新增成功')
                this.open = false
                this.getList()
              })
            }
          }
        })
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids
        this.$modal.confirm('是否确认删除疫情登记用户编号为"' + ids + '"的数据项？').then(function() {
          return delYiqing(ids)
        }).then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        }).catch(() => {
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams
        this.$modal.confirm('是否确认导出所有疫情登记用户数据项？').then(() => {
          this.exportLoading = true
          return exportYiqing(queryParams)
        }).then(response => {
          this.$download.name(response.msg)
          this.exportLoading = false
        }).catch(() => {
        })
      }
    }
  }
</script>
