<template>
  <div class="app-container">
    <div class="filter-container">
      <el-form ref="query" :model="installerdata" style="display: inline-block;">
        <el-input v-model="installerdata.phone" placeholder="手机号" clearable style="width: 200px;" class="filter-item" @input="changeValue" />
        <el-select v-model="installerdata.status" placeholder="状态" clearable class="filter-item" style="width: 130px" @change="selectOne">
          <el-option v-for="item in calendarTypeOptions" :key="item" :label="item" :value="item" />
        </el-select>
        <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleSubmit">
          {{ $t('table.search') }}
        </el-button>
      </el-form>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-edit" @click="addinstaller">添加</el-button>
      <el-button v-if="total > 0 " v-waves :loading="downloadLoading" class="filter-item" type="primary" icon="el-icon-download" @click="handleDownload">
        {{ $t('table.export') }}
      </el-button>
    </div>
    <el-table :key="tableKey" v-loading="loading" :data="instller" border fit highlight-current-rows>
      <el-table-column prop="username" label="用户名" width="150">
        <template slot-scope="scope">
          <span>{{ scope.row.username }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="name" label="姓名" width="150">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="phone" label="手机号" width="150">
        <template slot-scope="scope">
          <span @click="handleCopy(scope.row.phone,$event)">{{ scope.row.phone }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="email" label="邮箱" width="160">
        <template slot-scope="scope">
          <span @click="handleCopy(scope.row.email,$event)">{{ scope.row.email }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="addtime" label="创建时间" width="150">
        <template slot-scope="scope">
          <span>{{ scope.row.addtime | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="zip" label="状态" width="140">
        <template slot-scope="scope">
          <span :style="{color:( scope.row.status==1 ? '#67C23A' : '#909399' )}">{{ scope.row.status==1 ? '正常' : '禁用' }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="zip" label="是否被删除" width="150">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.delete==1" type="success" effect="dark">正常</el-tag>
          <el-popover v-if="scope.row.delete==2" width="160" trigger="hover" placement="top">
            <p>将禁用以及删除用户人员恢复</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="normalinstaller(scope.row.id)">恢复人员</el-button>
            </div>
            <el-button slot="reference" size="mini" type="danger">已删除</el-button>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template slot-scope="scope">
          <el-popover width="160" trigger="click" placement="top">
            <p>此删除,分丢弃废纸篓,和删除</p>
            <div style="text-align: right; margin: 0">
              <el-button v-if="scope.row.delete==1" size="mini" type="text" @click="deleteInsrall(scope.row.id, 2)">丢弃废纸篓</el-button>
              <el-button type="text" size="mini" @click="deleteInsrall(scope.row.id, 1)">删除</el-button>
            </div>
            <el-button slot="reference" size="mini" type="danger">删除</el-button>
          </el-popover>
          <el-button slot="reference" size="mini" type="primary" @click="getUpInsrall(scope.row.id)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="installerdata.page" :limit.sync="installerdata.limit" @pagination="installerlist" />
    <el-dialog title="添加装维人员" :visible.sync="dialogFormVisible">
      <el-form ref="addinstallerfrom" :model="addinstallerfrom" :rules="rules">
        <el-form-item label="用户名" :label-width="formLabelWidth" prop="username">
          <el-input v-model="addinstallerfrom.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="姓名" :label-width="formLabelWidth" prop="username">
          <el-input v-model="addinstallerfrom.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="手机号" :label-width="formLabelWidth" prop="phone">
          <el-input v-model="addinstallerfrom.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" :label-width="formLabelWidth" prop="email">
          <el-input v-model="addinstallerfrom.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" :label-width="formLabelWidth" prop="password">
          <el-input v-model="addinstallerfrom.password" type="password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" :label-width="formLabelWidth" prop="ispassword">
          <el-input v-model="addinstallerfrom.ispassword" type="password" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="okdialogFormVisible('addinstallerfrom')">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 编辑 -->
    <el-dialog title="编辑装维人员" :visible.sync="updialogFormVisible">
      <el-form ref="addinstallerfrom" :model="upDatainstaller" :rules="rules">
        <el-form-item label="用户名" :label-width="formLabelWidth" prop="username">
          <el-input v-model="upDatainstaller.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="姓名" :label-width="formLabelWidth" prop="username">
          <el-input v-model="upDatainstaller.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="手机号" :label-width="formLabelWidth" prop="phone">
          <el-input v-model="upDatainstaller.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" :label-width="formLabelWidth" prop="email">
          <el-input v-model="upDatainstaller.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" :label-width="formLabelWidth" prop="password">
          <el-input v-model="upDatainstaller.password" type="password" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="updialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="okUpdialogFormVisible('addinstallerfrom')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { parseTime } from '@/utils';
import Pagination from '@/components/Pagination'; // 分页
import { installerlist, seekInstaller, deleteInsrall, addinstaller, updateinstaller, postupdateinstaller, normalinstaller } from '@/api/installer';
import waves from '@/directive/waves'; // 水波纹效果
import clip from '@/utils/clipboard';
export default {
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      instller: [], // 列表
      tableKey: 0,
      downloadLoading: false,
      dialogFormVisible: false,
      updialogFormVisible: false,
      formLabelWidth: '120px',
      total: 0,
      installerdata: {
        limit: 10,
        page: 1,
        phone: undefined,
        status: undefined,
      },
      calendarTypeOptions: ['正常', '禁用'],
      addinstallerfrom: {
        username: undefined,
        name: undefined,
        phone: undefined,
        email: undefined,
        password: undefined,
        ispassword: undefined,
      },
      upDatainstaller: [],
      rules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          { min: 2, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur' },
        ],
        username: [
          { required: true, message: '请输入用户名', trigger: 'change' },
          { min: 2, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur' },
        ],
        phone: [
          { type: 'string', required: true, message: '请输入手机号', trigger: 'change' },
          { min: 11, max: 11, message: '长度在11字符', trigger: 'blur' },
        ],
        email: [
          { type: 'email', required: true, message: '请输入邮箱', trigger: 'change' },
        ],
        password: [
          { type: 'string', required: true, message: '请输入密码', trigger: 'change' },
          { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' },
        ],
        ispassword: [
          { type: 'string', required: true, message: '请输入密码', trigger: 'change' },
          { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' },
        ],
      },
    };
  },
  created() {
    this.installerlist(); // 用户列表
  },
  methods: {
    installerlist() {
      this.loading = true;
      installerlist(this.installerdata.limit, this.installerdata.page)
        .then(response => {
          this.instller = response.data.instllerdata;
          this.instller.forEach((element, index) => {
            element['index'] = (this.installerdata.page - 1) * this.installerdata.limit + index + 1;
          });
          this.total = response.data.count;
          this.loading = false;
        })
        .catch(err => {
          console.log(err);
        });
    },
    handleCopy(text, event) {
      clip(text, event);
    },
    selectOne(event, item) {
      console.log(event);
      if (event === '') {
        this.installerdata.status = undefined;
      }
    },
    changeValue(event, item) {
      if (event === '') {
        this.installerdata.phone = undefined;
      }
    },
    handleSubmit() {
      seekInstaller(this.installerdata)
        .then(response => {
          this.instller = response.data.instllerdata;
          this.instller.forEach((element, index) => {
            element['index'] = (this.installerdata.page - 1) * this.installerdata.limit + index + 1;
          });
          this.total = response.data.count;
        })
        .catch(err => {
          console.log(err);
        });
    },
    handleDownload() {
      this.downloadLoading = true;
      import('@/vendor/Export2Excel').then(excel => {
        const tHeader = ['id', '用户名', '姓名', '用户邮箱', '创建时间', '状态'];
        const filterVal = ['id', 'username', 'name', 'email', 'addtime', 'status'];
        const data = this.formatJson(filterVal, this.instller);
        excel.export_json_to_excel({
          header: tHeader,
          data,
          filename: '装维人员',
        });
        this.downloadLoading = false;
      });
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => {
        if (j === 'addtime') {
          return parseTime(v[j]);
        } else if (j === 'status') {
          return v[j] === 1 ? '正常' : '禁用';
        } else {
          return v[j];
        }
      }));
    },
    // 删除
    deleteInsrall(id, type) {
      deleteInsrall(id, type)
        .then(response => {
          if (response.code === 105) {
            this.$message({
              message: response.msg,
              type: 'warning',
            });
          } else {
            this.installerlist();
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    addinstaller() {
      this.dialogFormVisible = true;
    },
    okdialogFormVisible(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.addinstallerfrom.password !== this.addinstallerfrom.ispassword) {
            this.$message.error('两次密码不一致');
          } else {
            addinstaller(this.addinstallerfrom)
              .then(response => {
                if (response.code === 105) {
                  this.$message({
                    message: response.msg,
                    type: 'warning',
                  });
                } else if (response.code === 200) {
                  this.installerlist();
                  this.dialogFormVisible = false;
                  this.$refs[formName].resetFields();
                }
              })
              .catch(err => {
                console.log(err);
              });
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    getUpInsrall(id) {
      updateinstaller(id)
        .then(response => {
          if (response.code === 105) {
            this.$message({
              message: response.msg,
              type: 'warning',
            });
          } else {
            this.upDatainstaller = response.data;
            this.updialogFormVisible = true;
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    okUpdialogFormVisible(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          postupdateinstaller(this.upDatainstaller)
            .then(response => {
              if (response.code === 105) {
                this.$message({
                  message: response.msg,
                  type: 'warning',
                });
              } else if (response.code === 200) {
                this.installerlist();
                this.updialogFormVisible = false;
              }
            })
            .catch(err => {
              console.log(err);
            });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    normalinstaller(id) {
      normalinstaller(id)
        .then(response => {
          if (response.code === 105) {
            this.$message({
              message: response.msg,
              type: 'warning',
            });
          } else {
            this.installerlist();
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
  },
};
</script>

<style>
</style>
