<template>
  <div id="index">
    <el-row>
      <el-col :span="24" class="main">
        <el-col :span="24" class="one">
          <el-col :span="24" class="one_1"> 公安端部门管理 </el-col>
          <el-col :span="24" class="one_2">
            <el-form ref="searchFrom" :model="searchFrom" label-width="100px">
              <el-col :span="24" class="one_2txt">
                <el-col :span="8">
                  <el-form-item label="编号">
                    <el-input v-model="searchFrom.num"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="名称">
                    <el-input v-model="searchFrom.name"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8" class="btn">
                  <el-button type="primary" size="mini" @click="search()">查询</el-button>
                  <el-button type="primary" size="mini" @click="toAdd()">添加</el-button>
                </el-col>
              </el-col>
            </el-form>
          </el-col>
        </el-col>
        <el-col :span="24" class="two">
          <data-table @query="search" :fields="fields" :opera="opera" :data="list" :total="total" @edit="toEdit"> </data-table>
        </el-col>
      </el-col>
    </el-row>
    <el-dialog title="部门信息管理" :visible.sync="show" width="50%" :before-close="close" :close-on-click-modal="false">
      <el-form :model="form" :rules="rules" ref="form" label-width="100px">
        <el-form-item label="部门类型" prop="type">
          <el-radio-group v-model="form.type">
            <el-radio label="1">市局</el-radio>
            <el-radio label="2">分局</el-radio>
            <el-radio label="3">派出所</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="市局" prop="city_id" v-if="form.type == '2' || form.type == '3'">
          <el-select v-model="form.city_id" clearable placeholder="请选择" style="width: 100%" @change="changeCity">
            <el-option v-for="(item, index) in cityList" :key="index" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="分局" prop="branch_id" v-if="form.type == '3'">
          <el-select v-model="form.branch_id" clearable placeholder="请选择" style="width: 100%">
            <el-option v-for="(item, index) in branchList" :key="index" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="编号" prop="num">
          <el-input v-model="form.num" placeholder="请输入编号"></el-input>
        </el-form-item>
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入部门名称"></el-input>
        </el-form-item>
        <el-form-item label="简介" prop="brief">
          <el-input v-model="form.brief" placeholder="请输入简介"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('form')">保存</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
const { mapActions: police_department } = createNamespacedHelpers('police_department');
import { mapState, createNamespacedHelpers } from 'vuex';
export default {
  name: 'index',
  props: {},
  components: {},
  data: function () {
    return {
      // 查询
      searchFrom: {},
      // 列表
      fields: [
        { label: '编号', model: 'num' },
        { label: '部门类型', model: 'type', prop: 'type', format: (i) => (i === '1' ? '市局' : i === '2' ? '分局' : '派出所') },
        { label: '部门名称', model: 'name' },
        { label: '简介', model: 'brief' },
      ],
      opera: [{ label: '维护', method: 'edit' }],
      list: [],
      total: 0,
      // 添加部门
      show: false,
      form: { type: '1' },
      rules: {
        num: [{ required: true, message: '请输入编号', trigger: 'blur' }],
        name: [{ required: true, message: '请输入部门名称', trigger: 'blur' }],
        type: [{ required: true, message: '请输入类型', trigger: 'change' }],
        brief: [{ required: true, message: '请输入简介', trigger: 'blur' }],
      },
      // 市局列表
      cityList: [],
      // 分局列表
      branchList: [],
    };
  },
  async created() {
    await this.search();
    await this.searchOther();
  },
  methods: {
    ...police_department(['query', 'create', 'update', 'delete']),
    async search({ skip = 1, limit = 10, ...info } = {}) {
      const condition = _.cloneDeep(this.searchFrom);
      let res = await this.query({ skip, limit, ...condition, ...info });
      if (this.$checkRes(res)) {
        this.$set(this, `list`, res.data);
        this.$set(this, `total`, res.total);
      }
    },
    // 查询市局
    async searchOther() {
      let res = await this.query({ type: '1' });
      if (this.$checkRes(res)) this.$set(this, `cityList`, res.data);
      res = await this.query({ type: '2' });
      if (this.$checkRes(res)) this.$set(this, `branchList`, res.data);
    },
    // 添加
    toAdd() {
      this.show = true;
    },
    // 提交
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          let data = this.form;
          if (data.id) {
            let res = await this.update(data);
            if (this.$checkRes(res)) {
              this.$message({ type: `success`, message: `修改信息成功` });
              this.close();
            }
          } else {
            let res = await this.create(data);
            if (this.$checkRes(res)) {
              this.$message({ type: `success`, message: `创建信息成功` });
              this.close();
            }
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    // 取消添加
    close() {
      this.form = { type: '1' };
      this.show = false;
      this.search();
    },
    // 选择市局
    async changeCity(value) {
      this.$set(this.form, `city_id`, value);
      let res = this.branchList.filter((i) => i.city_id == value);
      if (res) this.$set(this, `branchList`, res);
    },
    // 修改信息
    toEdit({ data }) {
      this.$set(this, `form`, data);
      this.show = true;
    },
  },
  computed: {
    ...mapState(['user']),
  },
  metaInfo() {
    return { title: this.$route.meta.title };
  },
  watch: {
    test: {
      deep: true,
      immediate: true,
      handler(val) {},
    },
  },
};
</script>

<style lang="less" scoped>
.main {
  .one {
    .one_1 {
      height: 40px;
      line-height: 40px;
      border-bottom: 1px solid #ccc;
      margin: 0 0 10px 0;
      font-size: 18px;
      font-weight: bold;
    }
    .one_2 {
      .one_2txt {
        border: 1px solid #ccc;
        /deep/.el-form-item {
          margin: 0;
        }
        /deep/.el-date-editor.el-input,
        .el-date-editor.el-input__inner {
          width: 100%;
        }
        /deep/.el-select {
          width: 100%;
        }
        .btn {
          text-align: right;
          padding: 5px 5px;
        }
      }
    }
  }
}
</style>
