<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryParams" :inline="true" v-show="showSearch" label-width="80px">
      <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="linkMobile">
        <el-input
          v-model="queryParams.linkMobile"
          placeholder="请输入联系电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="安装时间" prop="installTime">
        <el-date-picker clearable size="small"
                        v-model="queryParams.installTimeStart"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择起始时间">
        </el-date-picker><span style="color: #bebfc3"> - </span>
        <el-date-picker clearable size="small"
                        v-model="queryParams.installTimeEnd"
                        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"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="equipmentList" >
      <el-table-column label="用户名称" align="center" prop="userName" width="200px"/>
      <el-table-column label="联系电话" align="center" prop="linkMobile"/>
      <el-table-column label="所属企业" align="center" prop="enterpriseName" :formatter="enterpriseFormat" :show-overflow-tooltip="true"/>
      <el-table-column label="详细地址" align="center" prop="userAddress" width="380px"/>
      <el-table-column label="安装时间" align="center" prop="installTime"/>
      <el-table-column label="装置类型" align="center" prop="equipmentTypeExplainDict"/>
      <el-table-column label="品牌名称" align="center" prop="brandName"/>
      <el-table-column label="操作" align="center" width="180px">
        <template slot-scope="scope">
          <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-edit"
            @click="showDetail(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="title1" :visible.sync="open1" width="800px" append-to-body @cancel="cancel1">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="11">
            <el-form-item label="用户名称" prop="userName">
              <el-input v-model="form.userName" placeholder="请输入用户名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户编号" prop="userNo">
              <el-input v-model="form.userNo" placeholder="请输入用户编号"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="装置类型" prop="equipments">
              <el-select placeholder="请选择装置类型" v-model="form.equipments" multiple filterable clearable style="width: 100%" onchange="change()">
                <el-option
                  v-for="dict in options"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="linkMobile">
              <el-input v-model="form.linkMobile" placeholder="请输入联系电话" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="23">
            <el-form-item label="详细地址" prop="userAddress">
              <el-input type="textarea" v-model="form.userAddress" placeholder="请输入详细地址"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="品牌名称" prop="brandName">
              <el-input v-model="form.brandName" placeholder="请输入品牌名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="安装时间" prop="installTime">
              <el-date-picker clearable size="small"
                              v-model="form.installTime"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择安装时间"
                              style="width: 100%">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row> 
          
          <el-col span="11">
            <el-form-item label="所属企业" prop="enterpriseId">
              <el-select v-model="form.enterpriseId"  placeholder="请在下拉框中选择名称" maxlength="255" style="width: 100%;">
                <el-option v-for="item in enterpriseList" :key="item.enterpriseId" :label="item.enterpriseName" :value="item.enterpriseId">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="11">
            <el-form-item label="用户类型">
              <el-select v-model="form.userType" placeholder="请选择用户类型" style="width: 100%;">
                <el-option label="城镇用户" value="1" />
                <el-option label="农村用户" value="2" />
                <el-option label="单位用户" value="3" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="23">
            <el-form-item label="备注信息" prop="remarks">
              <el-input type="textarea" v-model="form.remarks" placeholder="请输入备注信息" />
            </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="cancel1">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title2" :visible.sync="open2" width="800px" append-to-body @cancel="cancel2">
      <el-form ref="detailForm" :model="detailForm" label-width="100px">
        <el-row>
          <el-col :span="11">
            <el-form-item label="用户名称:">
              <el-input v-model="detailForm.userName" disabled/>
            </el-form-item>
            <el-form-item label="身份证号:">
              <el-input v-model="detailForm.idCard" disabled/>
            </el-form-item>
            <el-form-item label="品牌名称:">
              <el-input v-model="detailForm.brandName" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户编号:">
              <el-input v-model="detailForm.userNo" disabled style="width: 100%"/>
            </el-form-item>
            <el-form-item label="联系电话:">
              <el-input v-model="detailForm.linkMobile" disabled/>
            </el-form-item>
            <el-form-item label="安装时间:">
              <el-input v-model="detailForm.installTime" disabled/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="装置类型" prop="equipments">
              <el-select placeholder="请选择装置类型" v-model="detailForm.equipments" multiple filterable style="width: 100%" disabled>
                <el-option
                  v-for="dict in options"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="用户类型">
              <el-select v-model="detailForm.userType" placeholder="请选择用户类型" style="width: 100%;" disabled>
                <el-option label="城镇用户" value="1" />
                <el-option label="农村用户" value="2" />
                <el-option label="单位用户" value="3" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="23">
            <el-form-item label="详细地址:">
              <el-input type="textarea" v-model="detailForm.userAddress" disabled/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="23">
            <el-form-item label="备注信息:">
              <el-input type="textarea" v-model="detailForm.remarks" disabled/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
    
    <!-- 详情 -->
    <DetailInfo ref="detail"/>
  </div>
</template>

<script>
import { listEquipment, getEquipment, delEquipment, addEquipment, updateEquipment, exportEquipment } from "@/api/standingBook/equipment";

import { selectTEnterprise} from "@/api/regulation/supervise";
import DetailInfo from "./DetailInfo";

export default {
  name: "Equipment",
  components: {
    DetailInfo
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 企业数据
      enterpriseList:[],
      // 非单个禁用
      single: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户加装安全装置台账表格数据
      equipmentList: [],
      // 装置类型字典
      options: [],
      // 弹出层标题
      title1: "",
      title2: "",
      // 是否显示弹出层
      open1: false,
      open2: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: null,
        idCard: null,
        linkMobile: null,
        installTimeStart: null,
        installTimeEnd: null
      },
      // 表单参数
      form: {},
      detailForm: {},
      // 表单校验
      rules: {
        userName: [
          { required: true, message: "请输入用户名称", trigger: "blur" },
        ],
        userAddress: [
          { required: true, message: "请输入详细地址", trigger: "blur" },
        ],
        linkMobile: [
          { required: true, message: "请输入联系电话", trigger: "blur" },
          { min: 11, max: 11, message: "长度11位", trigger: "blur" },
        ],
        idCard: [
          { required: true, message: "请输入身份证号", trigger: "blur" },
          { min: 18, max: 18, message: "长度18位", trigger: "blur" },
        ],
        equipments: [
          { required: true, message: "请选择装置类型", trigger: "blur" }
        ],
        installTime: [
          { required: true, message: "选择安装时间", trigger: "change" },
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("t_equipment_type").then(response => {
      this.options = response.data;
    });
    this.getEnterp();
  },
  methods: {
    getEnterp(){
      selectTEnterprise().then(response => {
        this.enterpriseList = response.data;
      });
    },
    change(){

    },
    /** 查询用户加装安全装置台账列表 */
    getList() {
      this.loading = true;
      listEquipment(this.queryParams).then(response => {
        this.equipmentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel1() {
      this.open1 = false;
      this.reset1();
    },
    cancel2() {
      this.open2 = false;
      this.reset2();
    },
    enterpriseFormat(row){
      return row.enterpriseName==null?"-":row.enterpriseName;
    },
    // 表单重置
    reset1() {
      this.form = {
        safeEquipmentId: null,
        userName: null,
        userNo: null,
        userAddress: null,
        idCard: null,
        linkMobile: null,
        installTime: null,
        equipmentType: null,
        equipments: [],
        brandName: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        isDel: null,
        remarks: null,
        enterpriseId: null
      };
      this.resetForm("form");
    },
    reset2() {
      this.detailForm = {
        safeEquipmentId: null,
        userName: null,
        userNo: null,
        userAddress: null,
        idCard: null,
        linkMobile: null,
        installTime: null,
        equipmentType: null,
        equipments: [],
        brandName: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        isDel: null,
        remarks: null
      };
      this.resetForm("detailForm");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        userName: null,
        idCard: null,
        linkMobile: null,
        installTimeStart: null,
        installTimeEnd: null
      },
      this.resetForm("queryParams");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset1();
      this.open1 = true;
      this.title1 = "添加用户加装安全装置台账"; 
      // 如果当前是企业，则直接赋值
      if (this.$store.state.user.roleId == 5) {
        this.form.enterpriseId = this.enterpriseList[0].enterpriseId;
      }
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset1();
      getEquipment(row.safeEquipmentId).then(response => {
        let data = response.data;
        data.equipments = [];
        this.form = data;
        if(this.form.equipmentType){
          this.form.equipments = this.form.equipmentType.split(",");
        }
        this.open1 = true;
        this.title1 = "修改用户加装安全装置台账";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          var str = this.form.equipments.toString();
          this.form.equipmentType = str;
          if (this.form.safeEquipmentId != null) {
            updateEquipment(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open1 = false;
              this.getList();
            });
          } else {
            addEquipment(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open1 = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      // const safeEquipmentIds = row.safeEquipmentId || this.ids;
      row.isDel = "1";
      this.$confirm('是否确认删除此条信息?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return updateEquipment(row);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有用户加装安全装置台账数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportEquipment(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    },
    /** 详细信息跳转 */
    showDetail(row) {
      // this.reset2();
      // getEquipment(row.safeEquipmentId).then(response => {
      //   this.detailForm = response.data;
      //   this.detailForm.equipments = this.detailForm.equipmentType.split(",");
      //   this.open2 = true;
      //   this.title2 = "用户加装安全装置台账详情";
      // });
      
      this.$refs.detail.getDetailInfo(row.safeEquipmentId);
    },
  }
};
</script>
