<template>
  <div class='m-container'>
    <h2>
      <div class="squrt"></div>分部设置
    </h2>
    <div class="m-core">
      <div @click="onBranchOperation('create')"
           v-permission="'organization/add'"
           class='newBand'>添加</div>
      <!-- 列表 -->
      <div class="tableWrap">
        <el-table :data='tableList'
                  :header-cell-style="{background:'#EDEEF0',color:'#444'}">
          <el-table-column align='center'
                           prop="id"
                           label="分部编号">
          </el-table-column>
          <el-table-column align='center'
                           prop="name"
                           label="分部名称">
          </el-table-column>
          <el-table-column align='center'
                           prop="areaName"
                           label="城市">
          </el-table-column>
          <el-table-column align='center'
                           prop="address"
                           label="分部地址">
          </el-table-column>
          <el-table-column align='center'
                           label="成立时间">
            <template slot-scope="scope">
              {{ scope.row.registerDate | formatTimer }}
            </template>
          </el-table-column>
          <el-table-column align='center'
                           label="操作">
            <template slot-scope="scope">
              <el-button @click="onBranchOperation('update', scope.row)"
                         v-permission="'organization/update'"
                         type="text">修改</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination :total="pageInfo.total"
                    :page.sync="pageInfo.page"
                    :limit.sync="pageInfo.limit"
                    :page-sizes="pageInfo.page_size"
                    @pagination="getList" />
      </div>
    </div>

    <el-dialog :title="formTitle[formActionTitle]"
               :visible.sync="branchStatus"
               @close="onFormClose('ruleForm')"
               width="500px">
      <el-form :model="form"
               :rules="rules"
               ref="ruleForm">
        <el-form-item label="分部名称"
                      prop="name"
                      :label-width="formLabelWidth">
          <el-input v-model.trim="form.name"
                    autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="城市"
                      required
                      :label-width="formLabelWidth">
          <el-col :span="12">
            <el-form-item prop="province">
              <el-select v-model.trim="form.province"
                         filterable
                         clearable
                         @change="onChangeProvince">
                <el-option v-for="item in provinceList"
                           :key="item.value"
                           :label="item.label"
                           :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="city">
              <el-select v-model.trim="form.city"
                         filterable
                         clearable>
                <el-option v-for="item in cityList"
                           :key="item.value"
                           :label="item.label"
                           :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-form-item>
        <el-form-item label="分部地址"
                      prop="address"
                      :label-width="formLabelWidth">
          <el-input v-model.trim="form.address"
                    autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="成立时间"
                      prop="registerDate"
                      :label-width="formLabelWidth">
          <el-date-picker v-model.trim="form.registerDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择日期"></el-date-picker>
        </el-form-item>
      </el-form>
      <span slot="footer"
            class="dialog-footer">
        <el-button @click="branchStatus = false">取 消</el-button>
        <el-button @click="onBranchSubmit('ruleForm')"
                   type="primary">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>
<script>
import pagination from '@/components/Pagination/index'
import { branchQueryPage, areaQueryChild, branchAdd, branchUpdate, getParentArea } from '@/api/specialSetting'
import store from '@/store'
export default {
  name: 'branchSetting',
  components: { pagination },
  data () {
    return {
      tableList: [],
      pageInfo: {
        // 分页规则
        limit: 10, // 限制显示条数
        page: 1, // 当前页
        total: 0, // 总条数
        page_size: [10, 20, 40, 50] // 选择限制显示条数
      },
      organId: null,
      formActionTitle: 'create',
      formTitle: {
        create: '添加分部',
        update: '修改分部'
      },
      formLabelWidth: '100px',
      branchStatus: false,
      provinceList: [], // 省列表
      cityList: [], // 市列表
      form: {
        name: null,
        province: null,
        city: null,
        address: null,
        registerDate: null
      },
      rules: {
        name: [{ required: true, message: '请输入分部名称', trigger: 'blur' }],
        province: [{ required: true, message: '请选择省', trigger: 'change' }],
        city: [{ required: true, message: '请选择市', trigger: 'change' }],
        address: [{ required: true, message: '请输入分部地址', trigger: 'blur' }],
        registerDate: [{ required: true, message: '请选择成立时间', trigger: 'change' }]
      }
    }
  },
  mounted () {
    this.getList()
    this.getAreaList()
  },
  methods: {
    onBranchSubmit (formName) {
      this.$refs[formName].validate(valid => {
        if (!valid) return
        this.form.areaId = this.form.city
        if (this.formActionTitle == 'create') {
          if (this.form.id) { // 判断有没有Id，如果有则删除
            delete this.form.id
          }
          branchAdd(this.form).then(res => {
            this.messageTips('添加', res)
          })
        } else if (this.formActionTitle == 'update') {
          branchUpdate(this.form).then(res => {
            this.messageTips('修改', res)
          })
        }
      })
    },
    messageTips (title, res) {
      if (res.code == 200) {
        this.$message.success(title + '成功')
        this.branchStatus = false
        this.getList()
      } else {
        this.$message.error(res.msg)
      }
    },
    getList () {
      branchQueryPage({
        rows: this.pageInfo.limit,
        page: this.pageInfo.page
      }).then(res => {
        if (res.code == 200 && res.data) {
          this.tableList = res.data.rows
          this.pageInfo.total = res.data.total
        }
      })
    },
    onFormClose (formName) { // 关闭弹窗重置验证
      this.$refs[formName].resetFields()
    },
    getAreaList (parentId, callback) {
      parentId = parentId ? parentId : 0
      areaQueryChild({ parentId: parentId }).then(res => {
        if (res.code == 200 && res.data) {
          let tempData = []
          res.data.forEach(item => {
            tempData.push({
              label: item.name,
              value: item.id
            })
          })
          if (parentId) {
            this.cityList = tempData
          } else {
            this.provinceList = tempData
          }

          if (callback && typeof callback == 'function') {
            callback()
          }
        }
      })
    },
    onChangeProvince (val) {
      this.form.city = null
      this.getAreaList(val)
    },
    onBranchOperation (type, row) { // 修改分部信息
      this.formActionTitle = type
      this.branchStatus = true
      if (type == 'update') {
        getParentArea({ id: row.areaId }).then(res => {
          if (res.code == 200 && res.data) {
            this.getAreaList(res.data.id, () => {
              this.form = {
                id: row.id,
                name: row.name,
                province: res.data.id,
                city: row.areaId,
                address: row.address,
                registerDate: row.registerDate
              }
            })

          }
        })
      }
    }

  }
}
</script>
<style lang="scss" scoped>
/deep/.el-button--primary {
  background: #14928a;
  border-color: #14928a;
  color: #fff;
  &:hover,
  &:active,
  &:focus {
    background: #14928a;
    border-color: #14928a;
    color: #fff;
  }
}
/deep/.el-date-editor.el-input {
  width: 100% !important;
}
/deep/.el-select {
  width: 98% !important;
}
</style>