<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="场所名称" prop="csName">
        <el-input
          v-model="queryParams.csName"
          placeholder="请输入场所名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="负责人类型" prop="fzrType">
        <el-select
          v-model="queryParams.fzrType"
          placeholder="负责人类型"
          clearable
          style="width:100%"
        >
          <el-option
            v-for="dict in dict.type.szxc_fzr_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="负责人姓名" prop="fzrName">
        <el-input
          v-model="queryParams.fzrName"
          placeholder="请输入负责人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号" prop="cardId">
        <el-input
          v-model="queryParams.cardId"
          placeholder="请输入身份证号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-select
          v-model="queryParams.sex"
          placeholder="性别"
          clearable
          style="width:100%"
        >
          <el-option
            v-for="dict in dict.type.sys_user_sex"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否党员" prop="dyOrNot">
        <el-select
          v-model="queryParams.dyOrNot"
          placeholder="是否党员"
          clearable
          style="width:100%"
        >
          <el-option
            v-for="dict in dict.type.szxc_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="省" prop="province">
        <el-input
          v-model="queryParams.province"
          placeholder="请输入省"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="市" prop="city">
        <el-input
          v-model="queryParams.city"
          placeholder="请输入市"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="县" prop="county">
        <el-input
          v-model="queryParams.county"
          placeholder="请输入县"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </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="['szxc:response: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="['szxc:response: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="['szxc:response:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['szxc:response:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="responseList" @selection-change="handleSelectionChange" border>
      <el-table-column type="selection" width="45" align="center" />
      <el-table-column label="ID" align="center" prop="id"  min-width="60"/>
      <el-table-column label="场所名称" align="center" prop="csName"  min-width="120" show-overflow-tooltip/>
      <el-table-column label="负责人类型" align="center" prop="fzrType"  min-width="120" show-overflow-tooltip>
        <template slot-scope="scope">
          <dict-tag :options="dict.type.szxc_fzr_type" :value="scope.row.fzrType"/>
        </template>
      </el-table-column>
      <el-table-column label="负责人姓名" align="center" prop="fzrName"  min-width="120" show-overflow-tooltip/>
      <el-table-column label="手机号" align="center" prop="phone"  min-width="120" show-overflow-tooltip/>
      <el-table-column label="身份证号" align="center" prop="cardId"  min-width="150" show-overflow-tooltip/>
      <el-table-column label="性别" align="center" prop="sex"  min-width="120" show-overflow-tooltip>
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex"/>
        </template>
      </el-table-column>
      <el-table-column label="是否党员" align="center" prop="dyOrNot"  min-width="120" show-overflow-tooltip>
        <template slot-scope="scope">
          <dict-tag :options="dict.type.szxc_yes_no" :value="scope.row.dyOrNot"/>
        </template>
      </el-table-column>
      <el-table-column label="现居住地" align="center" prop="currentAddress"  min-width="160" show-overflow-tooltip/>
      <el-table-column label="省" align="center" prop="province"  min-width="120" show-overflow-tooltip/>
      <el-table-column label="市" align="center" prop="city"  min-width="120" show-overflow-tooltip/>
      <el-table-column label="县" align="center" prop="county"  min-width="120" show-overflow-tooltip/>
      <el-table-column label="户籍地址" align="center" prop="hjAddress"  min-width="160" show-overflow-tooltip/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width"  min-width="120" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['szxc:response:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['szxc:response: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="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="场所名称" prop="csName">
          <el-input readonly
                    placeholder="请选择场所"
                    v-model="form.csName">
            <el-button slot="append" icon="el-icon-search" @click="csQuery"></el-button>
          </el-input>
        </el-form-item>
        <el-form-item label="负责人类型" prop="fzrType">
          <el-select
            v-model="form.fzrType"
            placeholder="负责人类型"
            clearable
            style="width:100%"
          >
            <el-option
              v-for="dict in dict.type.szxc_fzr_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="负责人姓名" prop="fzrName">
          <el-input v-model="form.fzrName" placeholder="请输入负责人姓名" />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="身份证号" prop="cardId">
          <el-input v-model="form.cardId" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select
            v-model="form.sex"
            placeholder="性别"
            clearable
            style="width:100%"
          >
            <el-option
              v-for="dict in dict.type.sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="是否党员" prop="dyOrNot">
          <el-select
            v-model="form.dyOrNot"
            placeholder="是否党员"
            clearable
            style="width:100%"
          >
            <el-option
              v-for="dict in dict.type.szxc_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="现居住地" prop="currentAddress">
          <el-input v-model="form.currentAddress" placeholder="请输入现居住地" />
        </el-form-item>
        <el-form-item label="省/市/县" prop="province">
          <el-cascader style="width: 100%"
                       :props="{value: 'label'}" v-model="ssx"
                       :options="ssxData"
                       @change="handleChange"></el-cascader>
        </el-form-item>
        <el-form-item label="户籍地址" prop="hjAddress">
          <el-input v-model="form.hjAddress" placeholder="请输入户籍地址" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" 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>

    <el-dialog title="选择场所信息" :visible.sync="csInfo.visible" width="1000px" append-to-body>
      <el-form :model="csInfo.queryParams" ref="queryForm" size="small" :inline="true" label-width="130px">
        <el-form-item label="统一社会信用代码" prop="tyshxyCode">
          <el-input
            v-model="csInfo.queryParams.tyshxyCode"
            placeholder="请输入统一社会信用代码"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="场所名称" prop="csName">
          <el-input
            v-model="csInfo.queryParams.csName"
            placeholder="请输入场所名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="csInfoQuery">搜索</el-button>
        </el-form-item>
      </el-form>
      <el-alert title="双击选择场所" type="warning" :closable="false"></el-alert>
      <el-table v-loading="csInfo.loading" :data="csInfo.list" border @cell-dblclick="csInfoDblclick">
        <el-table-column label="所属网格" align="center" prop="deptName" min-width="120" show-overflow-tooltip/>
        <el-table-column label="场所名称" align="center" prop="csName" min-width="120" show-overflow-tooltip/>
        <el-table-column label="行业类别" align="center" prop="hyType" min-width="120" show-overflow-tooltip>
          <template slot-scope="scope">
            <dict-tag :options="dict.type.szxc_hy_type" :value="scope.row.hyType"/>
          </template>
        </el-table-column>
        <el-table-column label="场所类型" align="center" prop="csType" min-width="120" show-overflow-tooltip>
          <template slot-scope="scope">
            <dict-tag :options="dict.type.szxv_cs_type" :value="scope.row.csType"/>
          </template>
        </el-table-column>
        <el-table-column label="场所地址" align="center" prop="csAddress" min-width="140" show-overflow-tooltip/>
        <el-table-column label="统一社会信用代码" align="center" prop="tyshxyCode" min-width="160" show-overflow-tooltip/>
        <el-table-column label="员工数量" align="center" prop="employeeNum" min-width="120" show-overflow-tooltip/>
        <el-table-column label="商住一体" align="center" prop="szyt" min-width="120" show-overflow-tooltip>
          <template slot-scope="scope">
            <span v-if="scope.row.szyt==='0'">是</span>
            <span v-if="scope.row.szyt==='1'">否</span>
          </template>
        </el-table-column>
        <el-table-column label="房屋类型" align="center" prop="fwType" min-width="120" show-overflow-tooltip>
          <template slot-scope="scope">
            <dict-tag :options="dict.type.szxc_fw_type" :value="scope.row.fwType"/>
          </template>
        </el-table-column>
        <el-table-column label="法人姓名" align="center" prop="frName" min-width="120" show-overflow-tooltip/>
        <el-table-column label="成立日期" align="center" prop="clDate" min-width="120" show-overflow-tooltip>
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.clDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="营业期限" align="center" prop="yyqx" min-width="120" show-overflow-tooltip/>

        <!--        <el-table-column label="营业执照" align="center" prop="yyzzPicture" >
                  <template slot-scope="scope">
                    <image-preview :src="scope.row.yyzzPicture" :width="50" :height="50"/>
                  </template>
                </el-table-column>-->
      </el-table>
      <pagination
        v-show="csInfo.total>0"
        :total="csInfo.total"
        :page.sync="csInfo.queryParams.pageNum"
        :limit.sync="csInfo.queryParams.pageSize"
        @pagination="getlistCsmanage"
      />
    </el-dialog>

  </div>
</template>

<script>
import { listResponse, getResponse, delResponse, addResponse, updateResponse } from "@/api/szxc/response";
import { listCsmanage } from "@/api/szxc/csmanage";
import ssxData from '@/utils/ssxData.json';

export default {
  name: "Response",
  dicts: ['szxc_yes_no', 'szxc_hy_type', 'szxv_cs_type', 'szxc_fw_type', 'sys_user_sex', 'szxc_fzr_type'],
  data() {
    return {
      ssxData: ssxData,
      ssx: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 场所负责人表格数据
      responseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        csId: null,
        csName: null,
        fzrType: null,
        fzrName: null,
        phone: null,
        cardId: null,
        sex: null,
        dyOrNot: null,
        currentAddress: null,
        province: null,
        city: null,
        county: null,
        hjAddress: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        csName: [
          { required: true, message: "场所不能为空", trigger: "blur" }
        ],
        fzrType: [
          { required: true, message: "负责人类型不能为空", trigger: "blur" }
        ],
        fzrName: [
          { required: true, message: "负责人姓名不能为空", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "手机号不能为空", trigger: "blur" }
        ],
        cardId: [
          { required: true, message: "身份证号不能为空", trigger: "blur" }
        ],
        sex: [
          { required: true, message: "性别不能为空", trigger: "blur" }
        ]
      },
      // 场所信息
      csInfo: {
        visible: false,
        // 遮罩层
        loading: false,
        // 总条数
        total: 0,
        // 查询参数
        queryParams:{
          pageNum: 1,
          pageSize: 10,
        },
        // 查询列表
        list: [],
        // 选中数据
        selectRow: null
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询场所负责人列表 */
    getList() {
      this.loading = true;
      listResponse(this.queryParams).then(response => {
        this.responseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        csId: null,
        csName: null,
        fzrType: null,
        fzrName: null,
        phone: null,
        cardId: null,
        sex: null,
        dyOrNot: null,
        currentAddress: null,
        province: null,
        city: null,
        county: null,
        hjAddress: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.ssx = []
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    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 = "添加场所负责人";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getResponse(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改场所负责人";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateResponse(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addResponse(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除场所负责人编号为"' + ids + '"的数据项？').then(function() {
        return delResponse(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('szxc/response/export', {
        ...this.queryParams
      }, `response_${new Date().getTime()}.xlsx`)
    },
    csQuery(){
      this.csInfo.visible = true;
      this.getlistCsmanage()
    },
    /** 查询场所信息列表 */
    getlistCsmanage() {
      this.csInfo.loading = true;
      listCsmanage(this.csInfo.queryParams).then(response => {
        this.csInfo.list = response.rows;
        this.csInfo.total = response.total;
        this.csInfo.loading = false;
      });
    },
    // 场所信息查询
    csInfoQuery(){
      this.csInfo.queryParams.pageNum = 1;
      this.getlistCsmanage();
    },
    // 场所选择 选中数据
    csInfoDblclick(row) {
      this.csInfo.selectRow = row;
      this.form.csId = row.id;
      this.form.csName = row.csName;
      this.csInfo.visible = false
    },
    handleChange(value) {
      this.ssx = value;
      this.form.province = value[0];
      this.form.city = value[1];
      this.form.county = value[2];
    }
  }
};
</script>
