<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="90px">

      <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="usernum">
        <el-input
          v-model="queryParams.usernum"
          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="item in openStatus"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.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:business: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:business: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:business: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:business:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="businessList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="餐饮用户" align="center" prop="username" :show-overflow-tooltip="true"/>
      <el-table-column label="用户编号" align="center" prop="usernum" :show-overflow-tooltip="true"/>
      <!--<el-table-column label="合同号" align="center" prop="contract" :show-overflow-tooltip="true"/>-->
     <!-- <el-table-column label="所属市" align="center" prop="county" width="120" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span v-if="scope.row.city">{{ getAddressFormatter(scope.row.city) }}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>-->
      <el-table-column label="所属区" align="center" prop="county" width="180" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{ getAddressFormatter(scope.row.county) }}
          <span v-if="scope.row.street">-{{ getAddressFormatter(scope.row.street) }}</span>
          <span v-else></span>
        </template>
      </el-table-column>
      <!--<el-table-column label="所属街道/乡镇" align="center" prop="street" width="120" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span v-if="scope.row.street">{{ getAddressFormatter(scope.row.street) }}</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="company" width="120" :show-overflow-tooltip="true"/>
      <el-table-column label="联系电话" align="center" prop="phone" width="120"/>
      <el-table-column label="表计类型" align="center" prop="type" :formatter="typeFormatter" :show-overflow-tooltip="true"/>
      <el-table-column label="开通状态" align="center" prop="status" :formatter="statusFormatter" :show-overflow-tooltip="true"/>
      <el-table-column label="通气日期" align="center" prop="starttime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.starttime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <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="handleDetail(scope.row)"
            v-hasPermi="['baseInfo:business:query']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['baseInfo:business:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['baseInfo:business: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 destroy-on-close :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">

        <el-row>
          <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="usernum">
              <el-input v-model="form.usernum" placeholder="请输入用户编号" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <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="company">
              <el-input v-model="form.company" 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="proviceChange">
                  <el-option
                    v-for="item in proviceData"
                    :key="item.id"
                    :label="item.areaName"
                    :value="item.id"
                  />
                </el-select>
              </el-col>

              <el-col :span="8" style="margin-left: 8px">
                <el-select v-model="form.county" placeholder="所属区县" @change="countyChange">
                  <el-option
                    v-for="item in countyData"
                    :key="item.id"
                    :label="item.areaName"
                    :value="item.id"
                  />
                </el-select>
              </el-col>

              <el-col :span="8" style="margin-left: 8px" >
                <el-select v-model="form.street" placeholder="所属乡镇">
                  <el-option
                    v-for="item in streeData"
                    :key="item.id"
                    :label="item.areaName"
                    :value="item.id"
                  />
                </el-select>
              </el-col>
            </el-form-item>
        </el-row>

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

        <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-row>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="phone">
              <el-input v-model="form.phone" maxlength="11" placeholder="请输入联系电话" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="表计类型" prop="type">
              <el-select v-model="form.type" placeholder="请选择表计类型" style="width: 100%">
                <el-option
                  v-for="item in meterType"
                  :key="item.dictValue"
                  :label="item.dictLabel"
                  :value="item.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="开通状态" prop="status">
              <el-select v-model="form.status" placeholder="请选择开通状态" style="width: 100%">
                <el-option
                  v-for="item in openStatus"
                  :key="item.dictValue"
                  :label="item.dictLabel"
                  :value="item.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="通气日期" prop="starttime">
              <el-date-picker clearable size="small"
                              v-model="form.starttime"
                              style="width: 100%"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="选择通气日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="表具类型" prop="meterType">
              <el-select v-model="form.meterType" placeholder="请选择表具类型" style="width: 100%">
                <el-option label="皮膜" :value="1" />
                <el-option label="超声" :value="2" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="表具厂家" prop="meterCompany">
              <el-select v-model="form.meterCompany" placeholder="请选择表具厂家" style="width: 100%">
                <el-option label="金卡" :value="1" />
                <el-option label="秦川" :value="2" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="表具型号" prop="meterModel">
              <el-select v-model="form.meterModel" placeholder="请选择表具型号" style="width: 100%">
                <el-option label="G1.5" :value="1" />
                <el-option label="G2.5" :value="2" />
                <el-option label="G4" :value="3" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="进气方向" prop="direction">
              <el-select v-model="form.direction" placeholder="请选择进气方向" style="width: 100%">
                <el-option label="左" :value="1" />
                <el-option label="右" :value="2" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-form-item label="燃气用具" prop="direction">
            <el-form :model="gasUseDevice" label-width="80px">
              <el-row>
                <el-col :span="6">
                  <el-form-item label="双眼灶" prop="bothEyes">
                    <el-input v-model="gasUseDevice.bothEyes" placeholder="数量" />
                  </el-form-item>
                </el-col>

                <el-col :span="6">
                  <el-form-item label="单眼灶" prop="singleEyes">
                    <el-input v-model="gasUseDevice.singleEyes" placeholder="数量" />
                  </el-form-item>
                </el-col>

                <el-col :span="6">
                  <el-form-item label="多眼灶" prop="manyEyes">
                    <el-input v-model="gasUseDevice.manyEyes" placeholder="数量" />
                  </el-form-item>
                </el-col>

                <el-col :span="6">
                  <el-form-item label="大锅灶" prop="cauldron">
                    <el-input v-model="gasUseDevice.cauldron" placeholder="数量" />
                  </el-form-item>
                </el-col>

              </el-row>

              <el-row>

                <el-col :span="6">
                  <el-form-item label="民用灶" prop="civil">
                    <el-input v-model="gasUseDevice.civil" placeholder="数量" />
                  </el-form-item>
                </el-col>

                <el-col :span="6">
                  <el-form-item label="烧烤灶" prop="barbecue">
                    <el-input v-model="gasUseDevice.barbecue" placeholder="数量" />
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="低汤灶" prop="lowSoup">
                    <el-input v-model="gasUseDevice.lowSoup" placeholder="数量" />
                  </el-form-item>
                </el-col>

                <el-col :span="6">
                  <el-form-item label="煲仔炉" prop="clayPot">
                    <el-input v-model="gasUseDevice.clayPot" placeholder="数量" />
                  </el-form-item>
                </el-col>
              </el-row>

              <el-row>
                <el-col :span="6">
                  <el-form-item label="矮仔炉" prop="dwarf">
                    <el-input v-model="gasUseDevice.dwarf" placeholder="数量" />
                  </el-form-item>
                </el-col>

                <el-col :span="6">
                  <el-form-item label="烤箱/炉" prop="oven">
                    <el-input v-model="gasUseDevice.oven" placeholder="数量" />
                  </el-form-item>
                </el-col>

                <el-col :span="6">
                  <el-form-item label="蒸汽炉/箱" prop="steamer">
                    <el-input v-model="gasUseDevice.steamer" placeholder="数量" />
                  </el-form-item>
                </el-col>

                <el-col :span="6">
                  <el-form-item label="煮面桶" prop="barrel">
                    <el-input v-model="gasUseDevice.barrel" placeholder="数量" />
                  </el-form-item>
                </el-col>
              </el-row>

              <el-row>
                <el-col :span="6">
                  <el-form-item label="壁挂炉" prop="wall">
                    <el-input v-model="gasUseDevice.wall" placeholder="数量" />
                  </el-form-item>
                </el-col>
              </el-row>

            </el-form>
          </el-form-item>

        </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>

    <!-- 坐标选择 -->
    <GetPos
      :dialogVisible.sync="dialogTableVisibles"
      :devicePos="devicePos"
      @close="dialogcancelFun"
      @getPath="getPath"
      style="z-index: 3000"
    />

    <!-- 详情 -->
    <DetailInfo ref="detail"/>
  </div>
</template>

<script>
import { listBusiness, getBusiness, delBusiness, addBusiness, updateBusiness, exportBusiness } from "@/api/baseinfo/businessInfo";
import GetPos from '@/components/GetPos';
import { aLLAreaListInfo } from "@/api/baseinfo/area";
import DetailInfo from "./components/DetailInfo";
export default {
  name: "Business",
  components: {
    GetPos,
    DetailInfo
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      userNames: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 餐饮用户表格数据
      businessList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        usernum: null,
        contract: null,
        county: null,
        street: null,
        village: null,
        address: null,
        longitude: null,
        latitude: null,
        company: null,
        username: null,
        phone: null,
        type: null,
        status: null,
        starttime: null,
        device: null,
        meterType: null,
        meterCompany: null,
        meterModel: null,
        direction: null,
        taskId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        username: [
          { required: true, message: "用户名不能为空", trigger: "blur" }
        ],
        usernum: [
          { required: true, message: "用户编号不能为空", trigger: "blur" }
        ],
        contract: [
          { required: true, message: "合同号不能为空", trigger: "blur" }
        ],
        company: [
          { required: true, message: "单位名称不能为空", trigger: "blur" }
        ],
        city:[
          { required: true, message: "区域不能为空", trigger: "change" }
        ],
        /*county: [
          { required: true, message: "请选择所属市", trigger: "change" }
        ],
        street: [
          { required: true, message: "请选择所属区/县", trigger: "change" }
        ],*/
        address: [
          { required: true, message: "具体地址不能为空", trigger: "blur" }
        ],
        longitude: [
          { 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)
              }
            }
          }
        ],
        status: [
          { required: true, message: "请选择开通状态", trigger: "change" }
        ],
        starttime: [
          { required: true, message: "请选择通气日期", trigger: "change" }
        ],
      },
      meterType: [],
      openStatus: [],
      dialogTableVisibles: false,
      devicePos: [],
      proviceData: [],
      address: [],
      countyData: [],
      streeData: [],
      addressInfo: [],
      //用气设备
      gasUseDevice: {
        bothEyes: 0,
        singleEyes: 0,
        manyEyes: 0,
        cauldron: 0,
        civil: 0,
        barbecue: 0,
        lowSoup: 0,
        clayPot: 0,
        dwarf: 0,
        oven: 0,
        steamer: 0,
        barrel: 0,
        wall: 0
      }
    };
  },
  created() {
    this.getList();
    this.getMeterType();
    this.getOpenStatus();
    this.getAddressInfo();
  },
  methods: {
    /** 查询餐饮用户列表 */
    getList() {
      this.loading = true;
      listBusiness(this.queryParams).then(response => {
        this.businessList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    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: null,
        starttime: null,
        device: null,
        meterType: null,
        meterCompany: null,
        meterModel: null,
        direction: null,
        taskId: null,
        remark: null
      };
      this.resetForm("form");
      this.countyData = [];
      this. streeData = [];
    },
    resetGasUseDevice(){
      this.gasUseDevice = {
          bothEyes: 0,
          singleEyes: 0,
          manyEyes: 0,
          cauldron: 0,
          civil: 0,
          barbecue: 0,
          lowSoup: 0,
          clayPot: 0,
          dwarf: 0,
          oven: 0,
          steamer: 0,
          barrel: 0,
          wall: 0
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.userNames = selection.map(item => item.username);
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.getProvinceInfo();
      this.resetGasUseDevice();
      this.open = true;
      this.title = "添加餐饮用户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBusiness(id).then(response => {
        this.form = response.data;
        this.getProvinceInfo();
        this.getCounty(this.form.city);
        if(this.form.street){
          this.getStree(this.form.county);
        }
        this.gasUseDevice = JSON.parse(this.form.device);
        this.open = true;
        this.title = "修改餐饮用户";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.device = JSON.stringify(this.gasUseDevice);
          if (this.form.id != null) {
            updateBusiness(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBusiness(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      const userNames = row.username || this.userNames;
      this.$confirm('是否确认删除餐饮用户名称为"' + userNames + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delBusiness(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有餐饮用户数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportBusiness(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    },
    //表计类型字典
    getMeterType(){
      this.getDicts("t_meter_type").then((response) => {
          this.meterType = response.data;
      });
    },
    //开通状态
    getOpenStatus(){
      this.getDicts("t_user_status").then((response) => {
          this.openStatus = response.data;
      });
    },
    /**
     * 经纬度坐标方法
     */
    MapdialogFun() {
      this.dialogTableVisibles = true;
    },
    /**
     * 地图关闭方法
     */
    dialogcancelFun() {
      this.dialogTableVisibles = false;
    },
    /**
     * 经纬度 选择
     * @param res
     */
    getPath(res){
      //确认选择经纬度
      this.form.longitude = res[0];
      this.form.latitude = res[1];
    },
    //获取市信息
    getProvinceInfo(){
      aLLAreaListInfo({areaLevel: 1}).then(res =>{
        if(res.code == 200){
          this.proviceData = res.data;
        }
      })
    },
    //获取县区信息
    getCounty(provinceId){
      aLLAreaListInfo({parentId: provinceId}).then(res =>{
        if(res.code == 200){
          this.countyData = res.data;
        }
      })
    },
    //获取街道镇信息
    getStree(countyId){
      aLLAreaListInfo({parentId: countyId}).then(res =>{
        if(res.code == 200){
          this.streeData = res.data;
        }
      })
    },
    //市选择
    proviceChange(){
      this.form.county = null;
      this.form.street = null;
      this.getCounty(this.form.city);
      this.streeData = [];
    },
    //区选择
    countyChange(){
      this.form.street = null;
      this.getStree(this.form.county);
    },
    //获取地址信息
    getAddressInfo(){
      aLLAreaListInfo().then(res =>{
        if(res.code == 200){
          this.addressInfo = res.data;
        }
      })
    },
    //市格式化
    getAddressFormatter(addressId){
      let address = this.addressInfo.find(item => item.id == addressId);
      return address ? address.areaName : "";
    },
    //表计类型
    typeFormatter(row){
      return this.selectDictLabel(
        this.meterType,
        row.type
      );
    },
    //开通状态
    statusFormatter(row){
      return this.selectDictLabel(
        this.openStatus,
        row.status
      );
    },
    //详情
    handleDetail(row){
      this.$refs.detail.getDetailInfo(row.id);
    }
  }
};
</script>
