<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="储配站" prop="stationId">
        <el-select v-model="queryParams.stationId" placeholder="请选择储配站">
          <el-option
            v-for="item in stationList"
            :key="item.stationId"
            :label="item.stationName"
            :value="item.stationId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="practitionerNum">
        <el-input
          v-model="queryParams.practitionerNum"
          placeholder="请输入工号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          size="small"
          style="width: 174px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="岗位" prop="postId">
        <el-select v-model="queryParams.postId" placeholder="请选择岗位">
          <el-option
            v-for="post in postList"
            :key="post.postId"
            :label="post.postName"
            :value="post.postId"
          />
        </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"
        >新增从业人员</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          style="color: #1890ff"
        >导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          plain
          icon="el-icon-download"
          size="mini"
		     :loading="exportLoading"
          @click="handleExport"
          style="color: #1890ff"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList">
      <el-table-column label="储配站" align="center" prop="stationName" width="120" :show-overflow-tooltip="true"/>
      <el-table-column label="工号" align="center" prop="practitionerNum" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="性别" align="center" prop="sex">
        <template slot-scope="scope">
          <span v-if="scope.row.sex == '0'">男</span>
          <span v-if="scope.row.sex == '1'">女</span>
        </template>
      </el-table-column>
      <el-table-column label="出生日期" align="center" prop="birthDate" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.birthDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="岗位" align="center" prop="postName"/>
      <el-table-column label="联系电话" align="center" prop="telNum" width="120"/>
      <el-table-column label="从业资格编号" align="center" prop="certificateNum" width="100"/>
      <el-table-column label="配送区域" align="center" prop="deliveryArea" width="120" :show-overflow-tooltip="true" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document"
            @click="showDetail(scope.row)"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</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="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="16">
            <el-row>
              <el-col :span="12">
                <el-form-item label="储配站" prop="stationId">
                  <el-select v-model="form.stationId" placeholder="请选择储配站">
                    <el-option
                      v-for="item in stationList"
                      :key="item.stationId"
                      :label="item.stationName"
                      :value="item.stationId"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="工号" prop="practitionerNum">
                  <el-input v-model="form.practitionerNum" placeholder="请输入工号" />
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="12">
                <el-form-item label="姓名" prop="name">
                  <el-input v-model="form.name" placeholder="请输入姓名" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="性别" prop="sex">
                  <el-select v-model="form.sex" placeholder="请选择性别">
                    <el-option label="男" value="0" />
                    <el-option label="女" value="1" />
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="12">
                <el-form-item label="出生日期" prop="birthDate">
                  <el-date-picker clearable size="small"
                                  v-model="form.birthDate"
                                  type="date"
                                  value-format="yyyy-MM-dd"
                                  style="width: 100%"
                                  placeholder="选择出生日期">
                  </el-date-picker>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="联系电话" prop="telNum">
                  <el-input v-model="form.telNum" placeholder="请输入联系电话"  prop="telNum"/>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="12">
                <el-form-item label="岗位" prop="postId">
                  <el-select v-model="form.postId" placeholder="请选择岗位">
                    <el-option
                      v-for="post in postList"
                      :key="post.postId"
                      :label="post.postName"
                      :value="post.postId"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="从业资格编号" prop="certificateNum">
                  <el-input v-model="form.certificateNum" placeholder="请输入从业资格编号" />
                </el-form-item>
              </el-col>
            </el-row>

            <el-form-item label="配送区域" prop="deliveryArea">
              <el-input v-model="form.deliveryArea" placeholder="请输入配送区域" />
            </el-form-item>

            <el-form-item label="证书文件" prop="licenseFile">
              <FileUpload
                listType="picture"
                @resFun="getFileInfoFile"
                @remove="listRemoveFile"
                :fileArr="fileListFile"
                :fileType="fileType"
              />
              <el-input v-show="false" disabled v-model="form.licenseFile"></el-input>
            </el-form-item>

            <el-row>
              <el-col :span="12">
                <el-form-item label="资格证有效时间" prop="certificateEffectiveDate">
                  <el-date-picker clearable size="small"
                                  v-model="form.certificateEffectiveDate"
                                  type="date"
                                  value-format="yyyy-MM-dd"
                                  style="width: 100%"
                                  placeholder="选择资格证有效时间">
                  </el-date-picker>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="密码" prop="password">
                  <el-input v-model="form.password" placeholder="请输入密码" type="password"/>
                </el-form-item>
              </el-col>
            </el-row>
          </el-col>

          <el-col :span="8">
            <el-form-item label="人员照片" prop="personPhoto">
              <MyFileUpload
                listType="picture-card"
                @resFun="getFileInfo"
                @remove="listRemove"
                :fileArr="photoList"
              />
              <el-input v-show="false" disabled v-model="form.personPhoto"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </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>

    <!-- 导入数据 -->
    <ImportData ref="importData"/>
    <!-- 详情 -->
    <DetailInfo ref="detailInfo"/>
  </div>
</template>

<script>
import { listInfo, getInfo, delInfo, addInfo, updateInfo, exportInfo } from "@/api/gasBottleTrack/practitionerManage";
import FileUpload from '@/components/FileUpload';
import MyFileUpload from '@/components/MyFileUpload';
import { gasStorageStationList } from "@/api/gasBottleTrack/gasHolderStationManage";
import { optionselect } from "@/api/system/post.js";
import uploadfile from "@/assets/uploadfile.png";
import ImportData from "./ImportData";
import DetailInfo from "./DetailInfo";
export default {
  name: "Info",
  components: {
    FileUpload,
    MyFileUpload,
    ImportData,
    DetailInfo
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 从业人员信息表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        stationId: null,
        practitionerNum: null,
        name: null,
        sex: null,
        birthDate: null,
        postId: null,
        telNum: null,
        deliveryArea: null,
        personPhoto: null,
        licenseFile: null,
        certificateNum: null,
        certificateEffectiveDate: null,
        password: null,
        isDel: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        stationId: [
          { required: true, message: "请选择储配站", trigger: "change" }
        ],
        practitionerNum: [
          { required: true, message: "请输入工号", trigger: "blur" }
        ],
        name: [
          { required: true, message: "请输入姓名", trigger: "blur" }
        ],
        postId: [
          { required: true, message: "请选择岗位", trigger: "change" }
        ],
        certificateNum: [
          { required: true, message: "请输入从业资格编号", trigger: "blur" }
        ],
        certificateEffectiveDate: [
          { required: true, message: "请选择资格证有效时间", trigger: "change" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" }
        ],
        telNum: [
          { required: true, message: "请输入联系电话", trigger: "blur" }
        ],

      },
      photoList: [],
      stationList: [],
      postList: [],
      fileListFile: [],
      fileType: ['doc', 'xls', 'ppt','txt','pdf'],
    };
  },
  created() {
    this.getList();
    this.getStationInfo();
    this.getPostInfo();
  },
  methods: {
    /** 查询从业人员信息列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        practitionerId: null,
        stationId: null,
        practitionerNum: null,
        name: null,
        sex: null,
        birthDate: null,
        postId: null,
        telNum: null,
        deliveryArea: null,
        personPhoto: null,
        licenseFile: null,
        certificateNum: null,
        certificateEffectiveDate: null,
        password: "123456",
        createTime: null,
        updateTime: null,
        isDel: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.practitionerId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.getStationInfo();
      this.getPostInfo();
      this.open = true;
      this.title = "新增从业人员信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getStationInfo();
      this.getPostInfo();
      const practitionerId = row.practitionerId || this.ids
      getInfo(practitionerId).then(response => {
        this.form = response.data;
        if(this.form.licenseFile){
          this.fileListFile = [
            {
              name: "证书文件",
              url: uploadfile,
            }];
        }

        if(this.form.personPhoto){
          this.photoList = [
            {
              url: this.form.personPhoto,
            }];
        }
        this.open = true;
        this.title = "修改从业人员信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.practitionerId != null) {
            updateInfo(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const practitionerIds = row.practitionerId || this.ids;
      this.$confirm('是否确认删除从业人员信息编号为"' + practitionerIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delInfo(practitionerIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有从业人员信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportInfo(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    },
    getFileInfo(res){
      this.form.personPhoto = res.url;
      //this.$set(this.feedBookForm,'iconUrl',res.url)
    },
    listRemove(e) {
      this.form.personPhoto = "";
      this.photoList = [];
    },
    handleImport(){
      this.$refs.importData.upload.title = "从业人员导入";
      this.$refs.importData.upload.open = true;
    },
    getStationInfo(){
      gasStorageStationList().then(res =>{
        if(res.code == 200){
          this.stationList = res.data;
        }
      })
    },
    getPostInfo(){
      optionselect().then(res =>{
        if(res.code == 200){
          this.postList = res.data;
        }
      })
    },
    getFileInfoFile(res) {
      this.form.licenseFile = res.url;
      this.fileListFile = [
        {
          name: res.fileName,
          url: uploadfile,
        },
      ];
    },
    listRemoveFile(e) {
      this.fileListFile = [];
      this.form.licenseFile = "";
    },
    showDetail(row){
      const practitionerId = row.practitionerId || this.ids
      getInfo(practitionerId).then(response => {
        this.$refs.detailInfo.form = response.data;
        this.$refs.detailInfo.detailOpen = true;
      })
    },
  }
};
</script>