<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="单位名称" prop="company">
        <el-input
          v-model="queryParams.company"
          placeholder="请输入单位名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="流量计品牌" prop="usernum">
        <el-input
          v-model="queryParams.usernum"
          placeholder="请输入流量计品牌"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <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="status">
        <el-select v-model="queryParams.status" placeholder="请选择开通状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </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="['baseinfo:industry: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="['baseinfo:industry: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="['baseinfo:industry: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="['baseinfo:industry:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="industryList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="单位名称" align="center" prop="company" width="180" :show-overflow-tooltip="true"/>
      <el-table-column label="联系人姓名" align="center" prop="username" />
      <el-table-column label="联系电话" align="center" prop="phone" />
      <el-table-column label="流量计品牌" align="center" prop="usernum" :formatter="meterModelCompanyFormatter"/>
      <!--<el-table-column label="社会生产品种" align="center" prop="contract" />-->
      <el-table-column label="所属区域" align="center" prop="countyName" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{ scope.row.countyName }}
          <span v-if="scope.row.streetName">-{{ scope.row.streetName}}</span>
          <span v-else></span>
        </template>
      </el-table-column>
      <el-table-column label="具体地址" align="center" prop="address" width="180" :show-overflow-tooltip="true"/>
      <el-table-column label="流量计型号" align="center" prop="type">
        <template slot-scope="scope">
          <span v-if="scope.row.type == '1'">DN25</span>
          <span v-else-if="scope.row.type == '2'">DN50</span>
          <span v-else-if="scope.row.type == '3'">DN80</span>
          <span v-else-if="scope.row.type == '4'">DN100</span>
          <span v-else-if="scope.row.type == '5'">DN150</span>
          <span v-else-if="scope.row.type == '6'">DN200</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="开通状态" align="center" prop="status" :formatter="statusFormat" />
      <el-table-column label="通气日期" align="center" prop="starttime" width="180">
        <template slot-scope="scope">
          <span v-if="scope.row.starttime">{{ parseTime(scope.row.starttime, '{y}-{m}-{d}') }}</span>
          <span v-else>-</span>
        </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-document"
            @click="handleDetail(scope.row)"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['baseinfo:industry:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['baseinfo:industry: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="800px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
              <el-form-item label="单位名称" prop="company">
                <el-input v-model="form.company" placeholder="请输入单位名称" />
              </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="生产品种" prop="contract">
              <el-input v-model="form.contract" placeholder="请输入生产品种" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="流量计品牌" prop="usernum">
              <el-select v-model="form.usernum" placeholder="请选择流量计品牌" style="width: 100%">
                <el-option
                  v-for="dict in meterCompanyOptions"
                  :key="dict.dictValue"
                  :value="dict.dictValue"
                  :label="dict.dictLabel"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="流量计型号" prop="type">
              <el-select v-model.number="form.type" placeholder="请选择流量计型号">
                <!--<el-option
                  v-for="dict in typeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>-->
                <el-option label="DN25" value="1" />
                <el-option label="DN50" value="2" />
                <el-option label="DN80" value="3" />
                <el-option label="DN100" value="4" />
                <el-option label="DN150" value="5" />
                <el-option label="DN200" value="6" />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <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="phone">
              <el-input v-model="form.phone" placeholder="请输入联系电话" />
            </el-form-item>
          </el-col>
        </el-row>
        <!-- 市区县镇 -->
        <el-row>
          <el-form-item label="所属区域" prop="city">
            <el-col :span="7">
              <el-select v-model="form.city" placeholder="所属市" @change="(id) =>getAreaList(id,2)">
                <el-option
                  v-for="dict in oneAreaList"
                  :key="dict.id"
                  :label="dict.areaName"
                  :value="dict.id"
                />
              </el-select>
            </el-col>

            <el-col :span="8" style="margin-left: 8px">
              <el-select v-model="form.county" placeholder="所属区县" @change="(id) =>getAreaList(id,3)">
                <el-option
                  v-for="dict in twoAreaList"
                  :key="dict.id"
                  :label="dict.areaName"
                  :value="dict.id"
                />
              </el-select>
            </el-col>

            <el-col :span="8" style="margin-left: 8px" >
              <el-select v-model="form.street" placeholder="所属乡镇" @change="(id) =>getAreaList(id,4)">
                <el-option
                  v-for="dict in threeAreaList"
                  :key="dict.id"
                  :label="dict.areaName"
                  :value="dict.id"
                />
              </el-select>
            </el-col>

            <!--<el-col :span="6" style="margin-left: 8px">-->
              <!--<el-select v-model="form.village" placeholder="所属小区（村）">-->
                <!--<el-option-->
                  <!--v-for="dict in fourAreaList"-->
                  <!--:key="dict.id"-->
                  <!--:label="dict.name"-->
                  <!--:value="dict.id"-->
                <!--/>-->
              <!--</el-select>-->
            <!--</el-col>-->
          </el-form-item>
          <!--<el-col :span="1">-->
          <!--<img style="width: 30px;height: 30px;margin-left: 8px;margin-top: 4px;cursor: pointer" src="@/assets/icons/address.png"></img>-->
          <!--</el-col>-->
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="地址坐标" prop="longitude">
              <el-col :span="8">
                <el-input v-model="form.longitude" placeholder="请输入经度"/>
              </el-col>
              <el-col :span="8" style="margin-left: 20px">
                <el-input v-model="form.latitude" placeholder="请输入纬度"/>
              </el-col>
              <el-col :span="4" style="margin-left: 30px">
                <el-button type="primary" plain @click="MapdialogFun">选择经纬度</el-button>
              </el-col>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="具体地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入具体地址" />
        </el-form-item>


        <el-form-item label="开通状态">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="通气日期" prop="starttime">
          <el-date-picker clearable size="small"
            v-model="form.starttime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择通气日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" 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>
    <GetPos
      :dialogVisible.sync="dialogTableVisibles"
      :devicePos="devicePos"
      @close="dialogcancelFun"
      @getPath="getPath"
      style="z-index: 3000"
    />
    <DetailInfo ref="detail"/>
  </div>
</template>

<script>
import { listIndustry, getIndustry, delIndustry, addIndustry, updateIndustry, exportIndustry } from "@/api/baseinfo/industry";
import GetPos from '@/components/GetPos';
import { listArea} from "@/api/baseinfo/area";
import DetailInfo from "./components/DetailInfo"
export default {
  name: "Industry",
  components: {
    GetPos,
    DetailInfo
  },
  data() {
    return {
      //地图
      dialogTableVisibles: false,
      devicePos: [],
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工业用户表格数据
      industryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 用气场景字典
      typeOptions: [],
      // 开通状态字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        usernum: null,
        company: null,
        username: null,
        status: null
      },
      //地区关联查询
      queryParams2: {
        pageNum: 1,
        pageSize: 100,
        parentId:0
      },
      oneAreaList:[],
      twoAreaList:[],
      threeAreaList:[],
      fourAreaList:[],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        company: [
          { required: true, message: "单位名称不能为空", trigger: "blur" }
        ],
        contract: [
          { required: true, message: "生产品种不能为空", trigger: "blur" }
        ],
        usernum: [
          { required: true, message: "流量计品牌不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "用气设置不能为空", trigger: "blur" }
        ],
        /*username: [
          { required: true, message: "联系人不能为空", trigger: "blur" }
        ],*/
        phone: [
          { required: true, message: "用户手机不能为空", trigger: "blur" },
          {
            type: 'number',
            message: '手机号格式不正确',
            trigger: 'blur',
            transform(value) {
              let phonereg = 11 && /^((13|14|15|16|17|18|19)[0-9]{1}\d{8})$/
              if (!phonereg.test(value)) {
                return false
              } else {
                return Number(value)
              }
            }
          }
        ],
        city: [
          { required: true, message: "区域不能为空", trigger: "change" }
        ],
        /*longitude: [
          { required: true, message: "经纬度不能为空", trigger: "blur" }
        ],*/
      },
      meterCompanyOptions: []
    };
  },
  created() {
    this.getList();
    this.getAreaList(0,1)
    this.getDicts("t_user_scene").then(response => {
      this.typeOptions = response.data;
    });
    this.getDicts("t_user_status").then(response => {
      this.statusOptions = response.data;
    });
    this.getDicts("t_business_factory_meter_type").then(response => {
        this.meterCompanyOptions = response.data;
    });
  },
  methods: {
    /** 查询工业用户列表 */
    getList() {
      this.loading = true;
      listIndustry(this.queryParams).then(response => {
        this.industryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 用气场景字典翻译
    typeFormat(row, column) {
      if(row.type==undefined){
        return ""
      }
      var str = '';
      for(var tp of row.type.split(",")){
        str+= this.selectDictLabel(this.typeOptions, tp)+"、";
      }
      return str.substr(0,str.length-1)
      //return this.selectDictLabel(this.typeOptions, row.type);
    },
    // 开通状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // sheshiFormat(row, column){
    //   return this.selectDictLabel(this.sceneOptions, row.status);
    // },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        usernum: null,
        contract: null,
        city: null,
        county: null,
        street: null,
        village: null,
        address: null,
        longitude: null,
        latitude: null,
        company: null,
        username: null,
        phone: null,
        type: null,
        status: "0100",
        starttime: null,
        taskInspectId: null,
        remark: null,
        createTime: null
      };
      this.resetForm("form");
    },
    //获取地址
    getAreaList(e,level) {
      this.queryParams2.parentId = e;
      listArea(this.queryParams2).then(response => {
        if(level==1){
        this.oneAreaList = response.rows;
      }else if(level==2){
        this.twoAreaList = response.rows;
        this.form.county ='';
        this.form.street ='';
      }else if(level==3){
        this.threeAreaList = response.rows;
        this.form.street ='';
      }
    });
    },
    //用来回显
    getAreaListtwo(e,level) {
      this.queryParams2.parentId = e;
      if(level==4){
        return
      }
      listArea(this.queryParams2).then(response => {
        if(level==1){
        this.oneAreaList = response.rows;
      }else if(level==2){
        this.twoAreaList = response.rows;
      }else if(level==3){
        this.threeAreaList = response.rows;
      }
    });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加工业用户";
    },
    //详情
    handleDetail(row){
      this.$refs.detail.getDetailInfo(row.id);
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getIndustry(id).then(response => {
        this.form = response.data;
        /*if(this.form.type!=null&&this.form.type!=''){
          this.form.type = this.form.type.split(',')
        }*/
        this.getAreaListtwo(this.form.city,2);
        this.getAreaListtwo(this.form.county,3);
        this.open = true;
        this.title = "修改工业用户";
      });
    },
    /** 提交按钮 */
    submitForm() {
      /*if(this.form.type!=null&&this.form.type!=''){
        this.form.type = this.form.type.join(",")
      }else{
        this.form.type=''
      }*/
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateIndustry(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addIndustry(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    //开启地图
    MapdialogFun() {
      this.dialogTableVisibles = true;
    },
    getPath(res){
      //确认选择经纬度
      this.form.longitude = res[0];
      this.form.latitude = res[1];
    },
    /**
     * 地图关闭方法
     */
    dialogcancelFun() {
      this.dialogTableVisibles = false;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除所选择工业用户数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delIndustry(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有工业用户数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportIndustry(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    },
    meterModelCompanyFormatter(row){
      return this.selectDictLabel(
        this.meterCompanyOptions,
        row.usernum
      );
    },
  }
};
</script>
<style lang="scss" scoped>
  ::v-deep .el-select{
    width: 100%;
  }
  ::v-deep .el-date-editor.el-input, .el-date-editor.el-input__inner {
    width: 100%;
  }
</style>
